forked from xuperchain/crypto
/
hash.go
47 lines (37 loc) · 888 Bytes
/
hash.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
package hash
import (
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"github.com/consensys/gnark-crypto/hash"
"golang.org/x/crypto/ripemd160"
)
func HashUsingSha256(data []byte) []byte {
h := sha256.New()
h.Write(data)
out := h.Sum(nil)
return out
}
// 执行2次SHA256,这是为了防止SHA256算法被攻破。
func DoubleSha256(data []byte) []byte {
return HashUsingSha256(HashUsingSha256(data))
}
// Ripemd160,这种hash算法可以缩短长度
func HashUsingRipemd160(data []byte) []byte {
h := ripemd160.New()
h.Write(data)
out := h.Sum(nil)
return out
}
func HashUsingHmac512(data, key []byte) []byte {
hmac512 := hmac.New(sha512.New, key)
hmac512.Write(data)
out := hmac512.Sum(nil)
return out
}
func HashUsingDefaultMiMC(data []byte) []byte {
hashFunc := hash.MIMC_BLS12_381
goMimc := hashFunc.New()
goMimc.Write(data)
return goMimc.Sum(nil)
}