-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sign.go
233 lines (191 loc) · 7.39 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package crypto
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"io"
"math/big"
"strings"
"github.com/Laisky/errors/v2"
)
// // EncodeRSAPrivateKey encode rsa private key to pem bytes
// func EncodeRSAPrivateKey(privateKey *rsa.PrivateKey) ([]byte, error) {
// x509Encoded := x509.MarshalPKCS1PrivateKey(privateKey)
// return pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509Encoded}), nil
// }
// // EncodeRSAPublicKey encode rsa public key to pem bytes
// func EncodeRSAPublicKey(publicKey *rsa.PublicKey) ([]byte, error) {
// x509EncodedPub := x509.MarshalPKCS1PublicKey(publicKey)
// return pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: x509EncodedPub}), nil
// }
// // DecodeRSAPrivateKey decode rsa private key from pem bytes
// func DecodeRSAPrivateKey(pemEncoded []byte) (*rsa.PrivateKey, error) {
// block, _ := pem.Decode(pemEncoded)
// privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
// if err != nil {
// return nil, errors.Wrap(err, "parse rsa private key")
// }
// return privateKey, nil
// }
// // DecodeRSAPublicKey decode rsa public key from pem bytes
// func DecodeRSAPublicKey(pemEncodedPub []byte) (*rsa.PublicKey, error) {
// blockPub, _ := pem.Decode(pemEncodedPub)
// pubkey, err := x509.ParsePKCS1PublicKey(blockPub.Bytes)
// if err != nil {
// return nil, errors.Wrap(err, "parse rsa public key")
// }
// return pubkey, nil
// }
// SignByRSAWithSHA256 generate signature by rsa private key use sha256
func SignByRSAWithSHA256(prikey *rsa.PrivateKey, content []byte) ([]byte, error) {
hashed := sha256.Sum256(content)
return rsa.SignPKCS1v15(rand.Reader, prikey, crypto.SHA256, hashed[:])
}
// VerifyByRSAWithSHA256 verify signature by rsa public key use sha256
func VerifyByRSAWithSHA256(pubKey *rsa.PublicKey, content []byte, sig []byte) error {
hash := sha256.Sum256(content)
return rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, hash[:], sig)
}
// SignReaderByRSAWithSHA256 generate signature by rsa private key use sha256
func SignReaderByRSAWithSHA256(prikey *rsa.PrivateKey, reader io.Reader) (sig []byte, err error) {
hasher := sha256.New()
if _, err = io.Copy(hasher, reader); err != nil {
return nil, errors.Wrap(err, "read content")
}
return rsa.SignPKCS1v15(rand.Reader, prikey, crypto.SHA256, hasher.Sum(nil))
}
// VerifyReaderByRSAWithSHA256 verify signature by rsa public key use sha256
func VerifyReaderByRSAWithSHA256(pubKey *rsa.PublicKey, reader io.Reader, sig []byte) error {
hasher := sha256.New()
if _, err := io.Copy(hasher, reader); err != nil {
return errors.Wrap(err, "read content")
}
return rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, hasher.Sum(nil), sig)
}
// SignByECDSAWithSHA256 generate signature by ecdsa private key use sha256
func SignByECDSAWithSHA256(prikey *ecdsa.PrivateKey, content []byte) (r, s *big.Int, err error) {
hash := sha256.Sum256(content)
return ecdsa.Sign(rand.Reader, prikey, hash[:])
}
// VerifyByECDSAWithSHA256 verify signature by ecdsa public key use sha256
func VerifyByECDSAWithSHA256(pubKey *ecdsa.PublicKey, content []byte, r, s *big.Int) bool {
hash := sha256.Sum256(content)
return ecdsa.Verify(pubKey, hash[:], r, s)
}
// SignByECDSAWithSHA256AndBase64 generate signature by ecdsa private key use sha256
func SignByECDSAWithSHA256AndBase64(prikey *ecdsa.PrivateKey, content []byte) (signature string, err error) {
hash := sha256.Sum256(content)
r, s, err := ecdsa.Sign(rand.Reader, prikey, hash[:])
if err != nil {
return "", errors.Wrap(err, "sign")
}
return EncodeES256SignByBase64(r, s), nil
}
// VerifyByECDSAWithSHA256 verify signature by ecdsa public key use sha256
func VerifyByECDSAWithSHA256AndBase64(pubKey *ecdsa.PublicKey, content []byte, signature string) (bool, error) {
hash := sha256.Sum256(content)
r, s, err := DecodeES256SignByBase64(signature)
if err != nil {
return false, errors.Wrap(err, "decode signature")
}
return ecdsa.Verify(pubKey, hash[:], r, s), nil
}
// SignReaderByECDSAWithSHA256 generate signature by ecdsa private key use sha256
func SignReaderByECDSAWithSHA256(prikey *ecdsa.PrivateKey, reader io.Reader) (r, s *big.Int, err error) {
hasher := sha256.New()
if _, err = io.Copy(hasher, reader); err != nil {
return nil, nil, errors.Wrap(err, "read content")
}
return ecdsa.Sign(rand.Reader, prikey, hasher.Sum(nil))
}
// VerifyReaderByECDSAWithSHA256 verify signature by ecdsa public key use sha256
func VerifyReaderByECDSAWithSHA256(pubKey *ecdsa.PublicKey, reader io.Reader, r, s *big.Int) (bool, error) {
hasher := sha256.New()
if _, err := io.Copy(hasher, reader); err != nil {
return false, errors.Wrap(err, "read content")
}
return ecdsa.Verify(pubKey, hasher.Sum(nil), r, s), nil
}
const (
streamChunkSize = 4 * 1024 * 1024
)
// SignReaderByEd25519WithSHA256 generate signature by ecdsa private key use sha256
func SignReaderByEd25519WithSHA256(prikey ed25519.PrivateKey, reader io.Reader) (sig []byte, err error) {
hasher := sha256.New()
chunk := make([]byte, streamChunkSize)
for {
n, err := reader.Read(chunk)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, errors.Wrap(err, "read chunk")
}
if _, err = hasher.Write(chunk[:n]); err != nil {
return nil, errors.Wrap(err, "write chunk")
}
}
return prikey.Sign(rand.Reader, hasher.Sum(nil), crypto.Hash(0))
}
// VerifyReaderByEd25519WithSHA256 verify signature by ecdsa public key use sha256
func VerifyReaderByEd25519WithSHA256(pubKey ed25519.PublicKey, reader io.Reader, sig []byte) error {
hasher := sha256.New()
chunk := make([]byte, streamChunkSize)
for {
n, err := reader.Read(chunk)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return errors.Wrap(err, "read chunk")
}
if _, err = hasher.Write(chunk[:n]); err != nil {
return errors.Wrap(err, "write chunk")
}
}
err := ed25519.VerifyWithOptions(pubKey, hasher.Sum(nil), sig, &ed25519.Options{
Hash: crypto.Hash(0),
})
return errors.Wrap(err, "verify")
}
const ecdsaSignDelimiter = "."
// EncodeES256SignByHex format ecdsa sign to stirng
func EncodeES256SignByHex(r, s *big.Int) string {
return FormatBig2Hex(r) + ecdsaSignDelimiter + FormatBig2Hex(s)
}
// DecodeES256SignByHex parse ecdsa signature string to two *big.Int
func DecodeES256SignByHex(sign string) (r, s *big.Int, err error) {
ss := strings.Split(sign, ecdsaSignDelimiter)
if len(ss) != 2 {
return nil, nil, errors.Errorf("unknown format of signature `%s`, want `xxx.xxx`", sign)
}
var ok bool
if r, ok = ParseHex2Big(ss[0]); !ok {
return nil, nil, errors.Errorf("invalidate hex `%s`", ss[0])
}
if s, ok = ParseHex2Big(ss[1]); !ok {
return nil, nil, errors.Errorf("invalidate hex `%s`", ss[1])
}
return
}
// EncodeES256SignByBase64 format ecdsa signature to stirng
func EncodeES256SignByBase64(r, s *big.Int) string {
return FormatBig2Base64(r) + ecdsaSignDelimiter + FormatBig2Base64(s)
}
// DecodeES256SignByBase64 parse ecdsa signature string to two *big.Int
func DecodeES256SignByBase64(sign string) (r, s *big.Int, err error) {
ss := strings.Split(sign, ecdsaSignDelimiter)
if len(ss) != 2 {
return nil, nil, errors.Wrapf(err, "unknown format of signature `%s`, expect is `xxxx.xxxx`", sign)
}
if r, err = ParseBase642Big(ss[0]); err != nil {
return nil, nil, errors.Wrapf(err, "`%s` is not validate base64", ss[0])
}
if s, err = ParseBase642Big(ss[1]); err != nil {
return nil, nil, errors.Wrapf(err, "`%s` is not validate base64", ss[1])
}
return
}