-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature_verify.go
58 lines (45 loc) · 1.57 KB
/
signature_verify.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
package main
import (
"bytes"
"crypto/ecdsa"
"fmt"
"log"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
func main() {
privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
if err != nil {
log.Fatalf("try load private key: %s", err.Error())
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatalf("error casting public key to ECDSA: ", err.Error())
}
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
data := []byte("hello boy")
hash := crypto.Keccak256Hash(data)
fmt.Printf("Hash: %s\n", hash.Hex())
signature, err := crypto.Sign(hash.Bytes(), privateKey)
if err != nil {
log.Fatalf("try sign the data: %s", err.Error())
}
fmt.Println("Signature: ", hexutil.Encode(signature))
sigPublicKey, err := crypto.Ecrecover(hash.Bytes(), signature)
if err != nil {
log.Fatalf("try recover the public key signer error: %s", err.Error())
}
matches := bytes.Equal(sigPublicKey, publicKeyBytes)
fmt.Println("Are equals: ", matches)
sigPublicKeyECDSA, err := crypto.SigToPub(hash.Bytes(), signature)
if err != nil {
log.Fatalf("try get signature public key: %s", err.Error())
}
sigPublicKeyBytes := crypto.FromECDSAPub(sigPublicKeyECDSA)
matches = bytes.Equal(sigPublicKeyBytes, publicKeyBytes)
fmt.Println("Are equals: ", matches)
signatureNoRecoverID := signature[:len(signature)-1]
verified := crypto.VerifySignature(publicKeyBytes, hash.Bytes(), signatureNoRecoverID)
fmt.Println("Check Signature: ", verified)
}