-
Notifications
You must be signed in to change notification settings - Fork 6
/
base_crypto_key.go
60 lines (50 loc) · 1.2 KB
/
base_crypto_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
package framework
/**
* @Author: imuge
* @Date: 2020/4/29 4:39 下午
*/
var (
KEY_TYPE_BYTES = 1
)
var _ CryptoKey = (*BaseCryptoKey)(nil)
type BaseCryptoKey struct {
BaseCryptoBytes
keyType CryptoKeyType
}
func NewBaseCryptoKey(algorithm CryptoAlgorithm, rawKeyBytes []byte, keyType CryptoKeyType) BaseCryptoKey {
return BaseCryptoKey{
NewBaseCryptoBytes(algorithm, EncodeKeyBytes(rawKeyBytes, keyType)),
keyType,
}
}
func ParseBaseCryptoKey(cryptoBytes []byte) (*BaseCryptoKey, error) {
bcbs, err := ParseBaseCryptoBytes(cryptoBytes, supportAsymmetricOrSymmetric)
if err != nil {
return nil, err
}
bytes, err := bcbs.GetRawCryptoBytes()
if err != nil {
return nil, err
}
keyType, err := DecodeKeyType(*bytes)
if err != nil {
return nil, err
}
return &BaseCryptoKey{
*bcbs,
keyType,
}, nil
}
func (b BaseCryptoKey) GetKeyType() CryptoKeyType {
return b.keyType
}
func (b BaseCryptoKey) GetRawKeyBytes() ([]byte, error) {
slice, err := b.GetRawCryptoBytes()
if err != nil {
return nil, err
}
return slice.GetBytesCopy(1, slice.Size-1)
}
func supportAsymmetricOrSymmetric(algorithm CryptoAlgorithm) bool {
return algorithm.HasAsymmetricKey() || algorithm.HasSymmetricKey()
}