tpl/crypto: Add FNV32a

Main motivation to get a integer from a string.
This commit is contained in:
Bjørn Erik Pedersen 2022-04-26 10:35:45 +02:00
parent d7b54a4c37
commit 11047534e4

View file

@ -23,6 +23,7 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"hash" "hash"
"hash/fnv"
"github.com/spf13/cast" "github.com/spf13/cast"
) )
@ -68,6 +69,17 @@ func (ns *Namespace) SHA256(in any) (string, error) {
return hex.EncodeToString(hash[:]), nil return hex.EncodeToString(hash[:]), nil
} }
// FNV32a hashes using fnv32a algorithm
func (ns *Namespace) FNV32a(in any) (int, error) {
conv, err := cast.ToStringE(in)
if err != nil {
return 0, err
}
algorithm := fnv.New32a()
algorithm.Write([]byte(conv))
return int(algorithm.Sum32()), nil
}
// HMAC returns a cryptographic hash that uses a key to sign a message. // HMAC returns a cryptographic hash that uses a key to sign a message.
func (ns *Namespace) HMAC(h any, k any, m any, e ...any) (string, error) { func (ns *Namespace) HMAC(h any, k any, m any, e ...any) (string, error) {
ha, err := cast.ToStringE(h) ha, err := cast.ToStringE(h)