From eba92b3eba0778df8eccf69201e9ed3d8c05c573 Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 27 May 2022 11:28:05 +0800 Subject: [PATCH] [add] Process yao.crypto.hash --- crypto/crypto.go | 44 +++++++++++++++++++++++++++++++++++++++++++ crypto/crypto_test.go | 32 +++++++++++++++++++++++++++++++ crypto/process.go | 30 +++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 crypto/crypto.go create mode 100644 crypto/crypto_test.go create mode 100644 crypto/process.go diff --git a/crypto/crypto.go b/crypto/crypto.go new file mode 100644 index 00000000..06a5233f --- /dev/null +++ b/crypto/crypto.go @@ -0,0 +1,44 @@ +package crypto + +import ( + "crypto" + "fmt" + + "golang.org/x/crypto/md4" +) + +func init() { + crypto.RegisterHash(crypto.MD4, md4.New) +} + +var hashTypes = map[string]crypto.Hash{ + "MD4": crypto.MD4, + "MD5": crypto.MD5, + "SHA1": crypto.SHA1, + "SHA224": crypto.SHA224, + "SHA256": crypto.SHA256, + "SHA384": crypto.SHA384, + "SHA512": crypto.SHA512, + "MD5SHA1": crypto.MD5SHA1, + "RIPEMD160": crypto.RIPEMD160, + "SHA3_224": crypto.SHA3_224, + "SHA3_256": crypto.SHA3_256, + "SHA3_384": crypto.SHA3_384, + "SHA3_512": crypto.SHA3_512, + "SHA512_224": crypto.SHA512_224, + "SHA512_256": crypto.SHA512_256, + "BLAKE2s_256": crypto.BLAKE2s_256, + "BLAKE2b_256": crypto.BLAKE2b_256, + "BLAKE2b_384": crypto.BLAKE2b_384, + "BLAKE2b_512": crypto.BLAKE2b_512, +} + +// Hash string +func Hash(hash crypto.Hash, value string) (string, error) { + h := hash.New() + _, err := h.Write([]byte(value)) + if err != nil { + return "", err + } + return fmt.Sprintf("%x", h.Sum(nil)), nil +} diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go new file mode 100644 index 00000000..eff57f91 --- /dev/null +++ b/crypto/crypto_test.go @@ -0,0 +1,32 @@ +package crypto + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/yaoapp/gou" +) + +func TestMD4(t *testing.T) { + args := []interface{}{"MD4", "123456"} + res := gou.NewProcess("yao.crypto.hash", args...).Run() + assert.Equal(t, "585028aa0f794af812ee3be8804eb14a", res) +} + +func TestMD5(t *testing.T) { + args := []interface{}{"MD5", "123456"} + res := gou.NewProcess("yao.crypto.hash", args...).Run() + assert.Equal(t, "e10adc3949ba59abbe56e057f20f883e", res) +} + +func TestSHA1(t *testing.T) { + args := []interface{}{"SHA1", "123456"} + res := gou.NewProcess("yao.crypto.hash", args...).Run() + assert.Equal(t, "7c4a8d09ca3762af61e59520943dc26494f8941b", res) +} + +func TestSHA256(t *testing.T) { + args := []interface{}{"SHA256", "123456"} + res := gou.NewProcess("yao.crypto.hash", args...).Run() + assert.Equal(t, "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", res) +} diff --git a/crypto/process.go b/crypto/process.go new file mode 100644 index 00000000..d58f8297 --- /dev/null +++ b/crypto/process.go @@ -0,0 +1,30 @@ +package crypto + +import ( + "github.com/yaoapp/gou" + "github.com/yaoapp/kun/exception" +) + +func init() { + gou.RegisterProcessHandler("yao.crypto.hash", ProcessHash) +} + +// ProcessHash yao.crypto.hash Crypto Hash +// Args[0] string: the hash function name. MD4/MD5/SHA1/SHA224/SHA256/SHA384/SHA512/MD5SHA1/RIPEMD160/SHA3_224/SHA3_256/SHA3_384/SHA3_512/SHA512_224/SHA512_256/BLAKE2s_256/BLAKE2b_256/BLAKE2b_384/BLAKE2b_512 +// Args[1] string: value +func ProcessHash(process *gou.Process) interface{} { + process.ValidateArgNums(1) + typ := process.ArgsString(0) + value := process.ArgsString(1) + + h, has := hashTypes[typ] + if !has { + exception.New("%s does not support", 400, typ).Throw() + } + + res, err := Hash(h, value) + if err != nil { + exception.New("%s error: %s value: %s", 400, typ, err, value).Throw() + } + return res +}