From e913258d7bd27aceb5e51ff48c39518a541b6fa3 Mon Sep 17 00:00:00 2001 From: Dominik Schulz Date: Sat, 18 Nov 2023 07:43:40 +0100 Subject: [PATCH] [enhancement] Add blake3 to the template functions (#2693) * [enhancement] Add blake3 to the template functions Signed-off-by: Dominik Schulz * Add CC0-1.0 to unrestricted licenses. Signed-off-by: Dominik Schulz --------- Signed-off-by: Dominik Schulz --- .license-lint.yml | 1 + go.mod | 2 ++ go.sum | 6 ++++++ internal/hashsum/hashsums.go | 6 ++++++ internal/tpl/funcs.go | 9 +++++++++ internal/tpl/template_test.go | 1 + 6 files changed, 25 insertions(+) diff --git a/.license-lint.yml b/.license-lint.yml index 2e3c22140f..5c2faba739 100644 --- a/.license-lint.yml +++ b/.license-lint.yml @@ -5,6 +5,7 @@ unrestricted_licenses: - BSD-2-Clause - 0BSD - WTFPL + - CC0-1.0 reciprocal_licenses: - MPL-2.0 - MPL-2.0-no-copyleft-exception diff --git a/go.mod b/go.mod index 5074d37bfe..cebd845ef7 100644 --- a/go.mod +++ b/go.mod @@ -71,6 +71,7 @@ require ( github.com/jwalton/go-supportscolor v1.2.0 // indirect github.com/kjk/lzmadec v0.0.0-20210713164611-19ac3ee91a71 // indirect github.com/klauspost/compress v1.16.7 // indirect + github.com/klauspost/cpuid/v2 v2.0.12 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/lxn/win v0.0.0-20210218163916-a377121e959e // indirect github.com/mattn/go-runewidth v0.0.15 // indirect @@ -82,6 +83,7 @@ require ( github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/ulikunitz/xz v0.5.11 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect + github.com/zeebo/blake3 v0.2.3 // indirect golang.org/x/sync v0.3.0 // indirect golang.org/x/text v0.13.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect diff --git a/go.sum b/go.sum index 3cae3e78e6..5859eed313 100644 --- a/go.sum +++ b/go.sum @@ -105,6 +105,8 @@ github.com/kjk/lzmadec v0.0.0-20210713164611-19ac3ee91a71 h1:TYp9Fj0apeZMWentXRa github.com/kjk/lzmadec v0.0.0-20210713164611-19ac3ee91a71/go.mod h1:2zRkQCuw/eK6cqkYAeNqyBU7JKa2Gcq40BZ9GSJbmfE= github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE= +github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -194,6 +196,10 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsr github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/zalando/go-keyring v0.2.3 h1:v9CUu9phlABObO4LPWycf+zwMG7nlbb3t/B5wa97yms= github.com/zalando/go-keyring v0.2.3/go.mod h1:HL4k+OXQfJUWaMnqyuSOc0drfGPX2b51Du6K+MRgZMk= +github.com/zeebo/assert v1.1.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0= +github.com/zeebo/blake3 v0.2.3 h1:TFoLXsjeXqRNFxSbk35Dk4YtszE/MQQGK10BH4ptoTg= +github.com/zeebo/blake3 v0.2.3/go.mod h1:mjJjZpnsyIVtVgTOSpJ9vmRE4wgDeyt2HU3qXvvKCaQ= +github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= diff --git a/internal/hashsum/hashsums.go b/internal/hashsum/hashsums.go index 4b3c9dd4a9..ef9adb1a1a 100644 --- a/internal/hashsum/hashsums.go +++ b/internal/hashsum/hashsums.go @@ -6,6 +6,8 @@ import ( "crypto/sha256" "crypto/sha512" "fmt" + + "github.com/zeebo/blake3" ) func MD5Hex(in string) string { @@ -23,3 +25,7 @@ func SHA256Hex(in string) string { func SHA512Hex(in string) string { return fmt.Sprintf("%x", sha512.Sum512([]byte(in))) } + +func Blake3Hex(in string) string { + return fmt.Sprintf("%x", blake3.Sum256([]byte(in))) +} diff --git a/internal/tpl/funcs.go b/internal/tpl/funcs.go index 87137641b5..b3763f3bcb 100644 --- a/internal/tpl/funcs.go +++ b/internal/tpl/funcs.go @@ -26,6 +26,7 @@ const ( FuncSha1sum = "sha1sum" FuncSha256sum = "sha256sum" FuncSha512sum = "sha512sum" + FuncBlake3 = "blake3" FuncMd5Crypt = "md5crypt" FuncSSHA = "ssha" FuncSSHA256 = "ssha256" @@ -67,6 +68,12 @@ func sha512sum() func(...string) (string, error) { } } +func blake3sum() func(...string) (string, error) { + return func(s ...string) (string, error) { + return hashsum.Blake3Hex(s[0]), nil + } +} + // saltLen tries to parse the given string into a numeric salt length. func saltLen(s []string) uint8 { if len(s) < 2 { @@ -379,6 +386,7 @@ func funcMap(ctx context.Context, kv kvstore) template.FuncMap { FuncSha1sum: sha1sum(), FuncSha256sum: sha256sum(), FuncSha512sum: sha512sum(), + FuncBlake3: blake3sum(), FuncMd5Crypt: md5cryptFunc(), FuncSSHA: sshaFunc(), FuncSSHA256: ssha256Func(), @@ -400,6 +408,7 @@ func PublicFuncMap() template.FuncMap { FuncSha1sum: sha1sum(), FuncSha256sum: sha256sum(), FuncSha512sum: sha512sum(), + FuncBlake3: blake3sum(), FuncMd5Crypt: md5cryptFunc(), FuncSSHA: sshaFunc(), FuncSSHA256: ssha256Func(), diff --git a/internal/tpl/template_test.go b/internal/tpl/template_test.go index 308745d78d..4338546d29 100644 --- a/internal/tpl/template_test.go +++ b/internal/tpl/template_test.go @@ -24,6 +24,7 @@ func Example() { //nolint:testableexamples Password-value of the new entry: {{ .Content }} Md5sum of the new password: {{ .Content | md5sum }} Sha1sum of the new password: {{ .Content | sha1sum }} +Blake3sum of the new password: {{ .Content | blake3sum }} Md5crypt of the new password: {{ .Content | md5crypt }} SSHA of the new password: {{ .Content | ssha }} SSHA256 of the new password: {{ .Content | ssha256 }}