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 ¶
- func CRC32IEEE(data []byte) uint32
- func CRC32IEEEHex(data []byte) string
- func CRC64ECMA(data []byte) uint64
- func CRC64ISO(data []byte) uint64
- func Equal(a, b []byte) bool
- func FNV1a32(data []byte) uint32
- func FNV1a64(data []byte) uint64
- func FNV1aString32(s string) uint32
- func FNV1aString64(s string) uint64
- func FNV132(data []byte) uint32
- func FNV164(data []byte) uint64
- func HMACSHA1(key, data []byte) []byte
- func HMACSHA256(key, data []byte) []byte
- func HMACSHA256Base64(key, data []byte) string
- func HMACSHA256Hex(key, data []byte) string
- func HMACSHA512(key, data []byte) []byte
- func MD5(data []byte) []byte
- func MD5Hex(s string) string
- func SHA1(data []byte) []byte
- func SHA1Hex(s string) string
- func SHA224(data []byte) []byte
- func SHA256(data []byte) []byte
- func SHA256Hex(s string) string
- func SHA384(data []byte) []byte
- func SHA512(data []byte) []byte
- func SHA512Hex(s string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CRC32IEEE ¶
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 ¶
CRC32IEEEHex returns the lowercase hex CRC-32 (IEEE) of data.
func Equal ¶
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 ¶
FNV1a32 returns the 32-bit FNV-1a hash of data. The empty-input value is the FNV offset basis 0x811c9dc5 (2166136261).
func FNV1aString32 ¶
FNV1aString32 is a string-keyed convenience wrapper around FNV1a32.
func FNV1aString64 ¶
FNV1aString64 is a string-keyed convenience wrapper around FNV1a64.
func HMACSHA256 ¶
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 ¶
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 ¶
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 ¶
HMACSHA512 returns the HMAC-SHA512 of data under key (64 bytes).
func MD5 ¶
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 ¶
MD5Hex returns the lowercase hex MD5 of s. Empty input yields the well-known empty-string digest, not "".
func SHA1 ¶
SHA1 returns the SHA-1 checksum of data (20 bytes). SHA-1 is collision-broken; prefer SHA-256 for any security-sensitive use.
func SHA256Hex ¶
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
Types ¶
This section is empty.