commit 11047534e47f2f2c710a6f8504d7415ff27d6024
parent d7b54a4c37c39fce60c25a745bae1d5987b2b966
Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date: Tue, 26 Apr 2022 10:35:45 +0200
tpl/crypto: Add FNV32a
Main motivation to get a integer from a string.
Diffstat:
1 file changed, 12 insertions(+), 0 deletions(-)
diff --git 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)