blob: 45276e95322fb4804db308e955c7fb2b5d8b311f [file] [log] [blame]
Scott Baker8461e152019-10-01 14:44:30 -07001package rfc4757
2
3import (
4 "bytes"
5 "crypto/hmac"
6 "crypto/md5"
7 "io"
8)
9
10// Checksum returns a hash of the data in accordance with RFC 4757
11func Checksum(key []byte, usage uint32, data []byte) ([]byte, error) {
12 // Create hashing key
13 s := append([]byte(`signaturekey`), byte(0x00)) //includes zero octet at end
14 mac := hmac.New(md5.New, key)
15 mac.Write(s)
16 Ksign := mac.Sum(nil)
17
18 // Format data
19 tb := UsageToMSMsgType(usage)
20 p := append(tb, data...)
21 h := md5.New()
22 rb := bytes.NewReader(p)
23 _, err := io.Copy(h, rb)
24 if err != nil {
25 return []byte{}, err
26 }
27 tmp := h.Sum(nil)
28
29 // Generate HMAC
30 mac = hmac.New(md5.New, Ksign)
31 mac.Write(tmp)
32 return mac.Sum(nil), nil
33}
34
35// HMAC returns a keyed MD5 checksum of the data
36func HMAC(key []byte, data []byte) []byte {
37 mac := hmac.New(md5.New, key)
38 mac.Write(data)
39 return mac.Sum(nil)
40}