-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
36 lines (30 loc) · 857 Bytes
/
crypto.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
package crypto
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"github.com/msaldanha/realChain/util"
"math/big"
)
func Sign(data []byte, privateKey *ecdsa.PrivateKey) ([]byte, error) {
r, s, err := ecdsa.Sign(rand.Reader, privateKey, data)
if err != nil {
return nil, err
}
return append(util.LeftPadBytes(r.Bytes(), 32), util.LeftPadBytes(s.Bytes(), 32)...), nil
}
func VerifySignature(signature []byte, pubKey []byte, data []byte) bool {
r := big.Int{}
s := big.Int{}
sigLen := len(signature)
r.SetBytes(signature[:(sigLen / 2)])
s.SetBytes(signature[(sigLen / 2):])
x := big.Int{}
y := big.Int{}
keyLen := len(pubKey)
x.SetBytes(pubKey[:(keyLen / 2)])
y.SetBytes(pubKey[(keyLen / 2):])
curve := elliptic.P256()
rawPubKey := ecdsa.PublicKey{Curve: curve, X: &x, Y: &y}
return ecdsa.Verify(&rawPubKey, data, &r, &s)
}