hmac.mx raw
1 package hmac
2
3 import "smesh.lol/web/common/jsbridge/schnorr"
4
5 const blockSize = 64
6
7 // Sum computes HMAC-SHA256(key, message).
8 func Sum(key, message []byte) [32]byte {
9 // If key > blockSize, hash it.
10 var k [blockSize]byte
11 if len(key) > blockSize {
12 h := schnorr.SHA256Sum(key)
13 copy(k[:], h)
14 } else {
15 copy(k[:], key)
16 }
17
18 var ipad, opad [blockSize]byte
19 for i := 0; i < blockSize; i++ {
20 ipad[i] = k[i] ^ 0x36
21 opad[i] = k[i] ^ 0x5c
22 }
23
24 // inner = SHA256(ipad || message)
25 inner := []byte{:blockSize+len(message)}
26 copy(inner, ipad[:])
27 copy(inner[blockSize:], message)
28 innerHash := schnorr.SHA256Sum(inner)
29
30 // outer = SHA256(opad || inner_hash)
31 outer := []byte{:blockSize+32}
32 copy(outer[:blockSize], opad[:])
33 copy(outer[blockSize:], innerHash)
34 result := schnorr.SHA256Sum(outer)
35 var out [32]byte
36 copy(out[:], result)
37 return out
38 }
39