Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

hmac, crc64, and fnv

These modules wrap Go's crypto/hmac, hash/crc64, and hash/fnv packages. They accept either str or Bytes input. Digest values are returned as Bytes by sum() and lowercase text by hex().

import "x/crypto/hmac"
import "x/hash/crc64"
import "x/hash/fnv"

var signature = hmac.sum("secret", "message")
print(hmac.verify(signature, "secret", "message"))
print(crc64.hex("123456789"))
print(fnv.hex("hello"))

HMAC

FunctionReturnsDescription
sum(key, data, algorithm="sha256")BytesCompute an HMAC digest
hex(key, data, algorithm="sha256")strCompute an HMAC as hexadecimal text
verify(signature, key, data, algorithm="sha256")boolCompare a raw signature in constant time

Supported algorithms are sha256, sha512, sha1, and md5. SHA-1 and MD5 are provided only for compatibility with existing protocols. verify() expects the raw Bytes returned by sum(), not hexadecimal text.

CRC-64

crc64.sum(data, polynomial=crc64.ECMA) and crc64.hex(...) support the crc64.ECMA and crc64.ISO polynomial constants. Unlike crc32.checksum(), CRC-64 returns Bytes because a Goblin int is signed and cannot represent every unsigned 64-bit checksum.

FNV

fnv.sum(data, variant=fnv.FNV_64A) and fnv.hex(...) support the variant constants FNV_32, FNV_32A, FNV_64, FNV_64A, FNV_128, and FNV_128A. FNV is a non-cryptographic hash; do not use it for passwords, signatures, or integrity checks against an attacker.