1 Star 0 Fork 50

dtbsky/dongle

forked from golang-module/dongle 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
aes_cbc_test.go 10.55 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2022-05-30 08:43 . v0.1.1 统一单元测试格式
package dongle
import (
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)
var (
aesCbcInput = "hello world"
aesCbcKey = "1234567887654321"
aesCbcIV = "1234567887654321"
// AES-CBC-NoPadding
aesCbcNoHexExpected = "fb5baf6e799b4da405d63b1ed3e14d62"
aesCbcNoBase32Expected = "7NN263TZTNG2IBOWHMPNHYKNMI======"
aesCbcNoBase64Expected = "+1uvbnmbTaQF1jse0+FNYg=="
// AES-CBC-ZeroPadding
aesCbcZeroHexExpected = "5eb15d89da69f77372ee8d9bd02b0252"
aesCbcZeroBase32Expected = "L2YV3CO2NH3XG4XORWN5AKYCKI======"
aesCbcZeroBase64Expected = "XrFdidpp93Ny7o2b0CsCUg=="
// AES-CBC-PKCS5Padding
aesCbcPkcs5HexExpected = "65d823bdf1c581a1ded1cba42e03ca52"
aesCbcPkcs5Base32Expected = "MXMCHPPRYWA2DXWRZOSC4A6KKI======"
aesCbcPkcs5Base64Expected = "ZdgjvfHFgaHe0cukLgPKUg=="
// AES-CBC-PKCS7Padding
aesCbcPkcs7HexExpected = "65d823bdf1c581a1ded1cba42e03ca52"
aesCbcPkcs7Base32Expected = "MXMCHPPRYWA2DXWRZOSC4A6KKI======"
aesCbcPkcs7Base64Expected = "ZdgjvfHFgaHe0cukLgPKUg=="
)
func aesCbcCipher(padding string) *Cipher {
cipher := NewCipher()
cipher.SetMode(CBC)
cipher.SetPadding(padding)
cipher.SetKey(aesCbcKey)
cipher.SetIV(aesCbcIV)
return cipher
}
func TestEncrypt_ByAes_CBC_NoPadding(t *testing.T) {
input := "12345678asdfghjk"
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{input, aesCbcNoHexExpected},
}
for index, test := range hexTests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(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, aesCbcNoBase32Expected},
}
for index, test := range base32Tests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(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, aesCbcNoBase64Expected},
}
for index, test := range base64Tests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(No))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase64String(), "Current test index is "+strconv.Itoa(index))
}
}
func TestDecrypt_ByAes_CBC_NoPadding(t *testing.T) {
input := "12345678asdfghjk"
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCbcNoHexExpected, input},
}
for index, test := range hexTests {
e := Decrypt.FromHexString(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcNoBase32Expected, input},
}
for index, test := range base32Tests {
e := Decrypt.FromBase32String(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcNoBase64Expected, input},
}
for index, test := range base64Tests {
e := Decrypt.FromBase64String(test.input).ByAes(aesCbcCipher(No))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestEncrypt_ByAes_CBC_ZeroPadding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCbcInput, aesCbcZeroHexExpected},
}
for index, test := range hexTests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcInput, aesCbcZeroBase32Expected},
}
for index, test := range base32Tests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcInput, aesCbcZeroBase64Expected},
}
for index, test := range base64Tests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(Zero))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase64String(), "Current test index is "+strconv.Itoa(index))
}
}
func TestDecrypt_ByAes_CBC_ZeroPadding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCbcZeroHexExpected, aesCbcInput},
}
for index, test := range hexTests {
e := Decrypt.FromHexString(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcZeroBase32Expected, aesCbcInput},
}
for index, test := range base32Tests {
e := Decrypt.FromBase32String(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcZeroBase64Expected, aesCbcInput},
}
for index, test := range base64Tests {
e := Decrypt.FromBase64String(test.input).ByAes(aesCbcCipher(Zero))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestEncrypt_ByAes_CBC_PKCS5Padding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCbcInput, aesCbcPkcs5HexExpected},
}
for index, test := range hexTests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcInput, aesCbcPkcs5Base32Expected},
}
for index, test := range base32Tests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcInput, aesCbcPkcs5Base64Expected},
}
for index, test := range base64Tests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(PKCS5))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase64String(), "Current test index is "+strconv.Itoa(index))
}
}
func TestDecrypt_ByAes_CBC_PKCS5Padding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCbcPkcs5HexExpected, aesCbcInput},
}
for index, test := range hexTests {
e := Decrypt.FromHexString(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcPkcs5Base32Expected, aesCbcInput},
}
for index, test := range base32Tests {
e := Decrypt.FromBase32String(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcPkcs5Base64Expected, aesCbcInput},
}
for index, test := range base64Tests {
e := Decrypt.FromBase64String(test.input).ByAes(aesCbcCipher(PKCS5))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestEncrypt_ByAes_CBC_PKCS7Padding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCbcInput, aesCbcPkcs7HexExpected},
}
for index, test := range hexTests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcInput, aesCbcPkcs7Base32Expected},
}
for index, test := range base32Tests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcInput, aesCbcPkcs7Base64Expected},
}
for index, test := range base64Tests {
e := Encrypt.FromString(test.input).ByAes(aesCbcCipher(PKCS7))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToBase64String(), "Current test index is "+strconv.Itoa(index))
}
}
func TestDecrypt_ByAes_CBC_PKCS7Padding(t *testing.T) {
hexTests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{aesCbcPkcs7HexExpected, aesCbcInput},
}
for index, test := range hexTests {
e := Decrypt.FromHexString(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcPkcs7Base32Expected, aesCbcInput},
}
for index, test := range base32Tests {
e := Decrypt.FromBase32String(test.input).ByAes(aesCbcCipher(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 // 期望值
}{
{"", ""},
{aesCbcPkcs7Base64Expected, aesCbcInput},
}
for index, test := range base64Tests {
e := Decrypt.FromBase64String(test.input).ByAes(aesCbcCipher(PKCS7))
assert.Nil(t, e.Error)
assert.Equal(t, test.expected, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/daoker/dongle.git
git@gitee.com:daoker/dongle.git
daoker
dongle
dongle
master

搜索帮助