[add] Process yao.crypto.hash
This commit is contained in:
parent
0829ae17cd
commit
eba92b3eba
3 changed files with 106 additions and 0 deletions
44
crypto/crypto.go
Normal file
44
crypto/crypto.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
32
crypto/crypto_test.go
Normal file
32
crypto/crypto_test.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
30
crypto/process.go
Normal file
30
crypto/process.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue