-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encryption_key.go
98 lines (86 loc) · 1.56 KB
/
encryption_key.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package encryption
import (
"bytes"
"encoding/json"
)
type EncryptionKey int64
const (
HS256 EncryptionKey = iota
HS384
HS512
RS256
RS384
RS512
ES256
ES384
ES512
PS256
PS384
PS512
)
func (j EncryptionKey) String() string {
return toEncryptionKeyString[j]
}
func (j EncryptionKey) FromString(keyType string) EncryptionKey {
return toEncryptionKeyID[keyType]
}
func (j EncryptionKey) GetFamily() EncryptionKeyType {
return toEncryptionKeyFamily[j]
}
func (s EncryptionKey) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(toEncryptionKeyString[s])
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
func (s *EncryptionKey) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
*s = toEncryptionKeyID[j]
return nil
}
var toEncryptionKeyString = map[EncryptionKey]string{
HS256: "HS256",
HS384: "HS384",
HS512: "HS512",
RS256: "RS256",
RS384: "RS384",
RS512: "RS512",
ES256: "ES256",
ES384: "ES384",
ES512: "ES512",
PS256: "PS256",
PS384: "PS384",
PS512: "PS512",
}
var toEncryptionKeyID = map[string]EncryptionKey{
"HS256": HS256,
"HS384": HS384,
"HS512": HS512,
"RS256": RS256,
"RS384": RS384,
"RS512": RS512,
"ES256": ES256,
"ES384": ES384,
"ES512": ES512,
"P256": PS256,
"PS384": PS384,
"PS512": PS512,
}
var toEncryptionKeyFamily = map[EncryptionKey]EncryptionKeyType{
HS256: HMAC,
HS384: HMAC,
HS512: HMAC,
RS256: RSA,
RS384: RSA,
RS512: RSA,
ES256: ECDSA,
ES384: ECDSA,
ES512: ECDSA,
PS256: RSA,
PS384: RSA,
PS512: RSA,
}