hash

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 11 Imported by: 0

README

hash

Ergonomic wrappers around the standard library's crypto/*, hash/fnv, and hash/crc packages. Zero third-party dependencies — pure stdlib.

Returns raw digest bytes for composability, with hex / base64 convenience helpers for the common "hash a string and format it" case. Every function allocates a fresh hasher per call, so all are safe for concurrent use.

Why

Real services reach for the same handful of hashes repeatedly. This package gives them stable names and the two return shapes people actually want (raw bytes + hex string), without dragging in a crypto framework.

API

Cryptographic digests
hash.MD5(data)     // []byte (16)  — checksums/ETags only; MD5 is broken
hash.SHA1(data)    // []byte (20)  — collision-broken; prefer SHA-256
hash.SHA224(data)  // []byte (28)
hash.SHA256(data)  // []byte (32)  — the workhorse
hash.SHA384(data)  // []byte (48)
hash.SHA512(data)  // []byte (64)

hash.SHA256Hex("auction_id=42")  // lowercase hex string
hash.MD5Hex(""), hash.SHA1Hex(s), hash.SHA512Hex(s)
HMAC (signing)
hash.HMACSHA256(key, data)         // []byte (32) — postbacks, webhooks, MMP callbacks
hash.HMACSHA1(key, data), hash.HMACSHA512(key, data)

hash.HMACSHA256Hex(key, data)      // hex form
hash.HMACSHA256Base64(key, data)   // base64 form (some webhooks expect this)

hash.Equal(a, b)                   // constant-time compare; never use == on MACs
FNV — fast, non-cryptographic, deterministic

For consistent bucketing / sharding of keys where a crypto hash is wasted cost.

hash.FNV1a32(data)   // uint32 — empty input is the offset basis 0x811c9dc5
hash.FNV1a64(data)   // uint64
hash.FNV132(data), hash.FNV164(data)              // FNV-1 (multiply-then-xor) variants
hash.FNV1aString64("user_hash_42")                // string-keyed convenience

bucket := hash.FNV1aString64(userHash) % shardCount
CRC — cheap checksums

For payload validation, ETags, change detection. Never for security.

hash.CRC32IEEE(data)        // uint32 — "123456789" → 0xcbf43926
hash.CRC32IEEEHex(data)     // 8-char lowercase hex
hash.CRC64ISO(data), hash.CRC64ECMA(data)

Ad-tech uses

  • HMAC-SHA256 signs postbacks, MMP callbacks, and SSP webhook payloads.
  • SHA-256 fingerprints auction / bid IDs for dedup and idempotency keys.
  • FNV buckets a user hash into bidder shards or frequency-cap windows cheaply and deterministically.
  • CRC32 produces cheap payload checksums / ETags.

Testing

100% statement coverage, -race clean. Known vectors (NIST FIPS, RFC 1321, RFC 4231), cross-checks against the stdlib reference, and concurrent-call invariants for every family.

go test -race -cover ./hash/...

Documentation

Overview

Package hash provides ergonomic wrappers around the standard library's cryptographic hashes (MD5, SHA-1, SHA-2 family), HMAC, FNV, and CRC.

Each function returns the raw digest bytes for composability, with hex convenience variants for the common "hash a string and format it" case. All functions allocate a fresh hasher per call, so they are safe for concurrent use without locking.

Typical ad-tech uses: signing postbacks and webhooks (HMAC-SHA256), fingerprinting auction or bid IDs (SHA-256), consistent bucketing of a user hash (FNV), and cheap payload checksums (CRC32) — none of which need a third-party dependency.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CRC32IEEE

func CRC32IEEE(data []byte) uint32

CRC32IEEE returns the CRC-32 checksum of data using the IEEE polynomial (the most common CRC-32, used by zlib/PNG/Ethernet). The empty-input value is 0.

func CRC32IEEEHex

func CRC32IEEEHex(data []byte) string

CRC32IEEEHex returns the lowercase hex CRC-32 (IEEE) of data.

func CRC64ECMA

func CRC64ECMA(data []byte) uint64

CRC64ECMA returns the CRC-64 checksum of data using the ECMA polynomial.

func CRC64ISO

func CRC64ISO(data []byte) uint64

CRC64ISO returns the CRC-64 checksum of data using the ISO polynomial.

func Equal

func Equal(a, b []byte) bool

Equal reports whether two MACs or digests are equal in constant time, avoiding timing side-channels. Use it instead of bytes.Equal when comparing signatures.

func FNV1a32

func FNV1a32(data []byte) uint32

FNV1a32 returns the 32-bit FNV-1a hash of data. The empty-input value is the FNV offset basis 0x811c9dc5 (2166136261).

func FNV1a64

func FNV1a64(data []byte) uint64

FNV1a64 returns the 64-bit FNV-1a hash of data.

func FNV1aString32

func FNV1aString32(s string) uint32

FNV1aString32 is a string-keyed convenience wrapper around FNV1a32.

func FNV1aString64

func FNV1aString64(s string) uint64

FNV1aString64 is a string-keyed convenience wrapper around FNV1a64.

func FNV132

func FNV132(data []byte) uint32

FNV132 returns the 32-bit FNV-1 hash of data (the multiply-then-xor variant).

func FNV164

func FNV164(data []byte) uint64

FNV164 returns the 64-bit FNV-1 hash of data.

func HMACSHA1

func HMACSHA1(key, data []byte) []byte

HMACSHA1 returns the HMAC-SHA1 of data under key (20 bytes).

func HMACSHA256

func HMACSHA256(key, data []byte) []byte

HMACSHA256 returns the HMAC-SHA256 of data under key (32 bytes). This is the standard choice for signing postbacks, MMP callbacks, and webhook payloads.

func HMACSHA256Base64

func HMACSHA256Base64(key, data []byte) string

HMACSHA256Base64 returns the standard base64 HMAC-SHA256 of data under key. Many webhook APIs (and some MMPs) expect the signature in base64 rather than hex.

func HMACSHA256Hex

func HMACSHA256Hex(key, data []byte) string

HMACSHA256Hex returns the lowercase hex HMAC-SHA256 of data under key.

Example
package main

import (
	"fmt"

	"github.com/v8fg/kit4go/hash"
)

func main() {
	// HMACSHA256Hex signs data under a shared secret — the standard choice for
	// postbacks, MMP callbacks, and webhook payloads. Pair it with Equal when
	// verifying an inbound signature: never compare MACs with ==.
	sig := hash.HMACSHA256Hex([]byte("secret"), []byte("payload"))
	fmt.Println(sig)

	want := hash.HMACSHA256Hex([]byte("secret"), []byte("payload"))
	fmt.Println(hash.Equal([]byte(sig), []byte(want)))
}
Output:
b82fcb791acec57859b989b430a826488ce2e479fdf92326bd0a2e8375a42ba4
true

func HMACSHA512

func HMACSHA512(key, data []byte) []byte

HMACSHA512 returns the HMAC-SHA512 of data under key (64 bytes).

func MD5

func MD5(data []byte) []byte

MD5 returns the MD5 checksum of data (16 bytes). MD5 is cryptographically broken; use it only for non-security checksums, ETags, or legacy interop.

func MD5Hex

func MD5Hex(s string) string

MD5Hex returns the lowercase hex MD5 of s. Empty input yields the well-known empty-string digest, not "".

func SHA1

func SHA1(data []byte) []byte

SHA1 returns the SHA-1 checksum of data (20 bytes). SHA-1 is collision-broken; prefer SHA-256 for any security-sensitive use.

func SHA1Hex

func SHA1Hex(s string) string

SHA1Hex returns the lowercase hex SHA-1 of s.

func SHA224

func SHA224(data []byte) []byte

SHA224 returns the SHA-224 checksum of data (28 bytes).

func SHA256

func SHA256(data []byte) []byte

SHA256 returns the SHA-256 checksum of data (32 bytes).

func SHA256Hex

func SHA256Hex(s string) string

SHA256Hex returns the lowercase hex SHA-256 of s.

Example
package main

import (
	"fmt"

	"github.com/v8fg/kit4go/hash"
)

func main() {
	// SHA256Hex is the workhorse: lowercase hex digest of a string, useful for
	// fingerprinting auction/bid IDs or deriving idempotency keys. The empty
	// string yields the well-known NIST empty-input digest.
	fmt.Println(hash.SHA256Hex(""))
	fmt.Println(hash.SHA256Hex("hello"))
}
Output:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

func SHA384

func SHA384(data []byte) []byte

SHA384 returns the SHA-384 checksum of data (48 bytes).

func SHA512

func SHA512(data []byte) []byte

SHA512 returns the SHA-512 checksum of data (64 bytes).

func SHA512Hex

func SHA512Hex(s string) string

SHA512Hex returns the lowercase hex SHA-512 of s.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL