diff --git a/crypto/md5.go b/crypto/md5.go index 4550193..8071483 100644 --- a/crypto/md5.go +++ b/crypto/md5.go @@ -2,6 +2,7 @@ package crypto import ( "bufio" + "crypto/hmac" "crypto/md5" "encoding/hex" "fmt" @@ -53,3 +54,10 @@ func (c *Crypto) Md5EncodeFile(filename string) (string, error) { checksum := fmt.Sprintf("%x", hash.Sum(nil)) return checksum, nil } + +// Md5Hmac return the hmac hash of string use md5 +func (c *Crypto) Md5Hmac(data, key string) string { + h := hmac.New(md5.New, []byte(key)) + h.Write([]byte(data)) + return hex.EncodeToString(h.Sum([]byte(""))) +} diff --git a/crypto/md5_test.go b/crypto/md5_test.go index f9183ed..234bf3f 100644 --- a/crypto/md5_test.go +++ b/crypto/md5_test.go @@ -20,3 +20,8 @@ func TestCrypto_Md5EncodeFile(t *testing.T) { fmt.Println(cryptoTool.Md5EncodeFile(filePath)) //fea07bd54d5c1841a682b408fbbb0cf8 } + +func TestCrypto_Md5Hmac(t *testing.T) { + fmt.Println(cryptoTool.Md5Hmac("hello world!", "123456")) + //0869302654295f1e6100539678192559 +} diff --git a/crypto/sha.go b/crypto/sha.go new file mode 100644 index 0000000..01614a6 --- /dev/null +++ b/crypto/sha.go @@ -0,0 +1,52 @@ +package crypto + +import ( + "crypto/hmac" + "crypto/sha1" + "crypto/sha256" + "crypto/sha512" + "encoding/hex" +) + +// Sha1Encode return the sha1 value (SHA-1 hash algorithm) of string +func (c *Crypto) Sha1Encode(data string) string { + sha1 := sha1.New() + sha1.Write([]byte(data)) + return hex.EncodeToString(sha1.Sum([]byte(""))) +} + +// Sha256Encode Sha256加密 +func (c *Crypto) Sha256Encode(src string) string { + m := sha256.New() + m.Write([]byte(src)) + res := hex.EncodeToString(m.Sum(nil)) + return res +} + +// Sha512Encode return the sha512 value (SHA512 hash algorithm) of string +func (c *Crypto) Sha512Encode(data string) string { + sha512 := sha512.New() + sha512.Write([]byte(data)) + return hex.EncodeToString(sha512.Sum([]byte(""))) +} + +// Sha1Hmac return the hmac hash of string use sha1 +func (c *Crypto) Sha1Hmac(data, key string) string { + h := hmac.New(sha1.New, []byte(key)) + h.Write([]byte(data)) + return hex.EncodeToString(h.Sum([]byte(""))) +} + +// Sha256Hmac return the hmac hash of string use sha256 +func (c *Crypto) Sha256Hmac(data, key string) string { + h := hmac.New(sha256.New, []byte(key)) + h.Write([]byte(data)) + return hex.EncodeToString(h.Sum([]byte(""))) +} + +// Sha512Hmac return the hmac hash of string use sha512 +func (c *Crypto) Sha512Hmac(data, key string) string { + h := hmac.New(sha512.New, []byte(key)) + h.Write([]byte(data)) + return hex.EncodeToString(h.Sum([]byte(""))) +} diff --git a/crypto/sha256.go b/crypto/sha256.go deleted file mode 100644 index 3286a44..0000000 --- a/crypto/sha256.go +++ /dev/null @@ -1,14 +0,0 @@ -package crypto - -import ( - "crypto/sha256" - "encoding/hex" -) - -// Sha256加密 -func (c *Crypto) Sha256Encode(src string) string { - m := sha256.New() - m.Write([]byte(src)) - res := hex.EncodeToString(m.Sum(nil)) - return res -} diff --git a/crypto/sha256_test.go b/crypto/sha256_test.go deleted file mode 100644 index 87d107a..0000000 --- a/crypto/sha256_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package crypto - -import ( - "fmt" - "testing" -) - -func TestSha256Encode(t *testing.T) { - fmt.Println(cryptoTool.Sha256Encode("hello world!")) - //7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 -} diff --git a/crypto/sha_test.go b/crypto/sha_test.go new file mode 100644 index 0000000..53647d2 --- /dev/null +++ b/crypto/sha_test.go @@ -0,0 +1,24 @@ +package crypto + +import ( + "fmt" + "testing" +) + +func TestShaEncode(t *testing.T) { + fmt.Println(cryptoTool.Sha256Encode("hello world!")) + //7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 + fmt.Println(cryptoTool.Sha1Encode("hello world!")) + //430ce34d020724ed75a196dfc2ad67c77772d169 + fmt.Println(cryptoTool.Sha512Encode("hello world!")) + //db9b1cd3262dee37756a09b9064973589847caa8e53d31a9d142ea2701b1b28abd97838bb9a27068ba305dc8d04a45a1fcf079de54d607666996b3cc54f6b67c +} + +func TestCrypto_ShaHmac(t *testing.T) { + fmt.Println(cryptoTool.Sha1Hmac("hello world!", "123456")) + //397d14b5c064f1f3a19c2e8d1f5108d797a47a3c + fmt.Println(cryptoTool.Sha256Hmac("hello world!", "123456")) + //03c6198ea823af32c338b1524d1653ccc065c06af916495a178208189555d428 + fmt.Println(cryptoTool.Sha512Hmac("hello world!", "123456")) + //8a432b00e63bbca7fe42d75d2517724af89f0d5404b42b828adbbcb87b1916e73c3a4186adab399ba636aee431ee35a238e75e87f29583189ca1502d3da9885e +}