-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmac256.go
92 lines (78 loc) · 1.38 KB
/
hmac256.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
92
//
// Information about the algorithm is available on Wikipedia
//
// https://en.wikipedia.org/wiki/HMAC
//
package hmac256
// https://golang.org/src/crypto/sha256/sha256.go
import (
"crypto"
_ "crypto/sha256"
"hash"
)
/*
type digest struct {
h [8]uint32
x [64]byte
nx int
len uint64
}
*/
func Digest(input []byte, secret []byte) (checksum []byte) {
var (
opad []byte
ipad []byte
outer hash.Hash
inner hash.Hash
i int
)
outer = crypto.SHA256.New()
inner = crypto.SHA256.New()
ipad = make([]byte, 64)
opad = make([]byte, 64)
if len(secret) > 64 {
outer.Write(secret)
secret = outer.Sum(nil)
/*
hash := outer.checkSum()
secret = append(nil, hash[:]...)
*/
}
copy(ipad, secret)
copy(opad, secret)
i = len(ipad) - 1
for i > 0 {
ipad[i] ^= 0x36
i--
}
i = len(opad) - 1
for i > 0 {
opad[i] ^= 0x5c
i--
}
inner.Write(ipad)
inner.Write(input)
in := inner.Sum(nil)
/*
hash := inner.checkSum()
in = append(nil, hash[:]...)
*/
outer.Reset()
/*
d.h[0] = 0x6A09E667
d.h[1] = 0xBB67AE85
d.h[2] = 0x3C6EF372
d.h[3] = 0xA54FF53A
d.h[4] = 0x510E527F
d.h[5] = 0x9B05688C
d.h[6] = 0x1F83D9AB
d.h[7] = 0x5BE0CD19
d.nx = 0
d.len = 0
*/
outer.Write(opad) //outer.Write(in[0:])
outer.Write(in)
return outer.Sum(in) //return outer.Sum(in[:0])
/*
*/
}