checksum.go 844 B

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