-
Notifications
You must be signed in to change notification settings - Fork 1
/
key.go
176 lines (135 loc) · 3.07 KB
/
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package alsdk
import (
"crypto/ecdsa"
"crypto/rsa"
"errors"
"github.com/activeledger/SDK-Golang/v2/internal/key"
)
type KeyHandler interface {
Sign(Data []byte) (Signature []byte, Hash []byte, SignError error)
Verify(Signature []byte, Checksum []byte) (Ok bool, VerifError error)
GetType() KeyType
GetRSAKey() (Key *rsa.PrivateKey)
GetEllipticKey() (Key *ecdsa.PrivateKey)
GetPrivatePEM() (KeyPublicPem string)
GetPublicPEM() (KeyPrivatePem string)
}
type Key struct {
RSA key.RSAHandler
Elliptic key.EllipticHandler
keyType KeyType
}
type KeyType string
const (
RSA KeyType = "rsa"
Elliptic KeyType = "elliptic"
)
var (
ErrNotImplemented = errors.New("not implemented")
ErrUnknownKey = errors.New("unknown key type")
)
// Generate a new RSA key
func GenerateRSA() (KeyHandler, error) {
rsaKey, err := key.GenerateRSA()
if err != nil {
return Key{}, err
}
k := Key{
RSA: rsaKey,
keyType: RSA,
}
return k, nil
}
// Generate new Elliptic Curve Key
func GenerateElliptic() (KeyHandler, error) {
eccKey, err := key.GenerateECC()
if err != nil {
return Key{}, err
}
k := Key{
Elliptic: eccKey,
keyType: Elliptic,
}
return k, nil
}
// VerifyUsingPem - Verify a given signature using a given public key PEM
func VerifyUsingPem(sig []byte, checksum []byte, pubPem string, keyType KeyType) (bool, error) {
switch keyType {
case RSA:
return key.VerifyUsingRSAPem(sig, checksum, pubPem)
case Elliptic:
return key.VerifyUsingECCPem(sig, checksum, pubPem)
default:
return false, ErrUnknownKey
}
}
// Sign a byte slice, returns the signature and checksum hash
func (k Key) Sign(data []byte) (Signature []byte, Hash []byte, SignError error) {
switch k.keyType {
case RSA:
return k.RSA.Sign(data)
case Elliptic:
return k.Elliptic.Sign(data)
default:
return []byte{}, []byte{}, ErrUnknownKey
}
}
// Verify a signature with a given checksum
func (k Key) Verify(signature []byte, checksum []byte) (bool, error) {
switch k.keyType {
case RSA:
return k.RSA.Verify(signature, checksum)
case Elliptic:
return k.Elliptic.Verify(signature, checksum), nil
default:
return false, ErrUnknownKey
}
}
func (k Key) GetType() KeyType {
return k.keyType
}
func (k Key) GetRSAKey() *rsa.PrivateKey {
return k.RSA.GetKey()
}
func (k Key) GetEllipticKey() *ecdsa.PrivateKey {
return k.Elliptic.GetKey()
}
func (k Key) GetPrivatePEM() string {
var pem string
if k.keyType == RSA {
pem = k.RSA.GetPrivatePEM()
} else {
return k.Elliptic.GetPrivatePEM()
}
return pem
}
func (k Key) GetPublicPEM() string {
if k.keyType == RSA {
return k.RSA.GetPublicPEM()
} else {
return k.Elliptic.GetPublicPEM()
}
}
func SetKey(pem string, keyType KeyType) (KeyHandler, error) {
k := Key{}
switch keyType {
case RSA:
key, err := key.SetRSAKey(pem)
if err != nil {
return Key{}, err
}
k.RSA = key
k.keyType = RSA
return k, nil
case Elliptic:
key, err := key.SetECCKey(pem)
if err != nil {
return Key{}, err
}
k.Elliptic = key
k.keyType = Elliptic
return k, nil
default:
return Key{}, ErrUnknownKey
}
}