HMAC-SHA256

medium · cryptography, hashing, hmac

HMAC-SHA256

HMAC is a keyed hash used to authenticate messages. It is built from a hash function (here SHA-256) and resists length-extension attacks.

HMAC construction:

HMAC(k, m) = H((k' xor opad) || H((k' xor ipad) || m))

Where:

  • block size for SHA-256 is 64 bytes
  • ipad = 0x36 repeated
  • opad = 0x5c repeated
  • k' is the key normalized to block size:
    • if len(k) > 64, k' = H(k)
    • else k' = k
    • then pad with zeros to 64 bytes

Function signature

func HMACSHA256(key, msg []byte) [32]byte

Notes

  • Use the provided SHA256 implementation (do not import crypto/sha256).
  • Return the raw 32-byte digest.
Run tests to see results
No issues detected