-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
44 lines (34 loc) · 827 Bytes
/
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
package entity
import (
"crypto"
"errors"
"google.golang.org/protobuf/proto"
)
type Key struct {
Id string
Type KeyType
Algorithm Algorithm
Raw crypto.PrivateKey
EncodeFunc KeyEncodeFunc
}
type KeyEncodeFunc func(crypto.PublicKey) (proto.Message, error)
type KeyType string
const (
RSA KeyType = "RSA"
ECDSA KeyType = "ECDSA"
HMAC KeyType = "HMAC"
)
type Algorithm string
const (
RS256 Algorithm = "RS256"
ES256 Algorithm = "ES256"
HS256 Algorithm = "HS256"
)
// Encode is a helper method used to invoke EncodeFunc safely.
func (key Key) Encode() (proto.Message, error) {
if key.EncodeFunc == nil {
// Return generic zero value and an error.
return nil, errors.New("encodeFunc is nil")
}
return key.EncodeFunc(key.Raw.(interface{ Public() crypto.PublicKey }).Public())
}