-
Notifications
You must be signed in to change notification settings - Fork 4
/
eth_test.go
350 lines (287 loc) · 9.01 KB
/
eth_test.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright (c) 2023 gpress Authors.
//
// This file is part of gpress.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"log"
"math/big"
"testing"
)
func TestEthSignature(t *testing.T) {
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)
}
message := "Some data"
msgHash := keccak256Hash([]byte(message))
r, s, err := ecdsa.Sign(rand.Reader, privateKey, msgHash)
if err != nil {
panic(err)
}
v := big.NewInt(0)
v.Add(v, big.NewInt(27))
fmt.Println(fmt.Sprintf("r %x", r.Bytes()))
fmt.Println(fmt.Sprintf("s %x", s.Bytes()))
fmt.Println(fmt.Sprintf("v %x", v.Bytes()))
sig, err := hex.DecodeString(fmt.Sprintf("%x%x%x", v.Bytes(), r.Bytes(), s.Bytes()))
if err != nil {
panic(err)
}
prefix := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(message), message)
messageBytes := []byte(prefix)
messageHash := keccak256Hash(messageBytes)
hash, err := hex.DecodeString(fmt.Sprintf("%x", messageHash))
if err != nil {
panic(err)
}
fmt.Println("sig", fmt.Sprintf("%x", sig), len(sig))
fmt.Println("hash", fmt.Sprintf("%x", hash), len(hash))
}
func TestEth(t *testing.T) {
// 生成新的以太坊密钥对
privateKey, err := generatePrivateKey()
if err != nil {
log.Fatal("生成以太坊私钥失败:", err)
}
privateKeyHex := hex.EncodeToString(privateKey.D.Bytes())
fmt.Println("以太坊私钥:", privateKeyHex)
publicKey := privateKey.Public().(*ecdsa.PublicKey)
/*
publicKeyHex := "0434d53f7efc6d3c63d0a3eb010c6bea6b153dca5f5a0a1e747a3d5a64a62959494af78b111d5025b866d3c90e2a100c7e77a7cc6ed0a3e15c7a9e65c3f7b5f0a3"
privateKeyHex := "f75c9ff1b8c2e0c1f78a39bc67a92dbb26cc727bac9e0a9f4c9c2d0a337d0ef5"
// 解析公钥
publicKey, err := parsePublicKey(publicKeyHex)
if err != nil {
log.Fatal("解析公钥失败:", err)
}
// 解析私钥
privateKey, err := parsePrivateKey(privateKeyHex)
if err != nil {
log.Fatal("解析私钥失败:", err)
}
*/
// 生成以太坊地址
address := generateAddress(publicKey)
fmt.Println("以太坊地址:", address)
// 要加密的数据
plaintext := []byte("Hello, World!")
// 使用以太坊公钥加密数据
ciphertext, err := encryptWithPublicKey(publicKey, plaintext)
if err != nil {
log.Fatal("加密失败:", err)
}
fmt.Println("加密后的数据:", hex.EncodeToString(ciphertext))
// 使用以太坊私钥解密数据
decrypted, err := decryptWithPrivateKey(privateKey, ciphertext)
if err != nil {
log.Fatal("解密失败:", err)
}
fmt.Println("解密后的数据:", string(decrypted))
}
// 生成以太坊私钥
func generatePrivateKey() (*ecdsa.PrivateKey, error) {
return ecdsa.GenerateKey(secp256k1(), rand.Reader)
}
// 使用以太坊公钥加密数据
func encryptWithPublicKey(publicKey *ecdsa.PublicKey, plaintext []byte) ([]byte, error) {
// 生成临时私钥
tempPrivateKey, err := generatePrivateKey()
if err != nil {
return nil, err
}
// 计算临时公钥的坐标
tempPublicKeyX, tempPublicKeyY := secp256k1().ScalarBaseMult(tempPrivateKey.D.Bytes())
// 计算共享密钥的坐标
sharedKeyX, _ := secp256k1().ScalarMult(publicKey.X, publicKey.Y, tempPrivateKey.D.Bytes())
// 使用共享密钥的坐标作为初始化向量
iv := sharedKeyX.Bytes()
// 加密数据
ciphertext := xor(plaintext, iv)
// 组合加密后的临时公钥和密文
encryptedData := append(tempPublicKeyX.Bytes(), tempPublicKeyY.Bytes()...)
encryptedData = append(encryptedData, ciphertext...)
return encryptedData, nil
}
// 使用以太坊私钥解密数据
func decryptWithPrivateKey(privateKey *ecdsa.PrivateKey, ciphertext []byte) ([]byte, error) {
// 解析临时公钥的坐标
tempPublicKeyX := new(big.Int).SetBytes(ciphertext[:32])
tempPublicKeyY := new(big.Int).SetBytes(ciphertext[32:64])
// 计算共享密钥的坐标
sharedKeyX, _ := secp256k1().ScalarMult(tempPublicKeyX, tempPublicKeyY, privateKey.D.Bytes())
// 使用共享密钥的坐标作为初始化向量
iv := sharedKeyX.Bytes()
// 解密数据
plaintext := xor(ciphertext[64:], iv)
return plaintext, nil
}
// 执行异或操作
func xor(a, b []byte) []byte {
result := make([]byte, len(a))
for i := range a {
result[i] = a[i] ^ b[i]
}
return result
}
// 返回 secp256k1 椭圆曲线
func secp256k1() elliptic.Curve {
return elliptic.P256()
}
// 解析公钥
func parsePublicKey(publicKeyHex string) (*ecdsa.PublicKey, error) {
publicKeyBytes, err := hex.DecodeString(publicKeyHex)
if err != nil {
return nil, err
}
// 由于以太坊的公钥前面有一个字节的标志位,需要去掉
publicKey := &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: new(big.Int).SetBytes(publicKeyBytes[1:33]),
Y: new(big.Int).SetBytes(publicKeyBytes[33:65]),
}
return publicKey, nil
}
// 解析私钥
func parsePrivateKey(privateKeyHex string) (*ecdsa.PrivateKey, error) {
privateKeyBytes, err := hex.DecodeString(privateKeyHex)
if err != nil {
return nil, err
}
privateKey := &ecdsa.PrivateKey{
PublicKey: ecdsa.PublicKey{
Curve: elliptic.P256(),
},
D: new(big.Int).SetBytes(privateKeyBytes),
}
privateKey.PublicKey.X, privateKey.PublicKey.Y = privateKey.PublicKey.Curve.ScalarBaseMult(privateKey.D.Bytes())
return privateKey, nil
}
// 生成以太坊地址
func generateAddress(publicKey *ecdsa.PublicKey) string {
publicKeyBytes := elliptic.Marshal(publicKey.Curve, publicKey.X, publicKey.Y)
hash := sha256.Sum256(publicKeyBytes)
address := hash[12:]
return fmt.Sprintf("0x%x", address)
}
func TestEth3(t *testing.T) {
// MetaMask 签名数据
signature := "0x4acafcdd5ee478e14453a36c074dee7d142dca7ead7a2029a0c6b7a3e547ee46379018cd8fc661450c84fd90bcca34e2e0008a02b27eab7944a97947c0d8bfa71b"
// 消息字符串
message := "20230522151922392009508861"
// 发送者地址
senderAddress := "0xD530eC9517C20DE518345A7210338dFB6279f454"
verify, err := verifySecp256k1Signature(senderAddress, message, signature)
fmt.Println(verify)
fmt.Println(err)
}
func TestECDH(t *testing.T) {
// 生成ECDH私钥
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
fmt.Println("私钥生成失败:", err)
return
}
// 生成对应的ECDH公钥
publicKey := privateKey.PublicKey
// 生成对方的ECDH公钥
otherPublicKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
fmt.Println("对方公钥生成失败:", err)
return
}
// ECDH密钥交换
x, _ := publicKey.Curve.ScalarMult(otherPublicKey.X, otherPublicKey.Y, privateKey.D.Bytes())
// 计算共享密钥
sharedKey := sha256.Sum256(x.Bytes())
// 加密数据
plaintext := []byte("Hello, world!")
ciphertext, err := encryptAES(sharedKey[:], plaintext)
if err != nil {
fmt.Println("加密失败:", err)
return
}
// 解密数据
decryptedText, err := decryptAES(sharedKey[:], ciphertext)
if err != nil {
fmt.Println("解密失败:", err)
return
}
fmt.Printf("加密前的数据: %s\n", plaintext)
fmt.Printf("解密后的数据: %s\n", decryptedText)
}
func pad(in []byte) []byte {
padding := 16 - (len(in) % 16)
for i := 0; i < padding; i++ {
in = append(in, byte(padding))
}
return in
}
func unPad(in []byte) []byte {
if len(in) == 0 {
return nil
}
padding := in[len(in)-1]
if int(padding) > len(in) || padding > aes.BlockSize {
return nil
} else if padding == 0 {
return nil
}
for i := len(in) - 1; i > len(in)-int(padding)-1; i-- {
if in[i] != padding {
return nil
}
}
return in[:len(in)-int(padding)]
}
// 使用AES-CBC模式加密数据
func encryptAES(key, plaintext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
plaintext = pad(plaintext)
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
return ciphertext, nil
}
// 使用AES-CBC模式解密数据
func decryptAES(key, ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
plaintext := make([]byte, len(ciphertext))
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(plaintext, ciphertext)
plaintext = unPad(plaintext)
return plaintext, nil
}