-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecdsa_encrypt.go
142 lines (109 loc) · 3.04 KB
/
ecdsa_encrypt.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
package criptus
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"log"
"math/big"
)
type EllipticCurve struct {
pubKeyCurve elliptic.Curve
privateKey *ecdsa.PrivateKey
publicKey *ecdsa.PublicKey
}
func NewECDSA(opts ...ECDSAOptions) (*EllipticCurve, error) {
params, err := newECDSAParams(opts...)
if err != nil {
return nil, err
}
if params.GetCurve() == nil {
params.SetCurve(EllipticCurveP256)
}
return &EllipticCurve{
pubKeyCurve: params.GetCurve(),
privateKey: new(ecdsa.PrivateKey),
}, nil
}
func (ec *EllipticCurve) GenerateKeys() (*ecdsa.PrivateKey, *ecdsa.PublicKey, error) {
privKey, err := ecdsa.GenerateKey(ec.pubKeyCurve, rand.Reader)
if err != nil {
return nil, nil, err
}
ec.privateKey = privKey
ec.publicKey = &privKey.PublicKey
return privKey, &privKey.PublicKey, nil
}
func (ec *EllipticCurve) EncodePrivate(privKey *ecdsa.PrivateKey) (string, error) {
encoded, err := x509.MarshalECPrivateKey(privKey)
if err != nil {
return "", err
}
pemEncoded := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: encoded})
return string(pemEncoded), nil
}
func (ec *EllipticCurve) EncodePublic(pubKey *ecdsa.PublicKey) (string, error) {
encoded, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
return "", err
}
pemEncodedPub := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: encoded})
return string(pemEncodedPub), nil
}
func (ec *EllipticCurve) DecodePrivate(pemEncodedPriv string) (*ecdsa.PrivateKey, error) {
blockPriv, _ := pem.Decode([]byte(pemEncodedPriv))
x509EncodedPriv := blockPriv.Bytes
privateKey, err := x509.ParseECPrivateKey(x509EncodedPriv)
if err != nil {
return nil, err
}
return privateKey, nil
}
func (ec *EllipticCurve) DecodePublic(pemEncodedPub string) (*ecdsa.PublicKey, error) {
blockPub, _ := pem.Decode([]byte(pemEncodedPub))
x509EncodedPub := blockPub.Bytes
genericPublicKey, err := x509.ParsePKIXPublicKey(x509EncodedPub)
if err != nil {
return nil, err
}
publicKey := genericPublicKey.(*ecdsa.PublicKey)
return publicKey, nil
}
func (ec *EllipticCurve) Sign(message string, privKey *ecdsa.PrivateKey) ([]byte, error) {
hasher := sha256.New()
_, err := hasher.Write([]byte(message))
if err != nil {
return nil, err
}
hash := hasher.Sum(nil)
// Sign the hash
r, s, err := ecdsa.Sign(rand.Reader, privKey, hash)
if err != nil {
return nil, err
}
// Convert r and s values to bytes
rBytes := r.Bytes()
sBytes := s.Bytes()
// Concatenate r and s
signature := append(rBytes, sBytes...)
return signature, nil
}
func (ec *EllipticCurve) Verify(message string, signature []byte, pubKey *ecdsa.PublicKey) bool {
if pubKey == nil {
log.Printf("【WARN】] Public key is nil")
return false
}
h := sha256.New()
_, err := h.Write([]byte(message))
if err != nil {
return false
}
hash := h.Sum(nil)
sigLen := len(signature)
r, s := big.NewInt(0), big.NewInt(0)
r.SetBytes(signature[:sigLen/2])
s.SetBytes(signature[sigLen/2:])
return ecdsa.Verify(pubKey, hash, r, s)
}