From 6e088f68bdd1756aa5d61af1046e6c3e82126e8f Mon Sep 17 00:00:00 2001 From: Yun Date: Mon, 3 Nov 2025 23:08:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- desx/desx_test.go | 94 +++++++++++++++++++++++++++++++++++++++++++++ hmacx/hmacx_test.go | 75 ++++++++++++++++++++++++++++++++++++ tdesx/tdesx_test.go | 94 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+) create mode 100644 desx/desx_test.go create mode 100644 hmacx/hmacx_test.go create mode 100644 tdesx/tdesx_test.go diff --git a/desx/desx_test.go b/desx/desx_test.go new file mode 100644 index 0000000..722633f --- /dev/null +++ b/desx/desx_test.go @@ -0,0 +1,94 @@ +package desx + +import ( + "testing" +) + +func TestEncryptECBHex(t *testing.T) { + // DES密钥需要8字节 + key := "12345678" + input := "hello world" + + encrypted, err := EncryptECBHex(input, key) + if err != nil { + t.Errorf("加密失败: %v", err) + return + } + + decrypted, err := DecryptECBHex(encrypted, key) + if err != nil { + t.Errorf("解密失败: %v", err) + return + } + + if decrypted != input { + t.Errorf("解密结果不匹配,期望: %s, 实际: %s", input, decrypted) + } +} + +func TestEncryptCBCHex(t *testing.T) { + // DES密钥需要8字节 + key := "12345678" + input := "hello world" + + encrypted, err := EncryptCBCHex(input, key) + if err != nil { + t.Errorf("CBC加密失败: %v", err) + return + } + + decrypted, err := DecryptCBCHex(encrypted, key) + if err != nil { + t.Errorf("CBC解密失败: %v", err) + return + } + + if decrypted != input { + t.Errorf("CBC解密结果不匹配,期望: %s, 实际: %s", input, decrypted) + } +} + +func TestEncryptCFBHex(t *testing.T) { + // DES密钥需要8字节 + key := "12345678" + input := "hello world" + + encrypted, err := EncryptCFBHex(input, key) + if err != nil { + t.Errorf("CFB加密失败: %v", err) + return + } + + decrypted, err := DecryptCFBHex(encrypted, key) + if err != nil { + t.Errorf("CFB解密失败: %v", err) + return + } + + if decrypted != input { + t.Errorf("CFB解密结果不匹配,期望: %s, 实际: %s", input, decrypted) + } +} + +func TestPKCS7Padding(t *testing.T) { + // 测试PKCS7填充和去除 + data := []byte("hello") + blockSize := 8 + + // 填充 + padded := PKCS7Padding(data, blockSize) + if len(padded) != blockSize { + t.Errorf("填充长度错误,期望: %d, 实际: %d", blockSize, len(padded)) + } + + // 去除填充 + unpadded, err := PKCS7UnPadding(padded) + if err != nil { + t.Errorf("去除填充失败: %v", err) + return + } + + if string(unpadded) != string(data) { + t.Errorf("去除填充后数据不匹配,期望: %s, 实际: %s", string(data), string(unpadded)) + } +} diff --git a/hmacx/hmacx_test.go b/hmacx/hmacx_test.go new file mode 100644 index 0000000..7566656 --- /dev/null +++ b/hmacx/hmacx_test.go @@ -0,0 +1,75 @@ +package hmacx + +import ( + "testing" +) + +func TestHMACSHA256(t *testing.T) { + message := "hello world" + key := "secret_key" + + // 计算HMAC-SHA256 + hmac1 := HMACSHA256(message, key) + + // 再次计算相同的消息和密钥 + hmac2 := HMACSHA256(message, key) + + // 相同的消息和密钥应该产生相同的HMAC + if hmac1 != hmac2 { + t.Errorf("相同输入产生了不同的HMAC: %s vs %s", hmac1, hmac2) + } + + // 不同的消息应该产生不同的HMAC + hmac3 := HMACSHA256("different message", key) + if hmac1 == hmac3 { + t.Errorf("不同消息产生了相同的HMAC") + } + + // 不同的密钥应该产生不同的HMAC + hmac4 := HMACSHA256(message, "different_key") + if hmac1 == hmac4 { + t.Errorf("不同密钥产生了相同的HMAC") + } +} + +func TestHMACSHA512(t *testing.T) { + message := "hello world" + key := "secret_key" + + // 计算HMAC-SHA512 + hmac1 := HMACSHA512(message, key) + + // 再次计算相同的消息和密钥 + hmac2 := HMACSHA512(message, key) + + // 相同的消息和密钥应该产生相同的HMAC + if hmac1 != hmac2 { + t.Errorf("相同输入产生了不同的HMAC: %s vs %s", hmac1, hmac2) + } + + // 不同的消息应该产生不同的HMAC + hmac3 := HMACSHA512("different message", key) + if hmac1 == hmac3 { + t.Errorf("不同消息产生了相同的HMAC") + } + + // 不同的密钥应该产生不同的HMAC + hmac4 := HMACSHA512(message, "different_key") + if hmac1 == hmac4 { + t.Errorf("不同密钥产生了相同的HMAC") + } +} + +func TestHMACSHA256VsSHA512(t *testing.T) { + message := "hello world" + key := "secret_key" + + // 计算HMAC-SHA256和HMAC-SHA512 + hmac256 := HMACSHA256(message, key) + hmac512 := HMACSHA512(message, key) + + // SHA256和SHA512应该产生不同的HMAC + if hmac256 == hmac512 { + t.Errorf("SHA256和SHA512产生了相同的HMAC") + } +} diff --git a/tdesx/tdesx_test.go b/tdesx/tdesx_test.go new file mode 100644 index 0000000..7a75c1f --- /dev/null +++ b/tdesx/tdesx_test.go @@ -0,0 +1,94 @@ +package tdesx + +import ( + "testing" +) + +func TestEncryptECBHex(t *testing.T) { + // 3DES密钥需要24字节 + key := "123456789012345678901234" + input := "hello world" + + encrypted, err := EncryptECBHex(input, key) + if err != nil { + t.Errorf("加密失败: %v", err) + return + } + + decrypted, err := DecryptECBHex(encrypted, key) + if err != nil { + t.Errorf("解密失败: %v", err) + return + } + + if decrypted != input { + t.Errorf("解密结果不匹配,期望: %s, 实际: %s", input, decrypted) + } +} + +func TestEncryptCBCHex(t *testing.T) { + // 3DES密钥需要24字节 + key := "123456789012345678901234" + input := "hello world" + + encrypted, err := EncryptCBCHex(input, key) + if err != nil { + t.Errorf("CBC加密失败: %v", err) + return + } + + decrypted, err := DecryptCBCHex(encrypted, key) + if err != nil { + t.Errorf("CBC解密失败: %v", err) + return + } + + if decrypted != input { + t.Errorf("CBC解密结果不匹配,期望: %s, 实际: %s", input, decrypted) + } +} + +func TestEncryptCFBHex(t *testing.T) { + // 3DES密钥需要24字节 + key := "123456789012345678901234" + input := "hello world" + + encrypted, err := EncryptCFBHex(input, key) + if err != nil { + t.Errorf("CFB加密失败: %v", err) + return + } + + decrypted, err := DecryptCFBHex(encrypted, key) + if err != nil { + t.Errorf("CFB解密失败: %v", err) + return + } + + if decrypted != input { + t.Errorf("CFB解密结果不匹配,期望: %s, 实际: %s", input, decrypted) + } +} + +func TestPKCS7Padding(t *testing.T) { + // 测试PKCS7填充和去除 + data := []byte("hello") + blockSize := 8 + + // 填充 + padded := PKCS7Padding(data, blockSize) + if len(padded) != blockSize { + t.Errorf("填充长度错误,期望: %d, 实际: %d", blockSize, len(padded)) + } + + // 去除填充 + unpadded, err := PKCS7UnPadding(padded) + if err != nil { + t.Errorf("去除填充失败: %v", err) + return + } + + if string(unpadded) != string(data) { + t.Errorf("去除填充后数据不匹配,期望: %s, 实际: %s", string(data), string(unpadded)) + } +}