TestCrypto_ShaHmac

master
sre 4 years ago
parent 132bb53c43
commit 5fa0e8b06d

@ -2,6 +2,7 @@ package crypto
import ( import (
"bufio" "bufio"
"crypto/hmac"
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
@ -53,3 +54,10 @@ func (c *Crypto) Md5EncodeFile(filename string) (string, error) {
checksum := fmt.Sprintf("%x", hash.Sum(nil)) checksum := fmt.Sprintf("%x", hash.Sum(nil))
return checksum, 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("")))
}

@ -20,3 +20,8 @@ func TestCrypto_Md5EncodeFile(t *testing.T) {
fmt.Println(cryptoTool.Md5EncodeFile(filePath)) fmt.Println(cryptoTool.Md5EncodeFile(filePath))
//fea07bd54d5c1841a682b408fbbb0cf8 <nil> //fea07bd54d5c1841a682b408fbbb0cf8 <nil>
} }
func TestCrypto_Md5Hmac(t *testing.T) {
fmt.Println(cryptoTool.Md5Hmac("hello world!", "123456"))
//0869302654295f1e6100539678192559
}

@ -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("")))
}

@ -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
}

@ -1,11 +0,0 @@
package crypto
import (
"fmt"
"testing"
)
func TestSha256Encode(t *testing.T) {
fmt.Println(cryptoTool.Sha256Encode("hello world!"))
//7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9
}

@ -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
}
Loading…
Cancel
Save