完善测试用例
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user