-
Notifications
You must be signed in to change notification settings - Fork 244
/
crypto.go
133 lines (107 loc) · 2.89 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
package common
import (
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/rand"
"errors"
"io"
"math/big"
"golang.org/x/crypto/sha3"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
)
const (
nonceLength = 12
defaultECHDSharedKeyLength = 16
defaultECHDMACLength = 16
)
var (
ErrInvalidCiphertextLength = errors.New("invalid cyphertext length")
letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
numberRunes = []rune("0123456789")
alphanumericRunes = append(numberRunes, letterRunes...)
)
func HashPublicKey(pk *ecdsa.PublicKey) []byte {
return Shake256(crypto.CompressPubkey(pk))
}
func Decrypt(cyphertext []byte, key []byte) ([]byte, error) {
if len(cyphertext) < nonceLength {
return nil, ErrInvalidCiphertextLength
}
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonce := cyphertext[:nonceLength]
return gcm.Open(nil, nonce, cyphertext[nonceLength:], nil)
}
func Encrypt(plaintext []byte, key []byte, reader io.Reader) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(reader, nonce); err != nil {
return nil, err
}
return gcm.Seal(nonce, nonce, plaintext, nil), nil
}
func Shake256(buf []byte) []byte {
h := make([]byte, 64)
sha3.ShakeSum256(h, buf)
return h
}
// IsPubKeyEqual checks that two public keys are equal
func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool {
// the curve is always the same, just compare the points
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
}
func PubkeyToHex(key *ecdsa.PublicKey) string {
return types.EncodeHex(crypto.FromECDSAPub(key))
}
func PubkeyToHexBytes(key *ecdsa.PublicKey) types.HexBytes {
return crypto.FromECDSAPub(key)
}
func HexToPubkey(pk string) (*ecdsa.PublicKey, error) {
bytes, err := types.DecodeHex(pk)
if err != nil {
return nil, err
}
return crypto.UnmarshalPubkey(bytes)
}
func MakeECDHSharedKey(yourPrivateKey *ecdsa.PrivateKey, theirPubKey *ecdsa.PublicKey) ([]byte, error) {
return ecies.ImportECDSA(yourPrivateKey).GenerateShared(
ecies.ImportECDSAPublic(theirPubKey),
defaultECHDSharedKeyLength,
defaultECHDMACLength,
)
}
func randomString(choice []rune, n int) (string, error) {
max := big.NewInt(int64(len(choice)))
rr := rand.Reader
b := make([]rune, n)
for i := range b {
pos, err := rand.Int(rr, max)
if err != nil {
return "", err
}
b[i] = choice[pos.Int64()]
}
return string(b), nil
}
func RandomAlphabeticalString(n int) (string, error) {
return randomString(letterRunes, n)
}
func RandomAlphanumericString(n int) (string, error) {
return randomString(alphanumericRunes, n)
}