From 11047534e47f2f2c710a6f8504d7415ff27d6024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 26 Apr 2022 10:35:45 +0200 Subject: [PATCH] tpl/crypto: Add FNV32a Main motivation to get a integer from a string. --- tpl/crypto/crypto.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tpl/crypto/crypto.go b/tpl/crypto/crypto.go index e6e67e64b..42b420d59 100644 --- a/tpl/crypto/crypto.go +++ b/tpl/crypto/crypto.go @@ -23,6 +23,7 @@ import ( "encoding/hex" "fmt" "hash" + "hash/fnv" "github.com/spf13/cast" ) @@ -68,6 +69,17 @@ func (ns *Namespace) SHA256(in any) (string, error) { 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. func (ns *Namespace) HMAC(h any, k any, m any, e ...any) (string, error) { ha, err := cast.ToStringE(h)