/
hash.go
71 lines (54 loc) · 1.48 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
package hash
import (
"crypto/md5"
"crypto/sha256"
"crypto/sha512"
"strings"
"github.com/3JoB/unsafeConvert"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/sha3"
"github.com/3JoB/ulib/internal/hash"
)
func SHA3_224(data []byte) *hash.Hash {
return hash.Crypt(sha3.New224(), data)
}
func SHA3_256(data []byte) *hash.Hash {
return hash.Crypt(sha3.New256(), data)
}
func SHA3_384(data []byte) *hash.Hash {
return hash.Crypt(sha3.New384(), data)
}
func SHA3_512(data []byte) *hash.Hash {
return hash.Crypt(sha3.New512(), data)
}
func SHA224(data []byte) *hash.Hash {
return hash.Crypt(sha256.New224(), data)
}
func SHA256(data []byte) *hash.Hash {
return hash.Crypt(sha256.New(), data)
}
func SHA384(data []byte) *hash.Hash {
return hash.Crypt(sha512.New384(), data)
}
func SHA512(data []byte) *hash.Hash {
return hash.Crypt(sha512.New(), data)
}
func SHA512_224(data []byte) *hash.Hash {
return hash.Crypt(sha512.New512_224(), data)
}
func SHA512_256(data []byte) *hash.Hash {
return hash.Crypt(sha512.New512_256(), data)
}
func MD5(data []byte) *hash.Hash {
return hash.Crypt(md5.New(), data)
}
func HashBcrypt(password string) []byte {
hash, _ := bcrypt.GenerateFromPassword(unsafeConvert.BytePointer(password), bcrypt.DefaultCost)
return hash
}
func CorrectBcrypt(hash []byte, password string) bool {
return bcrypt.CompareHashAndPassword(hash, unsafeConvert.BytePointer(password)) == nil
}
func MD5Str(data []byte) string {
return strings.ToUpper(MD5(data).Hex())[8:24]
}