-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyusage.go
41 lines (36 loc) · 1007 Bytes
/
keyusage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package pcert
import (
"crypto/x509"
"sort"
"strings"
)
//go:generate go run x509_lists_gen.go
// KeyUsageToString returns a string representation of a x509.KeyUsage bitmask
func KeyUsageToString(bitmask x509.KeyUsage) string {
return strings.Join(KeyUsageToStringSlice(bitmask), ",")
}
// KeyUsageToStringSlice returns a slice with string representations of the x509.KeyUsage bitmask
func KeyUsageToStringSlice(bitmask x509.KeyUsage) []string {
usages := []string{}
for str, usage := range KeyUsages {
if usage&bitmask == usage {
usages = append(usages, str)
}
}
sort.Strings(usages)
return usages
}
// ExtKeyUsageToString returns a string representation of a []x509.ExtKeyUsage slice
func ExtKeyUsageToString(ku []x509.ExtKeyUsage) string {
usages := []string{}
for str, usage := range ExtKeyUsages {
for _, existingUsage := range ku {
if usage == existingUsage {
usages = append(usages, str)
break
}
}
}
sort.Strings(usages)
return strings.Join(usages, ",")
}