mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
tpl/crypto: Add FNV32a
Main motivation to get a integer from a string.
This commit is contained in:
parent
d7b54a4c37
commit
11047534e4
1 changed files with 12 additions and 0 deletions
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue