Skip to content

Commit

Permalink
tpl/crypto: Add FNV32a
Browse files Browse the repository at this point in the history
Main motivation to get a integer from a string.
  • Loading branch information
bep committed Apr 27, 2022
1 parent d7b54a4 commit 1104753
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tpl/crypto/crypto.go
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/hex"
"fmt"
"hash"
"hash/fnv"

"github.com/spf13/cast"
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1104753

Please sign in to comment.