github.com/wangyougui/gf/v2@v2.6.5/crypto/gaes/gaes_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 // go test *.go -bench=".*" 8 9 package gaes_test 10 11 import ( 12 "testing" 13 14 "github.com/wangyougui/gf/v2/crypto/gaes" 15 "github.com/wangyougui/gf/v2/encoding/gbase64" 16 "github.com/wangyougui/gf/v2/test/gtest" 17 ) 18 19 var ( 20 content = []byte("pibigstar") 21 content_16, _ = gbase64.DecodeString("v1jqsGHId/H8onlVHR8Vaw==") 22 content_24, _ = gbase64.DecodeString("0TXOaj5KMoLhNWmJ3lxY1A==") 23 content_32, _ = gbase64.DecodeString("qM/Waw1kkWhrwzek24rCSA==") 24 content_16_iv, _ = gbase64.DecodeString("DqQUXiHgW/XFb6Qs98+hrA==") 25 content_32_iv, _ = gbase64.DecodeString("ZuLgAOii+lrD5KJoQ7yQ8Q==") 26 // iv 长度必须等于blockSize,只能为16 27 iv = []byte("Hello My GoFrame") 28 key_16 = []byte("1234567891234567") 29 key_17 = []byte("12345678912345670") 30 key_24 = []byte("123456789123456789123456") 31 key_32 = []byte("12345678912345678912345678912345") 32 keys = []byte("12345678912345678912345678912346") 33 key_err = []byte("1234") 34 key_32_err = []byte("1234567891234567891234567891234 ") 35 36 // cfb模式blockSize补位长度, add by zseeker 37 padding_size = 16 - len(content) 38 content_16_cfb, _ = gbase64.DecodeString("oSmget3aBDT1nJnBp8u6kA==") 39 ) 40 41 func TestEncrypt(t *testing.T) { 42 gtest.C(t, func(t *gtest.T) { 43 data, err := gaes.Encrypt(content, key_16) 44 t.AssertNil(err) 45 t.Assert(data, []byte(content_16)) 46 data, err = gaes.Encrypt(content, key_24) 47 t.AssertNil(err) 48 t.Assert(data, []byte(content_24)) 49 data, err = gaes.Encrypt(content, key_32) 50 t.AssertNil(err) 51 t.Assert(data, []byte(content_32)) 52 data, err = gaes.Encrypt(content, key_16, iv) 53 t.AssertNil(err) 54 t.Assert(data, []byte(content_16_iv)) 55 data, err = gaes.Encrypt(content, key_32, iv) 56 t.AssertNil(err) 57 t.Assert(data, []byte(content_32_iv)) 58 }) 59 } 60 61 func TestDecrypt(t *testing.T) { 62 gtest.C(t, func(t *gtest.T) { 63 decrypt, err := gaes.Decrypt([]byte(content_16), key_16) 64 t.AssertNil(err) 65 t.Assert(decrypt, content) 66 67 decrypt, err = gaes.Decrypt([]byte(content_24), key_24) 68 t.AssertNil(err) 69 t.Assert(decrypt, content) 70 71 decrypt, err = gaes.Decrypt([]byte(content_32), key_32) 72 t.AssertNil(err) 73 t.Assert(decrypt, content) 74 75 decrypt, err = gaes.Decrypt([]byte(content_16_iv), key_16, iv) 76 t.AssertNil(err) 77 t.Assert(decrypt, content) 78 79 decrypt, err = gaes.Decrypt([]byte(content_32_iv), key_32, iv) 80 t.AssertNil(err) 81 t.Assert(decrypt, content) 82 83 decrypt, err = gaes.Decrypt([]byte(content_32_iv), keys, iv) 84 t.Assert(err, "invalid unpadding") 85 }) 86 } 87 88 func TestEncryptErr(t *testing.T) { 89 gtest.C(t, func(t *gtest.T) { 90 // encrypt key error 91 _, err := gaes.Encrypt(content, key_err) 92 t.AssertNE(err, nil) 93 }) 94 } 95 96 func TestDecryptErr(t *testing.T) { 97 gtest.C(t, func(t *gtest.T) { 98 // decrypt key error 99 encrypt, err := gaes.Encrypt(content, key_16) 100 _, err = gaes.Decrypt(encrypt, key_err) 101 t.AssertNE(err, nil) 102 103 // decrypt content too short error 104 _, err = gaes.Decrypt([]byte("test"), key_16) 105 t.AssertNE(err, nil) 106 107 // decrypt content size error 108 _, err = gaes.Decrypt(key_17, key_16) 109 t.AssertNE(err, nil) 110 }) 111 } 112 113 func TestPKCS5UnPaddingErr(t *testing.T) { 114 gtest.C(t, func(t *gtest.T) { 115 // PKCS5UnPadding blockSize zero 116 _, err := gaes.PKCS5UnPadding(content, 0) 117 t.AssertNE(err, nil) 118 119 // PKCS5UnPadding src len zero 120 _, err = gaes.PKCS5UnPadding([]byte(""), 16) 121 t.AssertNE(err, nil) 122 123 // PKCS5UnPadding src len > blockSize 124 _, err = gaes.PKCS5UnPadding(key_17, 16) 125 t.AssertNE(err, nil) 126 127 // PKCS5UnPadding src len > blockSize 128 _, err = gaes.PKCS5UnPadding(key_32_err, 32) 129 t.AssertNE(err, nil) 130 }) 131 132 gtest.C(t, func(t *gtest.T) { 133 // PKCS7UnPadding blockSize zero 134 _, err := gaes.PKCS7UnPadding(content, 0) 135 t.AssertNE(err, nil) 136 137 // PKCS7UnPadding src len zero 138 _, err = gaes.PKCS7UnPadding([]byte(""), 16) 139 t.AssertNE(err, nil) 140 141 // PKCS7UnPadding src len > blockSize 142 _, err = gaes.PKCS7UnPadding(key_17, 16) 143 t.AssertNE(err, nil) 144 145 // PKCS7UnPadding src len > blockSize 146 _, err = gaes.PKCS7UnPadding(key_32_err, 32) 147 t.AssertNE(err, nil) 148 }) 149 } 150 151 func TestEncryptCFB(t *testing.T) { 152 gtest.C(t, func(t *gtest.T) { 153 var padding int = 0 154 data, err := gaes.EncryptCFB(content, key_16, &padding, iv) 155 t.AssertNil(err) 156 t.Assert(padding, padding_size) 157 t.Assert(data, []byte(content_16_cfb)) 158 }) 159 } 160 161 func TestDecryptCFB(t *testing.T) { 162 gtest.C(t, func(t *gtest.T) { 163 decrypt, err := gaes.DecryptCFB([]byte(content_16_cfb), key_16, padding_size, iv) 164 t.AssertNil(err) 165 t.Assert(decrypt, content) 166 }) 167 }