anyhash

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 15 Imported by: 0

README

anyhash

GoDoc Build Status Coverage Status Go Report Card

A Go hashing library that supports 60 hash algorithms selected by name. Unlike Go's standard crypto packages, anyhash accepts algorithm names as strings and provides a Clone() method to duplicate hash state at any point during computation.

Features

  • String-based algorithm selectionNew("sha256") instead of importing crypto/sha256
  • Name normalization — case-insensitive, hyphens ignored: "SHA-256", "sha256", "SHA256" all work
  • Cloneable stateClone() creates an independent copy of the hash mid-computation
  • Continue after Sum — calling Sum() does not reset the hash; you can keep writing
  • HMAC supportNewHMAC("sha256", key) with full Clone/continue-after-Sum support
  • HKDF supportNewHKDF("sha256", ikm, length, info, salt) for key derivation (RFC 5869)
  • Zero external dependencies — all algorithms implemented using only the Go standard library
  • PHP compatible — algorithm names and output match PHP's hash() function

Installation

go get github.com/KarpelesLab/anyhash

Usage

package main

import (
    "encoding/hex"
    "fmt"
    "github.com/KarpelesLab/anyhash"
)

func main() {
    // Create a hash by name
    h, err := anyhash.New("sha256")
    if err != nil {
        panic(err)
    }
    h.Write([]byte("hello "))

    // Clone the state
    h2 := h.Clone()

    // Continue writing to both independently
    h.Write([]byte("world"))
    h2.Write([]byte("there"))

    fmt.Println(hex.EncodeToString(h.Sum(nil)))  // sha256("hello world")
    fmt.Println(hex.EncodeToString(h2.Sum(nil))) // sha256("hello there")

    // HMAC
    mac, _ := anyhash.NewHMAC("sha256", []byte("secret"))
    mac.Write([]byte("message"))
    fmt.Println(hex.EncodeToString(mac.Sum(nil)))

    // HKDF (RFC 5869)
    key, _ := anyhash.NewHKDF("sha256", []byte("input key"), 32, []byte("info"), nil)
    fmt.Println(hex.EncodeToString(key))

    // List all supported algorithms
    fmt.Println(anyhash.List())
}

Supported Algorithms

Category Algorithms
MD md2, md4, md5
SHA-1/2 sha1, sha224, sha256, sha384, sha512, sha512/224, sha512/256
SHA-3 sha3-224, sha3-256, sha3-384, sha3-512
RIPEMD ripemd128, ripemd160, ripemd256, ripemd320
Tiger tiger128,3 tiger160,3 tiger192,3 tiger128,4 tiger160,4 tiger192,4
HAVAL haval{128,160,192,224,256},{3,4,5} (15 variants)
Whirlpool whirlpool
GOST gost, gost-crypto
Snefru snefru, snefru256
Checksums adler32, crc32, crc32b, crc32c
FNV fnv132, fnv1a32, fnv164, fnv1a64
MurmurHash3 murmur3a, murmur3c, murmur3f
xxHash xxh32, xxh64, xxh3, xxh128
Other joaat

API

// Hash extends hash.Hash with cloning support.
type Hash interface {
    hash.Hash
    Clone() Hash
}

// New creates a hash by algorithm name.
func New(algo string) (Hash, error)

// NewHMAC creates an HMAC by algorithm name.
func NewHMAC(algo string, key []byte) (Hash, error)

// NewHKDF derives key material using HKDF (RFC 5869).
func NewHKDF(algo string, key []byte, length int, info []byte, salt []byte) ([]byte, error)

// List returns all supported algorithm names, sorted.
func List() []string

License

MIT

Documentation

Overview

Package anyhash provides a unified interface for hash algorithms selected by name.

Unlike Go's standard crypto packages, anyhash lets you specify the algorithm as a string and provides a Clone method to duplicate hash state at any point.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func List

func List() []string

List returns the sorted list of supported algorithm names.

func MarshalPHP added in v0.1.1

func MarshalPHP(h Hash) (algo string, state []any, err error)

MarshalPHP serializes a Hash's internal state into PHP-compatible components.

func NewHKDF

func NewHKDF(algo string, key []byte, length int, info []byte, salt []byte) ([]byte, error)

NewHKDF derives key material using HKDF (RFC 5869) with the named hash algorithm. It returns length bytes of output key material.

If salt is nil, a zero-filled salt of HashLen bytes is used per the RFC. info may be nil for empty context.

Types

type Hash

type Hash interface {
	hash.Hash
	// Clone returns an independent copy of the hash in its current state.
	Clone() Hash
}

Hash extends hash.Hash with the ability to clone the current state.

func New

func New(algo string, opts ...Options) (Hash, error)

New creates a new Hash for the named algorithm. Algorithm names are case-insensitive and hyphens are ignored, so "SHA-256", "sha256", and "SHA256" all work.

func NewHMAC

func NewHMAC(algo string, key []byte) (Hash, error)

NewHMAC creates a new HMAC using the named hash algorithm and the given key. The returned Hash supports Clone and continue-after-Sum like any other Hash.

func UnmarshalPHP added in v0.1.1

func UnmarshalPHP(algo string, state []any) (Hash, error)

UnmarshalPHP creates a Hash from PHP-compatible serialized state.

type Options added in v0.1.4

type Options struct {
	Seed   uint64 // seed value (murmur3, xxh32, xxh64, xxh3, xxh128)
	Secret []byte // custom secret (xxh3, xxh128 only, must be >= 136 bytes)
}

Options configures optional parameters for hash creation. Currently used for seeded hashes (murmur3, xxh32, xxh64) and secret-keyed hashes (xxh3, xxh128).

type PHPMarshaler added in v0.1.1

type PHPMarshaler interface {
	// PHPAlgo returns the PHP-compatible algorithm name (e.g. "sha256", "tiger192,3").
	PHPAlgo() string
	// MarshalPHP returns the hash state as a PHP-compatible array.
	// Elements are int32 or []byte, matching PHP's serialize() format.
	MarshalPHP() []any
	// UnmarshalPHP restores the hash state from a PHP-compatible array.
	UnmarshalPHP(state []any) error
}

PHPMarshaler is implemented by hashes that support PHP-compatible state serialization. The state array elements are int32 (matching PHP integers) or []byte (matching PHP strings). This mirrors PHP's HashContext serialization.

type Secreter added in v0.1.4

type Secreter interface {
	SetSecret(secret []byte) error
}

Secreter is implemented by hashes that support custom secrets.

type Seeder added in v0.1.4

type Seeder interface {
	SetSeed(seed uint64)
}

Seeder is implemented by hashes that support seeding.

Jump to

Keyboard shortcuts

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