Skip to content

Commit

Permalink
fix TestEncrypt_AuthCode
Browse files Browse the repository at this point in the history
  • Loading branch information
kakuilan committed May 6, 2024
1 parent cedfb74 commit c39aa5a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
4 changes: 4 additions & 0 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "你好,世界!"
Expand Down
2 changes: 1 addition & 1 deletion encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 动态密钥长度,相同的明文会生成不同密文就是依靠动态密钥
Expand Down
14 changes: 14 additions & 0 deletions encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c39aa5a

Please sign in to comment.