加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
aes_cfb_test.go 11.93 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2022-05-30 08:43 . v0.1.1 统一单元测试格式
package dongle
import (
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)
var (
aesCfbInput = "hello world"
aesCfbKey = "1234567887654321"
aesCfbIV = "1234567887654321"
// AES-CFB-NoPadding
aesCfbNoHexExpected = "66ec296f79f7f2f6015df0c9ea601e6e"
// aesCfbNoHexExpected = "66ec296f79f7f2f6015df0c9ea601e6e" // https://oktools.net/aes
// aesCfbNoHexExpected = "6609f5b6c7fdb4445152ab944169a6aa" // http://tool.chacuo.net/cryptaes
aesCfbNoBase32Expected = "M3WCS33Z67ZPMAK56DE6UYA6NY======"
aesCfbNoBase64Expected = "Zuwpb3n38vYBXfDJ6mAebg=="
// AES-CFB-ZeroPadding
aesCfbZeroHexExpected = "3fbb763723e1b2a11242f0af8d087405"
// aesCfbZeroHexExpected = "3fbb763723e1b2a11242f0af8d087405" // https://oktools.net/aes
// aesCfbZeroHexExpected = "3fbb763723e1b2a11242f0af8d087405" // https://tool.lmeee.com/jiami/aes
// aesCfbZeroHexExpected = "3fffdc1a33e646a965a0c5c1debd93e0" // http://tool.chacuo.net/cryptaes
// aesCfbZeroHexExpected = "3fffdc1a33e646a965a0c5c1debd93e0" // https://www.ssleye.com/ssltool/aes_cipher.html
aesCfbZeroBase32Expected = "H65XMNZD4GZKCESC6CXY2CDUAU======"
aesCfbZeroBase64Expected = "P7t2NyPhsqESQvCvjQh0BQ=="
// AES-CFB-PKCS5Padding
aesCfbPkcs5HexExpected = "3fbb763723e1b2a11242f0aa880d7100"
// aesCfbPkcs5HexExpected = "3fbb763723e1b2a11242f0aa880d7100" // https://oktools.net/aes
// aesCfbPkcs5HexExpected = "3fbb763723e1b2a11242f0aa880d7100" // https://oktools.net/aes
// aesCfbPkcs5HexExpected = "3fffdc1a33e646a965a0c5c43b178429" // https://tool.lmeee.com/jiami/aes
// aesCfbPkcs5HexExpected = "3fffdc1a33e646a965a0c5c43b178429" // https://www.ssleye.com/ssltool/aes_cipher.html
aesCfbPkcs5Base32Expected = "H65XMNZD4GZKCESC6CVIQDLRAA======"
aesCfbPkcs5Base64Expected = "P7t2NyPhsqESQvCqiA1xAA=="
// AES-CFB-PKCS7Padding
aesCfbPkcs7HexExpected = "3fbb763723e1b2a11242f0aa880d7100"
// aesCfbPkcs7HexExpected = "3fbb763723e1b2a11242f0aa880d7100" // https://oktools.net/aes
// aesCfbPkcs7HexExpected = "3fbb763723e1b2a11242f0aa880d7100" // https://tool.lmeee.com/jiami/aes
// aesCfbPkcs7HexExpected = "3fffdc1a33e646a965a0c5c43b178429" // http://tool.chacuo.net/cryptaes
// aesCfbPkcs7HexExpected = "3fffdc1a33e646a965a0c5c43b178429" // https://www.ssleye.com/ssltool/aes_cipher.html
aesCfbPkcs7Base32Expected = "H65XMNZD4GZKCESC6CVIQDLRAA======"
aesCfbPkcs7Base64Expected = "P7t2NyPhsqESQvCqiA1xAA=="
)
func aesCfbCipher(padding string) *Cipher {
cipher := NewCipher()
cipher.SetMode(CFB)
cipher.SetPadding(padding)
cipher.SetKey(aesCfbKey)
cipher.SetIV(aesCfbIV)
return cipher
}
func TestEncrypt_ByAes_CFB_NoPadding(t *testing.T) {
input := "12345678asdfghjk"
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{input, aesCfbNoHexExpected},
}
for index, test := range hexTests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(No))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToHexString(), "Current test index is "+strconv.Itoa(index))
}
base32Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{input, aesCfbNoBase32Expected},
}
for index, test := range base32Tests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(No))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase32String(), "Current test index is "+strconv.Itoa(index))
}
base64Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{input, aesCfbNoBase64Expected},
}
for index, test := range base64Tests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(No))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase64String(), "Current test index is "+strconv.Itoa(index))
}
}
func TestDecrypt_ByAes_CFB_NoPadding(t *testing.T) {
input := "12345678asdfghjk"
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbNoHexExpected, input},
}
for index, test := range hexTests {
e := Decrypt.FromHexString(test.input).ByAes(aesCfbCipher(No))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
base32Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbNoBase32Expected, input},
}
for index, test := range base32Tests {
e := Decrypt.FromBase32String(test.input).ByAes(aesCfbCipher(No))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
base64Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbNoBase64Expected, input},
}
for index, test := range base64Tests {
e := Decrypt.FromBase64String(test.input).ByAes(aesCfbCipher(No))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestEncrypt_ByAes_CFB_ZeroPadding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbInput, aesCfbZeroHexExpected},
}
for index, test := range hexTests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(Zero))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToHexString(), "Current test index is "+strconv.Itoa(index))
}
base32Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbInput, aesCfbZeroBase32Expected},
}
for index, test := range base32Tests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(Zero))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase32String(), "Current test index is "+strconv.Itoa(index))
}
base64Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbInput, aesCfbZeroBase64Expected},
}
for index, test := range base64Tests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(Zero))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase64String(), "Current test index is "+strconv.Itoa(index))
}
}
func TestDecrypt_ByAes_CFB_ZeroPadding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbZeroHexExpected, aesCfbInput},
}
for index, test := range hexTests {
e := Decrypt.FromHexString(test.input).ByAes(aesCfbCipher(Zero))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
base32Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbZeroBase32Expected, aesCfbInput},
}
for index, test := range base32Tests {
e := Decrypt.FromBase32String(test.input).ByAes(aesCfbCipher(Zero))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
base64Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbZeroBase64Expected, aesCfbInput},
}
for index, test := range base64Tests {
e := Decrypt.FromBase64String(test.input).ByAes(aesCfbCipher(Zero))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestEncrypt_ByAes_CFB_PKCS5Padding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbInput, aesCfbPkcs5HexExpected},
}
for index, test := range hexTests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(PKCS5))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToHexString(), "Current test index is "+strconv.Itoa(index))
}
base32Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbInput, aesCfbPkcs5Base32Expected},
}
for index, test := range base32Tests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(PKCS5))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase32String(), "Current test index is "+strconv.Itoa(index))
}
base64Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbInput, aesCfbPkcs5Base64Expected},
}
for index, test := range base64Tests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(PKCS5))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase64String(), "Current test index is "+strconv.Itoa(index))
}
}
func TestDecrypt_ByAes_CFB_PKCS5Padding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbPkcs5HexExpected, aesCfbInput},
}
for index, test := range hexTests {
e := Decrypt.FromHexString(test.input).ByAes(aesCfbCipher(PKCS5))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
base32Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbPkcs5Base32Expected, aesCfbInput},
}
for index, test := range base32Tests {
e := Decrypt.FromBase32String(test.input).ByAes(aesCfbCipher(PKCS5))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
base64Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbPkcs5Base64Expected, aesCfbInput},
}
for index, test := range base64Tests {
e := Decrypt.FromBase64String(test.input).ByAes(aesCfbCipher(PKCS5))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestEncrypt_ByAes_CFB_PKCS7Padding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbInput, aesCfbPkcs7HexExpected},
}
for index, test := range hexTests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(PKCS7))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToHexString(), "Current test index is "+strconv.Itoa(index))
}
base32Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbInput, aesCfbPkcs7Base32Expected},
}
for index, test := range base32Tests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(PKCS7))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase32String(), "Current test index is "+strconv.Itoa(index))
}
base64Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbInput, aesCfbPkcs7Base64Expected},
}
for index, test := range base64Tests {
e := Encrypt.FromString(test.input).ByAes(aesCfbCipher(PKCS7))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase64String(), "Current test index is "+strconv.Itoa(index))
}
}
func TestDecrypt_ByAes_CFB_PKCS7Padding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbPkcs7HexExpected, aesCfbInput},
}
for index, test := range hexTests {
e := Decrypt.FromHexString(test.input).ByAes(aesCfbCipher(PKCS7))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
base32Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbPkcs7Base32Expected, aesCfbInput},
}
for index, test := range base32Tests {
e := Decrypt.FromBase32String(test.input).ByAes(aesCfbCipher(PKCS7))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
base64Tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCfbPkcs7Base64Expected, aesCfbInput},
}
for index, test := range base64Tests {
e := Decrypt.FromBase64String(test.input).ByAes(aesCfbCipher(PKCS7))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化