forked from wheelcomplex/kmg
/
hmac.go
52 lines (44 loc) · 1.08 KB
/
hmac.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
package kmgCrypto
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/binary"
"hash"
"hash/adler32"
)
//长度32字节
func CheckSha256MAC(message, messageMAC, key []byte) bool {
expectedMAC := GetSha256MAC(message, key)
return hmac.Equal(messageMAC, expectedMAC)
}
//速度比较慢 118MB/s
func GetSha256MAC(message, key []byte) []byte {
mac := hmac.New(sha256.New, key)
mac.Write(message)
return mac.Sum(nil)
}
//长度4字节 速度非常快 1GB/s
func CheckAdler32(message, messageMAC []byte) bool {
get := GetAdler32(message)
return bytes.Equal(get, messageMAC)
}
func GetAdler32(message []byte) []byte {
outi := adler32.Checksum(message)
out := make([]byte, 4)
binary.LittleEndian.PutUint32(out, outi)
return out
}
//长度4字节 速度非常快 1GB/s
func CheckAdler32Mac(message, messageMAC, key []byte) bool {
get := GetAdler32Mac(message, key)
return hmac.Equal(get, messageMAC)
}
func GetAdler32Mac(message, key []byte) []byte {
mac := hmac.New(newAdler32, key)
mac.Write(message)
return mac.Sum(nil)
}
func newAdler32() hash.Hash {
return adler32.New()
}