-
Notifications
You must be signed in to change notification settings - Fork 22
/
sign.go
89 lines (67 loc) · 1.86 KB
/
sign.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
package eddsa
import (
"errors"
"crypto"
"crypto/rand"
"crypto/ed25519"
)
// 私钥签名
func (this EdDSA) Sign() EdDSA {
if this.privateKey == nil {
err := errors.New("EdDSA: privateKey error.")
return this.AppendError(err)
}
var key any
key = this.privateKey
var ed25519Key crypto.Signer
var ok bool
if ed25519Key, ok = key.(crypto.Signer); !ok {
err := errors.New("EdDSA: privateKey type error.")
return this.AppendError(err)
}
if _, ok := ed25519Key.Public().(ed25519.PublicKey); !ok {
err := errors.New("EdDSA: privateKey error.")
return this.AppendError(err)
}
// 判断是否需要做 hash
message := dataHash(this.data, this.options)
sig, err := ed25519Key.Sign(rand.Reader, message, this.options)
if err != nil {
return this.AppendError(err)
}
this.parsedData = []byte(sig)
return this
}
// 公钥验证
func (this EdDSA) Verify(data []byte) EdDSA {
if this.publicKey == nil {
err := errors.New("EdDSA: publicKey error.")
return this.AppendError(err)
}
var key any
key = this.publicKey
var ed25519Key ed25519.PublicKey
var ok bool
if ed25519Key, ok = key.(ed25519.PublicKey); !ok {
err := errors.New("EdDSA: publicKey type error.")
return this.AppendError(err)
}
// 判断是否需要做 hash
message := dataHash(data, this.options)
err := ed25519.VerifyWithOptions(ed25519Key, message, this.data, this.options)
if err != nil {
return this.AppendError(err)
}
this.verify = true
return this
}
// 判断是否需要做 hash
func dataHash(data []byte, opts *Options) []byte {
hash := opts.HashFunc()
if hash == crypto.SHA512 {
h := hash.New()
h.Write(data)
return h.Sum(nil)
}
return data
}