-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encryption_key_type.go
52 lines (42 loc) · 951 Bytes
/
encryption_key_type.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
package encryption
import (
"bytes"
"encoding/json"
)
type EncryptionKeyType int64
const (
ECDSA EncryptionKeyType = iota
HMAC
RSA
)
func (j EncryptionKeyType) String() string {
return toEncryptionKeyTypeString[j]
}
func (j EncryptionKeyType) FromString(keyType string) EncryptionKeyType {
return toEncryptionKeyTypeID[keyType]
}
var toEncryptionKeyTypeString = map[EncryptionKeyType]string{
RSA: "RSA",
ECDSA: "ECDSA",
HMAC: "HMAC",
}
var toEncryptionKeyTypeID = map[string]EncryptionKeyType{
"RSA": RSA,
"ECDSA": ECDSA,
"HMAC": HMAC,
}
func (s EncryptionKeyType) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(toEncryptionKeyTypeString[s])
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
func (s *EncryptionKeyType) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
*s = toEncryptionKeyTypeID[j]
return nil
}