-
Notifications
You must be signed in to change notification settings - Fork 2
/
cryptokey.go
68 lines (55 loc) · 1.21 KB
/
cryptokey.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
package models
import (
"crypto/x509"
"encoding/json"
"fmt"
)
type KeyStrength string
const (
KeyStrengthHigh KeyStrength = "HIGH"
KeyStrengthMedium KeyStrength = "MEDIUM"
KeyStrengthLow KeyStrength = "LOW"
)
type KeyType x509.PublicKeyAlgorithm
type KeyMetadata struct {
Type KeyType `json:"type"`
Bits int `json:"bits"`
}
type KeyStrengthMetadata struct {
Type KeyType `json:"type"`
Bits int `json:"bits"`
Strength KeyStrength `json:"strength"`
}
//---------------------------------------
func (kt KeyType) String() string {
publicKeyAlg := x509.PublicKeyAlgorithm(kt)
return publicKeyAlg.String()
}
func (kt KeyType) MarshalJSON() ([]byte, error) {
str := kt.String()
return json.Marshal(str)
}
func (kt *KeyType) UnmarshalJSON(data []byte) error {
var t string
err := json.Unmarshal(data, &t)
if err != nil {
return err
}
var nkt KeyType
switch string(t) {
case "UNKNOWN":
nkt = KeyType(x509.UnknownPublicKeyAlgorithm)
case "RSA":
nkt = KeyType(x509.RSA)
case "DSA":
nkt = KeyType(x509.DSA)
case "ECDSA":
nkt = KeyType(x509.ECDSA)
case "Ed25519":
nkt = KeyType(x509.Ed25519)
default:
return fmt.Errorf("unknown key type")
}
*kt = nkt
return nil
}