zhmac

package
v0.0.0-alpha.18 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNotAvailable = errors.New("zhmac: hash alg not available")
	ErrNotMatch     = errors.New("zhmac: hmac sum not match")
)

Functions

func Compare

func Compare(h crypto.Hash, msg, key, sum []byte) (err error)

Compare compares HMAC digest generated by the msg and key and the sum. If they matched, it returns nil. If not matched, it returns non-nil error.

func Equal

func Equal(h crypto.Hash, msg, key, sum []byte) bool

Equal reports if HMAC digest generated by the msg and key and the sum are the same or not.

func Sum

func Sum(h crypto.Hash, msg, key []byte) []byte

Sum returns HMAC digest. It supports any hash algorithm supported by the standard hash package. Note that for blake2b families, it does not use HMAC mechanism that blake2b natively support. It panics with ErrNotAvailable if the given hash is not available.

When testing or validating the results, we use openssl and python libraries.

Openssl command examples:

  • echo -n "text" | openssl dgst -hmac "key" -md4
  • echo -n "text" | openssl dgst -hmac "key" -md5
  • echo -n "text" | openssl dgst -hmac "key" -sha1
  • echo -n "text" | openssl dgst -hmac "key" -sha224
  • echo -n "text" | openssl dgst -hmac "key" -sha256
  • echo -n "text" | openssl dgst -hmac "key" -sha256
  • echo -n "text" | openssl dgst -hmac "key" -sha384
  • echo -n "text" | openssl dgst -hmac "key" -sha512
  • echo -n "text" | openssl dgst -hmac "key" -ripemd160
  • echo -n "text" | openssl dgst -hmac "key" -sha3-224
  • echo -n "text" | openssl dgst -hmac "key" -sha3-256
  • echo -n "text" | openssl dgst -hmac "key" -sha3-384
  • echo -n "text" | openssl dgst -hmac "key" -sha3-512
  • echo -n "text" | openssl dgst -hmac "key" -sha512-224
  • echo -n "text" | openssl dgst -hmac "key" -sha512-256
  • echo -n "text" | openssl dgst -hmac "key" -blake2s256

Python example:

>> import hmac, hashlib
>> def blake2b_256():
>>     return hashlib.blake2b(digest_size=32)
>> hmac.new(b"key",b"text", blake2b_256).hexdigest()
Example (Blake22)
package main

import (
	"crypto"
	"encoding/hex"
	"fmt"

	_ "crypto/sha256"
	"github.com/aileron-projects/go/zcrypto/zhmac"

	_ "golang.org/x/crypto/blake2b"
	_ "golang.org/x/crypto/blake2s"
	_ "golang.org/x/crypto/sha3"
)

func main() {
	// The hmac value can be validated using python.
	// Note that it does not use blake2s native HMAC mechanism
	//   >> import hmac, hashlib
	//   >> hmac.new(b"1234567890", b"hello world", lambda:hashlib.blake2s(digest_size=32)).hexdigest()
	msg := []byte("hello world")
	key := []byte("1234567890")
	b := zhmac.Sum(crypto.BLAKE2s_256, msg, key)
	fmt.Println(hex.EncodeToString(b))
}
Output:

dfb87acb873eebfb2b8cb06465dbbea440519fb39fae2b5d1f63a165ab331a4f
Example (Blake2b)
package main

import (
	"crypto"
	"encoding/hex"
	"fmt"

	_ "crypto/sha256"
	"github.com/aileron-projects/go/zcrypto/zhmac"

	_ "golang.org/x/crypto/blake2b"
	_ "golang.org/x/crypto/blake2s"
	_ "golang.org/x/crypto/sha3"
)

func main() {
	// The hmac value can be validated using python.
	// Note that it does not use blake2b native HMAC mechanism
	//   >> import hmac, hashlib
	//   >> hmac.new(b"1234567890", b"hello world", lambda:hashlib.blake2b(digest_size=32)).hexdigest()
	msg := []byte("hello world")
	key := []byte("1234567890")
	b := zhmac.Sum(crypto.BLAKE2b_256, msg, key)
	fmt.Println(hex.EncodeToString(b))
}
Output:

2c7e515a2659e64117f1111d0db87194b2dfe6aa84c7946fcef09afb5e1a864a
Example (Sha256)
package main

import (
	"crypto"
	"encoding/hex"
	"fmt"

	_ "crypto/sha256"
	"github.com/aileron-projects/go/zcrypto/zhmac"

	_ "golang.org/x/crypto/blake2b"
	_ "golang.org/x/crypto/blake2s"
	_ "golang.org/x/crypto/sha3"
)

func main() {
	// The hmac value can be validated using openssl.
	// echo -n "hello world" | openssl dgst -hmac "1234567890" -sha256
	msg := []byte("hello world")
	key := []byte("1234567890")
	b := zhmac.Sum(crypto.SHA256, msg, key)
	fmt.Println(hex.EncodeToString(b))
}
Output:

e45f6072617cce5e06a95be481c43351023d99233599eac9fdaffc958142629a
Example (Sha3)
package main

import (
	"crypto"
	"encoding/hex"
	"fmt"

	_ "crypto/sha256"
	"github.com/aileron-projects/go/zcrypto/zhmac"

	_ "golang.org/x/crypto/blake2b"
	_ "golang.org/x/crypto/blake2s"
	_ "golang.org/x/crypto/sha3"
)

func main() {
	// The hmac value can be validated using openssl.
	// echo -n "hello world" | openssl dgst -hmac "1234567890" -sha3-256
	msg := []byte("hello world")
	key := []byte("1234567890")
	b := zhmac.Sum(crypto.SHA3_256, msg, key)
	fmt.Println(hex.EncodeToString(b))
}
Output:

fa66d35e0fba7a2ca62b61b0fb837ad280c837a09f250f4774c4b0253e325f7e

Types

This section is empty.

Jump to

Keyboard shortcuts

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