mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 17:22:27 +08:00
test: add test for cryptor package new functions
This commit is contained in:
@@ -144,7 +144,7 @@ func HmacSha512(data, key string) string {
|
|||||||
return hex.EncodeToString(h.Sum([]byte("")))
|
return hex.EncodeToString(h.Sum([]byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
// HmacSha512 return the hmac hash of string use sha512.
|
// HmacSha512 return the hmac hash of string use sha512 with base64..
|
||||||
// Play: https://go.dev/play/p/61wBBOKO-GH
|
// Play: https://go.dev/play/p/61wBBOKO-GH
|
||||||
func HmacSha512WithBase64(data, key string) string {
|
func HmacSha512WithBase64(data, key string) string {
|
||||||
h := hmac.New(sha512.New, []byte(key))
|
h := hmac.New(sha512.New, []byte(key))
|
||||||
@@ -160,8 +160,8 @@ func Sha1(data string) string {
|
|||||||
return hex.EncodeToString(sha1.Sum([]byte("")))
|
return hex.EncodeToString(sha1.Sum([]byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sha1 return the sha1 value (SHA-1 hash algorithm) of string.
|
// Sha1 return the sha1 value (SHA-1 hash algorithm) of base64 string.
|
||||||
// Play: qvTGHdzF6KLavt4PO0gs2a6pQ00=
|
// Play: todo
|
||||||
func Sha1WithBase64(data string) string {
|
func Sha1WithBase64(data string) string {
|
||||||
sha1 := sha1.New()
|
sha1 := sha1.New()
|
||||||
sha1.Write([]byte(data))
|
sha1.Write([]byte(data))
|
||||||
@@ -176,7 +176,7 @@ func Sha256(data string) string {
|
|||||||
return hex.EncodeToString(sha256.Sum([]byte("")))
|
return hex.EncodeToString(sha256.Sum([]byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sha256 return the sha256 value (SHA256 hash algorithm) of string.
|
// Sha256 return the sha256 value (SHA256 hash algorithm) of base64 string.
|
||||||
// Play: https://go.dev/play/p/85IXJHIal1k
|
// Play: https://go.dev/play/p/85IXJHIal1k
|
||||||
func Sha256WithBase64(data string) string {
|
func Sha256WithBase64(data string) string {
|
||||||
sha256 := sha256.New()
|
sha256 := sha256.New()
|
||||||
@@ -192,7 +192,7 @@ func Sha512(data string) string {
|
|||||||
return hex.EncodeToString(sha512.Sum([]byte("")))
|
return hex.EncodeToString(sha512.Sum([]byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sha512 return the sha512 value (SHA512 hash algorithm) of string.
|
// Sha512 return the sha512 value (SHA512 hash algorithm) of base64 string.
|
||||||
// Play: https://go.dev/play/p/q_fY2rA-k5I
|
// Play: https://go.dev/play/p/q_fY2rA-k5I
|
||||||
func Sha512WithBase64(data string) string {
|
func Sha512WithBase64(data string) string {
|
||||||
sha512 := sha512.New()
|
sha512 := sha512.New()
|
||||||
|
|||||||
@@ -27,6 +27,13 @@ func TestMd5String(t *testing.T) {
|
|||||||
assert.Equal("5d41402abc4b2a76b9719d911017c592", Md5String("hello"))
|
assert.Equal("5d41402abc4b2a76b9719d911017c592", Md5String("hello"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMd5StringWithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestMd5StringWithBase64")
|
||||||
|
assert.Equal("XUFAKrxLKna5cZ2REBfFkg==", Md5StringWithBase64("hello"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestMd5Byte(t *testing.T) {
|
func TestMd5Byte(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -35,6 +42,13 @@ func TestMd5Byte(t *testing.T) {
|
|||||||
assert.Equal("0cc175b9c0f1b6a831c399e269772661", Md5Byte(data))
|
assert.Equal("0cc175b9c0f1b6a831c399e269772661", Md5Byte(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMd5ByteWithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestMd5ByteWithBase64")
|
||||||
|
assert.Equal("XUFAKrxLKna5cZ2REBfFkg==", Md5ByteWithBase64([]byte("hello")))
|
||||||
|
}
|
||||||
|
|
||||||
func TestMd5File(t *testing.T) {
|
func TestMd5File(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -66,15 +80,27 @@ func TestHmacSha1(t *testing.T) {
|
|||||||
func TestHmacSha256(t *testing.T) {
|
func TestHmacSha256(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
s := "hello world"
|
str := "hello world"
|
||||||
key := "12345"
|
key := "12345"
|
||||||
hmacSha256 := HmacSha256(s, key)
|
hmacSha256 := HmacSha256(str, key)
|
||||||
expected := "9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8"
|
expected := "9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8"
|
||||||
|
|
||||||
assert := internal.NewAssert(t, "TestHmacSha256")
|
assert := internal.NewAssert(t, "TestHmacSha256")
|
||||||
assert.Equal(expected, hmacSha256)
|
assert.Equal(expected, hmacSha256)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHmacSha256WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
hms := HmacSha256WithBase64(str, key)
|
||||||
|
expected := "MVu5PE6YmGK6Ccti4F1zpfN2yzbw14btqwwyDQWf3nU="
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestHmacSha256WithBase64")
|
||||||
|
assert.Equal(expected, hms)
|
||||||
|
}
|
||||||
|
|
||||||
func TestHmacSha512(t *testing.T) {
|
func TestHmacSha512(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -87,6 +113,18 @@ func TestHmacSha512(t *testing.T) {
|
|||||||
assert.Equal(expected, hmacSha512)
|
assert.Equal(expected, hmacSha512)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHmacSha512WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
hms := HmacSha512WithBase64(str, key)
|
||||||
|
expected := "3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A=="
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestHmacSha512WithBase64")
|
||||||
|
assert.Equal(expected, hms)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSha1(t *testing.T) {
|
func TestSha1(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -98,6 +136,16 @@ func TestSha1(t *testing.T) {
|
|||||||
assert.Equal(expected, sha1)
|
assert.Equal(expected, sha1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSha1WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
str := Sha1WithBase64("hello")
|
||||||
|
expected := "qvTGHdzF6KLavt4PO0gs2a6pQ00="
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestSha1WithBase64")
|
||||||
|
assert.Equal(expected, str)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSha256(t *testing.T) {
|
func TestSha256(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -109,6 +157,16 @@ func TestSha256(t *testing.T) {
|
|||||||
assert.Equal(expected, sha256)
|
assert.Equal(expected, sha256)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSha256WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
str := Sha256WithBase64("hello")
|
||||||
|
expected := "LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ="
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestSha256WithBase64")
|
||||||
|
assert.Equal(expected, str)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSha512(t *testing.T) {
|
func TestSha512(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -119,3 +177,13 @@ func TestSha512(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestSha512")
|
assert := internal.NewAssert(t, "TestSha512")
|
||||||
assert.Equal(expected, sha512)
|
assert.Equal(expected, sha512)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSha512WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
str := Sha512WithBase64("hello")
|
||||||
|
expected := "m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw=="
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestSha512WithBase64")
|
||||||
|
assert.Equal(expected, str)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user