-
Notifications
You must be signed in to change notification settings - Fork 21
/
key_dsa.go
52 lines (41 loc) · 1.3 KB
/
key_dsa.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 pkcs12
import (
"errors"
"crypto"
"crypto/dsa"
cryptobin_dsa "github.com/deatil/go-cryptobin/dsa"
)
// DSA
type KeyDSA struct {}
// 包装
func (this KeyDSA) MarshalPrivateKey(privateKey crypto.PrivateKey) ([]byte, error) {
priKey, ok := privateKey.(*dsa.PrivateKey)
if !ok {
return nil, errors.New("pkcs12: private key is err")
}
pkData, err := cryptobin_dsa.MarshalPrivateKey(priKey)
if err != nil {
return nil, errors.New("pkcs12: error encoding PKCS#8 private key: " + err.Error())
}
return pkData, nil
}
// 包装
func (this KeyDSA) MarshalPKCS8PrivateKey(privateKey crypto.PrivateKey) ([]byte, error) {
priKey, ok := privateKey.(*dsa.PrivateKey)
if !ok {
return nil, errors.New("pkcs12: private key is err")
}
pkData, err := cryptobin_dsa.MarshalPKCS8PrivateKey(priKey)
if err != nil {
return nil, errors.New("pkcs12: error encoding PKCS#8 private key: " + err.Error())
}
return pkData, nil
}
// 解析
func (this KeyDSA) ParsePKCS8PrivateKey(pkData []byte) (crypto.PrivateKey, error) {
privateKey, err := cryptobin_dsa.ParsePKCS8PrivateKey(pkData)
if err != nil {
return nil, errors.New("pkcs12: error parsing PKCS#8 private key: " + err.Error())
}
return privateKey, nil
}