From 989b4dd79178a9b337dd99932f40cb01f2f2b20e Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 25 Jul 2023 10:06:25 +0800 Subject: [PATCH] test: add test for cryptor package new functions --- cryptor/basic.go | 12 ++++---- cryptor/basic_test.go | 72 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/cryptor/basic.go b/cryptor/basic.go index b69b665..46049d8 100644 --- a/cryptor/basic.go +++ b/cryptor/basic.go @@ -144,7 +144,7 @@ func HmacSha512(data, key string) string { 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 func HmacSha512WithBase64(data, key string) string { h := hmac.New(sha512.New, []byte(key)) @@ -160,8 +160,8 @@ func Sha1(data string) string { return hex.EncodeToString(sha1.Sum([]byte(""))) } -// Sha1 return the sha1 value (SHA-1 hash algorithm) of string. -// Play: qvTGHdzF6KLavt4PO0gs2a6pQ00= +// Sha1 return the sha1 value (SHA-1 hash algorithm) of base64 string. +// Play: todo func Sha1WithBase64(data string) string { sha1 := sha1.New() sha1.Write([]byte(data)) @@ -176,7 +176,7 @@ func Sha256(data string) string { 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 func Sha256WithBase64(data string) string { sha256 := sha256.New() @@ -192,10 +192,10 @@ func Sha512(data string) string { 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 func Sha512WithBase64(data string) string { sha512 := sha512.New() sha512.Write([]byte(data)) return base64.StdEncoding.EncodeToString(sha512.Sum([]byte(""))) -} \ No newline at end of file +} diff --git a/cryptor/basic_test.go b/cryptor/basic_test.go index bc53c3d..9b8b624 100644 --- a/cryptor/basic_test.go +++ b/cryptor/basic_test.go @@ -27,6 +27,13 @@ func TestMd5String(t *testing.T) { 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) { t.Parallel() @@ -35,6 +42,13 @@ func TestMd5Byte(t *testing.T) { 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) { t.Parallel() @@ -66,15 +80,27 @@ func TestHmacSha1(t *testing.T) { func TestHmacSha256(t *testing.T) { t.Parallel() - s := "hello world" + str := "hello world" key := "12345" - hmacSha256 := HmacSha256(s, key) + hmacSha256 := HmacSha256(str, key) expected := "9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8" assert := internal.NewAssert(t, "TestHmacSha256") 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) { t.Parallel() @@ -87,6 +113,18 @@ func TestHmacSha512(t *testing.T) { 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) { t.Parallel() @@ -98,6 +136,16 @@ func TestSha1(t *testing.T) { 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) { t.Parallel() @@ -109,6 +157,16 @@ func TestSha256(t *testing.T) { 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) { t.Parallel() @@ -119,3 +177,13 @@ func TestSha512(t *testing.T) { assert := internal.NewAssert(t, "TestSha512") 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) +}