forked from nknorg/nkn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
55 lines (45 loc) · 955 Bytes
/
util.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
package util
import (
"crypto/elliptic"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
)
const (
HASHLEN = 32
PRIVATEKEYLEN = 32
PUBLICKEYLEN = 32
SIGNRLEN = 32
SIGNSLEN = 32
SIGNATURELEN = 64
NEGBIGNUMLEN = 33
)
const (
MaxRandomBytesLength = 2048
)
type CryptoAlgSet struct {
EccParams elliptic.CurveParams
Curve elliptic.Curve
}
func RandomBytes(length int) []byte {
if length <= 0 || length >= MaxRandomBytesLength {
return nil
}
random := make([]byte, length)
rand.Read(random)
return random
}
func Hash(data []byte) [HASHLEN]byte {
return sha256.Sum256(data)
}
// CheckMAC reports whether messageMAC is a valid HMAC tag for message.
func CheckMAC(message, messageMAC, key []byte) bool {
mac := hmac.New(sha256.New, key)
mac.Write(message)
expectedMAC := mac.Sum(nil)
return hmac.Equal(messageMAC, expectedMAC)
}
func RIPEMD160(value []byte) []byte {
//TODO: implement RIPEMD160
return nil
}