-
Notifications
You must be signed in to change notification settings - Fork 26
/
hash.go
78 lines (69 loc) · 1.59 KB
/
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
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
package encryption
import (
"crypto/sha1"
"encoding/hex"
"github.com/minio/sha256-simd"
"golang.org/x/crypto/sha3"
)
const HASH_LENGTH = 32
type HashBytes [HASH_LENGTH]byte
/*Hash - hash the given data and return the hash as hex string */
func Hash(data interface{}) string {
return hex.EncodeToString(RawHash(data))
}
/*RawHash - Logic to hash the text and return the hash bytes */
func RawHash(data interface{}) []byte {
var databuf []byte
switch dataImpl := data.(type) {
case []byte:
databuf = dataImpl
case HashBytes:
databuf = dataImpl[:]
case string:
databuf = []byte(dataImpl)
default:
panic("unknown type")
}
hash := sha3.New256()
hash.Write(databuf)
var buf []byte
return hash.Sum(buf)
}
func ShaHash(data interface{}) []byte {
var databuf []byte
switch dataImpl := data.(type) {
case []byte:
databuf = dataImpl
case HashBytes:
databuf = dataImpl[:]
case string:
databuf = []byte(dataImpl)
default:
panic("unknown type")
}
hash := sha256.New()
_, _ = hash.Write(databuf)
return hash.Sum(nil)
}
/*FastHash - sha1 hash the given data and return the hash as hex string */
func FastHash(data interface{}) string {
return hex.EncodeToString(RawFastHash(data))
}
/*RawFastHash - Logic to sha1 hash the text and return the hash bytes */
func RawFastHash(data interface{}) []byte {
var databuf []byte
switch dataImpl := data.(type) {
case []byte:
databuf = dataImpl
case HashBytes:
databuf = dataImpl[:]
case string:
databuf = []byte(dataImpl)
default:
panic("unknown type")
}
hash := sha1.New()
hash.Write(databuf)
var buf []byte
return hash.Sum(buf)
}