diff --git a/data_test.go b/data_test.go index e3c8cea..54555d6 100644 --- a/data_test.go +++ b/data_test.go @@ -129,6 +129,10 @@ var cmplNum2 = complex(3, 4) // 字符串 var strHello = "Hello World! 你好!" +var strHello2 = "Hello World! 你好,世界!" +var strHelloEmoji = "Hello World! 你好,世界!안녕, 세계! Olá mundo,With Emojis:😃🐳📜💯⌚" +var strJson = `{"id":9999,"url":"https://baidu.com"}` +var strEmptyMd5 = "d41d8cd98f00b204e9800998ecf8427e" var b64Hello = "SGVsbG8gV29ybGQhIOS9oOWlve+8gQ==" var strHelloHex = "48656c6c6f20576f726c642120e4bda0e5a5bdefbc81" //strHello的16进制 var utf8Hello = "你好,世界!" diff --git a/encrypt.go b/encrypt.go index 6c55686..d175238 100644 --- a/encrypt.go +++ b/encrypt.go @@ -84,7 +84,7 @@ func (ke *LkkEncrypt) Base64UrlDecode(str []byte) ([]byte, error) { // AuthCode 授权码编码或解码; // encode为true时编码,为false解码; -// expiry为加密时的有效期,单位秒,为0时代表永久(100年); +// expiry为加密时的有效期,单位秒,0为永久(100年),负数为已过期; // 返回结果为加密/解密的字符串和有效期时间戳. func (ke *LkkEncrypt) AuthCode(str, key []byte, encode bool, expiry int64) ([]byte, int64) { // DYNAMIC_KEY_LEN 动态密钥长度,相同的明文会生成不同密文就是依靠动态密钥 diff --git a/encrypt_test.go b/encrypt_test.go index 8ff5e13..4734c71 100644 --- a/encrypt_test.go +++ b/encrypt_test.go @@ -117,6 +117,20 @@ func TestEncrypt_AuthCode(t *testing.T) { res2, exp = KEncr.AuthCode([]byte(strSha512), bytSpeedLight, false, 0) assert.Empty(t, res2) + //其他 + str1 := []byte("8c9eb7905a6SdXZfm-GoJpYKu6CzMgF0I-7neF-x3UKIUpYuIZSnK_2ZqaYSZlZw0Ofzwa2Bn0QZ6b4SLzSz") + str2 := []byte("b42374af3DqX22zi207OJXsz6xP2vEXto39TPK_UzcJOdDZV0kQHPUFm5JOw-aWISFi0snglsrYtp5tpYGRuhgw50TPY8UnFSf912uZI38vGON0KHqAgCatmtdoBZ4VJI6IkHio-JLxbt8hkuCz1HCOElUkZxBMnGUle") + str3 := []byte("52a0945eK4NyxvnjEBnPlToROzO4KLKE9VvrqtxAiLPVPDK-HkvzahyMbxydmSifc3TQIo4mbsi9gzq7vbJ64YzpB_DP") + + res2, exp = KEncr.AuthCode(str1, []byte(strEmptyMd5), false, 0) + assert.Equal(t, string(res2), strHello2) + + res2, exp = KEncr.AuthCode(str2, []byte(strEmptyMd5), false, 0) + assert.Equal(t, string(res2), strHelloEmoji) + + res2, exp = KEncr.AuthCode(str3, []byte(strEmptyMd5[0:16]), false, 0) + assert.Equal(t, string(res2), strJson) + //不合法 KEncr.AuthCode([]byte("7caeNfPt/N1zHdj5k/7i7pol6NHsVs0Cji7c15h4by1RYcrBoo7EEw=="), bytSpeedLight, false, 0) KEncr.AuthCode([]byte("7caeNfPt/N1zHdj5k/7i7pol6N"), bytSpeedLight, false, 0) diff --git a/string_test.go b/string_test.go index c63c93c..322cd9c 100644 --- a/string_test.go +++ b/string_test.go @@ -26,13 +26,15 @@ func TestString_Md5Byte_Md5_IsMd5(t *testing.T) { res2 = KStr.Md5Byte(bytsHello) res4 = KStr.Md5(strHello) assert.Equal(t, string(res2), res4) + chk = KStr.IsMd5(res4) + assert.True(t, chk) + + res3 = KStr.Md5("") + assert.Equal(t, res3, strEmptyMd5) res3 = KStr.Md5(strHello, 16) chk = KStr.IsMd5(res3) assert.False(t, chk) - - chk = KStr.IsMd5(res4) - assert.True(t, chk) } func BenchmarkString_Md5Byte(b *testing.B) {