-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmac.go
37 lines (31 loc) · 808 Bytes
/
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
package main
import "crypto/sha256"
func HmacSha256NormalizeKey(key []byte) []byte {
if len(key) > sha256.BlockSize {
tmp := sha256.Sum256(key)
key = tmp[:]
}
if len(key) < sha256.BlockSize {
paddingLen := sha256.BlockSize - len(key)
padding := make([]byte, paddingLen)
key = append(key, padding...)
}
return key
}
func HmacSha256(key, message []byte) (mac []byte) {
// Normalize key length if necessary
if len(key) != sha256.BlockSize {
key = HmacSha256NormalizeKey(key)
}
// Generate ipad and opad
ipad := make([]byte, len(key))
opad := make([]byte, len(key))
for i := range key {
ipad[i] = key[i] ^ 0x36
opad[i] = key[i] ^ 0x5c
}
// Perform hashing
ihash := sha256.Sum256(append(ipad, message...))
ohash := sha256.Sum256(append(opad, ihash[:]...))
return ohash[:]
}