porting crypto functions
This commit is contained in:
parent
eb1d8b7909
commit
c4f4dddff5
|
@ -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})
|
||||
}
|
||||
|
|
|
@ -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 = `
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue