-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
136 lines (118 loc) · 3.46 KB
/
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
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
package ptcpayclient
//***********************************************
//* Copyright (c) 2021 Ulbora Labs LLC
//* Copyright (c) 2021 Ken Williamson
//***********************************************
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"math/big"
"golang.org/x/crypto/ripemd160"
"github.com/btcsuite/btcutil/base58"
)
const (
pubkeyCompressed byte = 0x2
pubKeyBytesLenCompressed = 33
)
//Crypto Crypto
type Crypto interface {
GenerateKeyPair(ec elliptic.Curve) *ecdsa.PrivateKey
LoadKeyPair(privateKey string, ec elliptic.Curve) *ecdsa.PrivateKey
GetSinFromKey(kp *ecdsa.PrivateKey) string
Sign(hash []byte, kp *ecdsa.PrivateKey) ([]byte, error)
GetPublicKey(kp *ecdsa.PrivateKey) string
}
//Cryptography Cryptography
type Cryptography struct {
}
//New New
func (c *Cryptography) New() Crypto {
return c
}
//GenerateKeyPair GenerateKeyPair
func (c *Cryptography) GenerateKeyPair(ec elliptic.Curve) *ecdsa.PrivateKey {
//A public private key pair is created using elliptic curve secp256k1
kp, _ := ecdsa.GenerateKey(ec, rand.Reader)
return kp
}
//LoadKeyPair LoadKeyPair
func (c *Cryptography) LoadKeyPair(privateKey string, ec elliptic.Curve) *ecdsa.PrivateKey {
//A public private key pair is created using elliptic curve secp256k1
var e ecdsa.PrivateKey
e.D, _ = new(big.Int).SetString(privateKey, 16)
e.PublicKey.Curve = ec //secp256k1.S256()
e.PublicKey.X, e.PublicKey.Y = e.PublicKey.Curve.ScalarBaseMult(e.D.Bytes())
return &e
}
//GetSinFromKey GetSinFromKey
func (c *Cryptography) GetSinFromKey(kp *ecdsa.PrivateKey) string {
//A public private key pair is created using elliptic curve secp256k1
var sin string
pub := kp.PublicKey
comp := serializeCompressed(pub)
//fmt.Println("byta: ", comp)
key := hex.EncodeToString(comp)
hexa := sha256ofHexString(key)
hexa = ripemd160ofHexString(hexa)
versionSinTypeEtc := "0F02" + hexa
hexa = sha256ofHexString(versionSinTypeEtc)
hexa = sha256ofHexString(hexa)
checksum := hexa[0:8]
hexa = versionSinTypeEtc + checksum
byta, _ := hex.DecodeString(hexa)
sin = base58.Encode(byta)
//fmt.Println("sin: ", sin)
return sin
}
//Sign Sign
func (c *Cryptography) Sign(hash []byte, kp *ecdsa.PrivateKey) ([]byte, error) {
//A public private key pair is created using elliptic curve secp256k1
ehash := sha256.Sum256(hash)
sig, err := ecdsa.SignASN1(rand.Reader, kp, ehash[:])
return sig, err
}
//GetPublicKey GetPublicKey
func (c *Cryptography) GetPublicKey(kp *ecdsa.PrivateKey) string {
comp := serializeCompressed(kp.PublicKey)
key := hex.EncodeToString(comp)
return key
}
func sha256ofHexString(hexa string) string {
//fmt.Println("hexa: ", hexa)
byta, _ := hex.DecodeString(hexa)
hash := sha256.New()
hash.Write(byta)
hashsum := hash.Sum(nil)
hexb := hex.EncodeToString(hashsum)
//fmt.Println("hexb: ", hexb)
return hexb
}
func serializeCompressed(p ecdsa.PublicKey) []byte {
b := make([]byte, 0, pubKeyBytesLenCompressed)
format := pubkeyCompressed
if isOdd(p.Y) {
format |= 0x1
}
b = append(b, format)
return paddedAppend(32, b, p.X.Bytes())
}
func isOdd(a *big.Int) bool {
return a.Bit(0) == 1
}
func ripemd160ofHexString(hexa string) string {
byta, _ := hex.DecodeString(hexa)
hash := ripemd160.New()
hash.Write(byta)
hashsum := hash.Sum(nil)
hexb := hex.EncodeToString(hashsum)
return hexb
}
func paddedAppend(size uint, dst, src []byte) []byte {
for i := 0; i < int(size)-len(src); i++ {
dst = append(dst, 0)
}
return append(dst, src...)
}