From c4f4dddff55c6ce736b6cfa78db21e017b71b8cf Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Wed, 23 May 2018 12:11:25 -0700 Subject: [PATCH] porting crypto functions --- lang/funcs/crypto.go | 48 +++++++++++++++++++++++++++++++++++++++ lang/funcs/crypto_test.go | 33 +++++++++++++++++++++++++++ lang/functions.go | 6 ++--- 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/lang/funcs/crypto.go b/lang/funcs/crypto.go index a491f54423..c2a0741927 100644 --- a/lang/funcs/crypto.go +++ b/lang/funcs/crypto.go @@ -190,6 +190,44 @@ var Sha1Func = function.New(&function.Spec{ }, }) +// Sha256Func contructs a function that computes the SHA256 hash of a given string +// and encodes it with hexadecimal digits. +var Sha256Func = function.New(&function.Spec{ + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + Type: function.StaticReturnType(cty.String), + Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) { + s := args[0].AsString() + h := sha256.New() + h.Write([]byte(s)) + hash := hex.EncodeToString(h.Sum(nil)) + return cty.StringVal(hash), nil + }, +}) + +// Sha512Func contructs a function that computes the SHA256 hash of a given string +// and encodes it with hexadecimal digits. +var Sha512Func = function.New(&function.Spec{ + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + Type: function.StaticReturnType(cty.String), + Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) { + s := args[0].AsString() + h := sha256.New() + h.Write([]byte(s)) + hash := hex.EncodeToString(h.Sum(nil)) + return cty.StringVal(hash), nil + }, +}) + // UUID generates and returns a Type-4 UUID in the standard hexadecimal string // format. // @@ -245,3 +283,13 @@ func RsaDecrypt(ciphertext, privatekey cty.Value) (cty.Value, error) { func Sha1(str cty.Value) (cty.Value, error) { return Sha1Func.Call([]cty.Value{str}) } + +// Sha256 computes the SHA256 hash of a given string and encodes it with hexadecimal digits. +func Sha256(str cty.Value) (cty.Value, error) { + return Sha256Func.Call([]cty.Value{str}) +} + +// Sha512 computes the SHA512 hash of a given string and encodes it with hexadecimal digits. +func Sha512(str cty.Value) (cty.Value, error) { + return Sha512Func.Call([]cty.Value{str}) +} diff --git a/lang/funcs/crypto_test.go b/lang/funcs/crypto_test.go index 8dcd404b0d..f31fd0f178 100644 --- a/lang/funcs/crypto_test.go +++ b/lang/funcs/crypto_test.go @@ -268,6 +268,39 @@ func TestSha1(t *testing.T) { } } +func TestSha256(t *testing.T) { + tests := []struct { + String cty.Value + Want cty.Value + Err bool + }{ + { + cty.StringVal("test"), + cty.StringVal("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"), + false, + }, + } + + for _, test := range tests { + t.Run(fmt.Sprintf("sha256(%#v)", test.String), func(t *testing.T) { + got, err := Sha256(test.String) + + if test.Err { + if err == nil { + t.Fatal("succeeded; want error") + } + return + } else if err != nil { + t.Fatalf("unexpected error: %s", err) + } + + if !got.RawEquals(test.Want) { + t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) + } + }) + } +} + const ( CipherBase64 = "eczGaDhXDbOFRZGhjx2etVzWbRqWDlmq0bvNt284JHVbwCgObiuyX9uV0LSAMY707IEgMkExJqXmsB4OWKxvB7epRB9G/3+F+pcrQpODlDuL9oDUAsa65zEpYF0Wbn7Oh7nrMQncyUPpyr9WUlALl0gRWytOA23S+y5joa4M34KFpawFgoqTu/2EEH4Xl1zo+0fy73fEto+nfkUY+meuyGZ1nUx/+DljP7ZqxHBFSlLODmtuTMdswUbHbXbWneW51D7Jm7xB8nSdiA2JQNK5+Sg5x8aNfgvFTt/m2w2+qpsyFa5Wjeu6fZmXSl840CA07aXbk9vN4I81WmJyblD/ZA==" PrivateKey = ` diff --git a/lang/functions.go b/lang/functions.go index d28418c8ec..7a9e9d5ddd 100644 --- a/lang/functions.go +++ b/lang/functions.go @@ -79,9 +79,9 @@ func (s *Scope) Functions() map[string]function.Function { "pow": unimplFunc, // TODO "replace": unimplFunc, // TODO "rsadecrypt": funcs.RsaDecryptFunc, - "sha1": unimplFunc, // TODO - "sha256": unimplFunc, // TODO - "sha512": unimplFunc, // TODO + "sha1": funcs.Sha1Func, + "sha256": funcs.Sha256Func, + "sha512": funcs.Sha512Func, "signum": unimplFunc, // TODO "slice": unimplFunc, // TODO "sort": funcs.SortFunc,