/
signer_ecdsa.go
69 lines (53 loc) · 1.51 KB
/
signer_ecdsa.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
package pkcs7
import (
"errors"
"crypto"
"crypto/rand"
"crypto/ecdsa"
"encoding/asn1"
)
// ecdsa 签名
type KeySignWithECDSA struct {
hashFunc crypto.Hash
hashId asn1.ObjectIdentifier
identifier asn1.ObjectIdentifier
}
// oid
func (this KeySignWithECDSA) HashOID() asn1.ObjectIdentifier {
return this.hashId
}
// oid
func (this KeySignWithECDSA) OID() asn1.ObjectIdentifier {
return this.identifier
}
// 签名
func (this KeySignWithECDSA) Sign(pkey crypto.PrivateKey, data []byte) ([]byte, []byte, error) {
var priv *ecdsa.PrivateKey
var ok bool
if priv, ok = pkey.(*ecdsa.PrivateKey); !ok {
return nil, nil, errors.New("pkcs7: PrivateKey is not ecdsa PrivateKey")
}
hashData := hashSignData(this.hashFunc, data)
signData, err := ecdsa.SignASN1(rand.Reader, priv, hashData)
return hashData, signData, err
}
// 验证
func (this KeySignWithECDSA) Verify(pkey crypto.PublicKey, signed []byte, signature []byte) (bool, error) {
var pub *ecdsa.PublicKey
var ok bool
if pub, ok = pkey.(*ecdsa.PublicKey); !ok {
return false, errors.New("pkcs7: PublicKey is not ecdsa PublicKey")
}
hashData := hashSignData(this.hashFunc, signed)
return ecdsa.VerifyASN1(pub, hashData, signature), nil
}
// 检测证书
func (this KeySignWithECDSA) Check(pkey any) bool {
if _, ok := pkey.(*ecdsa.PrivateKey); ok {
return true
}
if _, ok := pkey.(*ecdsa.PublicKey); ok {
return true
}
return false
}