Standard library / crypto/hmac

crypto/hmac.New()

Construct a keyed message authentication code with a hash constructor and secret key. Use a high-entropy secret key, SHA-256 or another approved hash, and hmac.Equal to verify tags. The fixed key here is a public test vector only. Expected stdout: 'b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7\n'.

Formal Syntax & Structure

go syntaxgo
func New(h func() hash.Hash, key []byte) hash.Hash

Parameters & Attributes

NameTypeDescription
hfunc() hash.HashConstructor returning a fresh hash for every call.
key[]byteSecret authentication key; public fixed value only for tests.

Return Value

hash.Hash

Incremental keyed MAC.

Working Example

Example Snippetgo
package main
import (  "bytes"  "crypto/hmac"  "crypto/sha256"  "fmt")
func main() {  h := hmac.New(sha256.New, bytes.Repeat([]byte{0x0b}, 20))  h.Write([]byte("Hi There"))  fmt.Printf("%x\n", h.Sum(nil))}
Go Reference