From 0456b65cc7688d5243acb54097e76282bc9b2d99 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 25 Jul 2023 10:18:54 +0800 Subject: [PATCH] test: add example for cryptor package new functions --- cryptor/basic_test.go | 12 ++++++ cryptor/crypto_example_test.go | 78 ++++++++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/cryptor/basic_test.go b/cryptor/basic_test.go index 9b8b624..add01e0 100644 --- a/cryptor/basic_test.go +++ b/cryptor/basic_test.go @@ -77,6 +77,18 @@ func TestHmacSha1(t *testing.T) { assert.Equal(expected, hmacSha1) } +func TestHmacSha1WithBase64(t *testing.T) { + t.Parallel() + + s := "hello" + key := "12345" + hmacSha1 := HmacSha1WithBase64(s, key) + expected := "XGqdsMzLkuNu0DI/0Jt/k23prOA=" + + assert := internal.NewAssert(t, "TestHmacSha1") + assert.Equal(expected, hmacSha1) +} + func TestHmacSha256(t *testing.T) { t.Parallel() diff --git a/cryptor/crypto_example_test.go b/cryptor/crypto_example_test.go index 4da1eda..745a325 100644 --- a/cryptor/crypto_example_test.go +++ b/cryptor/crypto_example_test.go @@ -322,7 +322,6 @@ func ExampleHmacMd5() { key := "12345" hms := HmacMd5(str, key) - fmt.Println(hms) // Output: @@ -334,37 +333,67 @@ func ExampleHmacSha1() { key := "12345" hms := HmacSha1(str, key) - fmt.Println(hms) // Output: // 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0 } +func ExampleHmacSha1WithBase64() { + str := "hello" + key := "12345" + + hms := HmacSha1WithBase64(str, key) + fmt.Println(hms) + + // Output: + // XGqdsMzLkuNu0DI/0Jt/k23prOA= +} + func ExampleHmacSha256() { str := "hello" key := "12345" hms := HmacSha256(str, key) - fmt.Println(hms) // Output: // 315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75 } +func ExampleHmacSha256WithBase64() { + str := "hello" + key := "12345" + + hms := HmacSha256WithBase64(str, key) + fmt.Println(hms) + + // Output: + // MVu5PE6YmGK6Ccti4F1zpfN2yzbw14btqwwyDQWf3nU= +} + func ExampleHmacSha512() { str := "hello" key := "12345" hms := HmacSha512(str, key) - fmt.Println(hms) // Output: // dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc } +func ExampleHmacSha512WithBase64() { + str := "hello" + key := "12345" + + hms := HmacSha512WithBase64(str, key) + fmt.Println(hms) + + // Output: + // 3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A== +} + func ExampleMd5String() { md5Str := Md5String("hello") fmt.Println(md5Str) @@ -373,6 +402,14 @@ func ExampleMd5String() { // 5d41402abc4b2a76b9719d911017c592 } +func ExampleMd5StringWithBase64() { + md5Str := Md5StringWithBase64("hello") + fmt.Println(md5Str) + + // Output: + // XUFAKrxLKna5cZ2REBfFkg== +} + func ExampleMd5Byte() { md5Str := Md5Byte([]byte{'a'}) fmt.Println(md5Str) @@ -380,6 +417,15 @@ func ExampleMd5Byte() { // Output: // 0cc175b9c0f1b6a831c399e269772661 } + +func ExampleMd5ByteWithBase64() { + md5Str := Md5ByteWithBase64([]byte("hello")) + fmt.Println(md5Str) + + // Output: + // XUFAKrxLKna5cZ2REBfFkg== +} + func ExampleSha1() { result := Sha1("hello") fmt.Println(result) @@ -388,6 +434,14 @@ func ExampleSha1() { // aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d } +func ExampleSha1WithBase64() { + result := Sha1WithBase64("hello") + fmt.Println(result) + + // Output: + // qvTGHdzF6KLavt4PO0gs2a6pQ00= +} + func ExampleSha256() { result := Sha256("hello") fmt.Println(result) @@ -396,6 +450,14 @@ func ExampleSha256() { // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 } +func ExampleSha256WithBase64() { + result := Sha256WithBase64("hello") + fmt.Println(result) + + // Output: + // LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ= +} + func ExampleSha512() { result := Sha512("hello") fmt.Println(result) @@ -403,3 +465,11 @@ func ExampleSha512() { // Output: // 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043 } + +func ExampleSha512WithBase64() { + result := Sha512WithBase64("hello") + fmt.Println(result) + + // Output: + // m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw== +}