1
0
Fork 0
mirror of https://github.com/Luzifer/korvike.git synced 2024-11-09 16:00:09 +00:00
korvike/functions/func_hash.go
Knut Ahlers 4083ee0cc2
Add "hash" template function
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-04-11 23:50:59 +02:00

40 lines
707 B
Go

package functions
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
"hash"
)
func init() {
registerFunction("hash", func(name string, v ...string) (string, error) {
if len(v) < 1 {
return "", errors.New("no string to hash")
}
var hash hash.Hash
switch name {
case "md5":
hash = md5.New()
case "sha1":
hash = sha1.New()
case "sha256":
hash = sha256.New()
case "sha512":
hash = sha512.New()
default:
return "", fmt.Errorf("hash algo %q not supported", name)
}
if _, err := hash.Write([]byte(v[0])); err != nil {
return "", fmt.Errorf("writing to hash: %w", err)
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
})
}