-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
91 lines (71 loc) · 1.7 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
// Copyright 2018 ProximaX Limited. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package crypto
import (
"crypto/sha256"
"crypto/subtle"
"golang.org/x/crypto/ripemd160"
"golang.org/x/crypto/sha3"
)
// HashesSha_256 return Sha 256 hash of byte
func HashesSha_256(b []byte) ([]byte, error) {
hash := sha256.New()
_, err := hash.Write(b)
if err != nil {
return nil, err
}
return hash.Sum(nil), nil
}
// HashesKeccak_256 return Keccak 256 hash of byte
func HashesKeccak_256(b []byte) ([]byte, error) {
hash := sha3.NewLegacyKeccak256()
_, err := hash.Write(b)
if err != nil {
return nil, err
}
return hash.Sum(nil), nil
}
// HashesSha3_256 return sha3 256 hash of byte
func HashesSha3_256(b []byte) ([]byte, error) {
hash := sha3.New256()
_, err := hash.Write(b)
if err != nil {
return nil, err
}
return hash.Sum(nil), nil
}
// HashesSha3_512 return sha3 512 hash of byte
func HashesSha3_512(inputs ...[]byte) ([]byte, error) {
hash := sha3.New512()
for _, b := range inputs {
_, err := hash.Write(b)
if err != nil {
return nil, err
}
}
return hash.Sum(nil), nil
}
// HashesRipemd160 return ripemd160 hash of byte
func HashesRipemd160(b []byte) ([]byte, error) {
hash := ripemd160.New()
_, err := hash.Write(b)
if err != nil {
return nil, err
}
return hash.Sum(nil), nil
}
func isNegativeConstantTime(b int) int {
return (b >> 8) & 1
}
func isConstantTimeByteEq(b, c int) int {
result := 0
xor := b ^ c // final
for i := uint(0); i < 8; i++ {
result |= xor >> i
}
return (result ^ 0x01) & 0x01
}
func isEqualConstantTime(x, y []byte) bool {
return subtle.ConstantTimeCompare(x, y) == 1
}