Hash-based Message Authentication Code (HMAC)

Overview

This page contains recommendations for using a Hash-based message authentication code (HMAC).

General

Hash algorithmBlock size, bytes

SHA-256

64

SHA-512

128

SHA3-256

136

SHA3-512

72

  • You can use HMAC to check the integrity (signature) of messages between internal systems.

  • Do not use HMAC to integrate with a third-party system, use digital signatures.

  • Use HMAC based on hash algorithms from the SHA-2 family.

HMAC implementation

Use the crypto/hmac package to calculate HMAC in Go.

import (
    "crypto/sha256"
    "crypto/hmac"
)

func CalculateHMAC(message, key []byte) []bytes {
    mac := hmac.New(sha256.New, key)
    mac.Write(message)
    return mac.Sum(nil)
}

Last updated