gf/crypto/gaes/gaes_test.go

151 lines
4.2 KiB
Go
Raw Normal View History

2019-04-05 17:11:03 +08:00
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*"
package gaes_test
import (
"testing"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/encoding/gbase64"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/crypto/gaes"
"github.com/gogf/gf/test/gtest"
2019-04-05 17:11:03 +08:00
)
var (
content = []byte("pibigstar")
content_16, _ = gbase64.DecodeString("v1jqsGHId/H8onlVHR8Vaw==")
content_24, _ = gbase64.DecodeString("0TXOaj5KMoLhNWmJ3lxY1A==")
content_32, _ = gbase64.DecodeString("qM/Waw1kkWhrwzek24rCSA==")
content_16_iv, _ = gbase64.DecodeString("DqQUXiHgW/XFb6Qs98+hrA==")
content_32_iv, _ = gbase64.DecodeString("ZuLgAOii+lrD5KJoQ7yQ8Q==")
2019-04-05 17:11:03 +08:00
// iv 长度必须等于blockSize只能为16
2019-06-10 17:26:32 +08:00
iv = []byte("Hello My GoFrame")
key_16 = []byte("1234567891234567")
key_17 = []byte("12345678912345670")
key_24 = []byte("123456789123456789123456")
key_32 = []byte("12345678912345678912345678912345")
keys = []byte("12345678912345678912345678912346")
key_err = []byte("1234")
key_32_err = []byte("1234567891234567891234567891234 ")
// cfb模式blockSize补位长度, add by zseeker
padding_size = 16 - len(content)
content_16_cfb, _ = gbase64.DecodeString("oSmget3aBDT1nJnBp8u6kA==")
2019-04-05 17:11:03 +08:00
)
func TestEncrypt(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
data, err := gaes.Encrypt(content, key_16)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(data, []byte(content_16))
data, err = gaes.Encrypt(content, key_24)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(data, []byte(content_24))
data, err = gaes.Encrypt(content, key_32)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(data, []byte(content_32))
data, err = gaes.Encrypt(content, key_16, iv)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(data, []byte(content_16_iv))
data, err = gaes.Encrypt(content, key_32, iv)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(data, []byte(content_32_iv))
})
}
func TestDecrypt(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
decrypt, err := gaes.Decrypt([]byte(content_16), key_16)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(decrypt, content)
decrypt, err = gaes.Decrypt([]byte(content_24), key_24)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(decrypt, content)
decrypt, err = gaes.Decrypt([]byte(content_32), key_32)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(decrypt, content)
decrypt, err = gaes.Decrypt([]byte(content_16_iv), key_16, iv)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(decrypt, content)
decrypt, err = gaes.Decrypt([]byte(content_32_iv), key_32, iv)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(decrypt, content)
decrypt, err = gaes.Decrypt([]byte(content_32_iv), keys, iv)
2020-03-19 22:56:12 +08:00
t.Assert(err, "invalid padding")
2019-04-05 17:11:03 +08:00
})
}
2019-06-10 17:26:32 +08:00
func TestEncryptErr(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 17:26:32 +08:00
// encrypt key error
_, err := gaes.Encrypt(content, key_err)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-10 17:26:32 +08:00
})
}
func TestDecryptErr(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 17:26:32 +08:00
// decrypt key error
encrypt, err := gaes.Encrypt(content, key_16)
_, err = gaes.Decrypt(encrypt, key_err)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-10 17:26:32 +08:00
// decrypt content too short error
_, err = gaes.Decrypt([]byte("test"), key_16)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-10 17:26:32 +08:00
// decrypt content size error
_, err = gaes.Decrypt(key_17, key_16)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-10 17:26:32 +08:00
})
}
func TestPKCS5UnPaddingErr(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-10 17:26:32 +08:00
// PKCS5UnPadding blockSize zero
_, err := gaes.PKCS5UnPadding(content, 0)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-10 17:26:32 +08:00
// PKCS5UnPadding src len zero
_, err = gaes.PKCS5UnPadding([]byte(""), 16)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-10 17:26:32 +08:00
// PKCS5UnPadding src len > blockSize
_, err = gaes.PKCS5UnPadding(key_17, 16)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-10 17:26:32 +08:00
// PKCS5UnPadding src len > blockSize
_, err = gaes.PKCS5UnPadding(key_32_err, 32)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-10 17:26:32 +08:00
})
}
func TestEncryptCFB(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
var padding int = 0
data, err := gaes.EncryptCFB(content, key_16, &padding, iv)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(padding, padding_size)
t.Assert(data, []byte(content_16_cfb))
})
}
func TestDecryptCFB(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
decrypt, err := gaes.DecryptCFB([]byte(content_16_cfb), key_16, padding_size, iv)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(decrypt, content)
})
}