@@ -2,6 +2,8 @@ local crypto = require("crypto")
22local assert = require ' assert'
33local hex = require ' hex'
44local require = require ' require'
5+ local filepath = require ' filepath'
6+ local ioutil = require ' ioutil'
57
68function TestMD5 (t )
79 local tests = {
@@ -212,4 +214,59 @@ function TestAESEncrypt(t)
212214 got = hex .encode_to_string (got )
213215 assert :Equal (t , tt .err , err )
214216 end
217+ end
218+
219+ function TestAESDecrypt (t )
220+ tests = {
221+ {
222+ data = " 138434a80bd7dcd9ee8adc" ,
223+ mode = " CTR" ,
224+ key = " 86e15cbc1cbf510d8f2e51d4b63a2144" ,
225+ init = " e3057fc2bf103a09a1b2c3d4e5f60718" ,
226+ expected = " 48656c6c6f20776f726c64" , -- "Hello world" in hex
227+ wantErr = false ,
228+ },
229+ }
230+ for _ , tt in ipairs (tests ) do
231+ t :Run (" aes_decrypt in " .. tostring (tt .mode ) .. " mode" , function (t )
232+ local key , err = hex .decode_string (tt .key )
233+ require :NoError (t , err )
234+ local init , err = hex .decode_string (tt .init )
235+ require :NoError (t , err )
236+ local data , err = hex .decode_string (tt .data )
237+ require :NoError (t , err )
238+ local got , err = crypto .aes_decrypt (tt .mode , key , init , data )
239+ if tt .wantErr then
240+ require :Error (t , err )
241+ return
242+ end
243+ require :NoError (t , err )
244+ got , err = hex .encode_to_string (got )
245+ require :NoError (t , err )
246+ assert :Equal (t , tt .expected , got )
247+ end )
248+ end
249+ end
250+
251+ function TestAESCodecFile (t )
252+ for i = 1 , 1 do
253+ local data , err = ioutil .read_file (filepath .join (" test/data" , tostring (i ) .. " .data.bin" ))
254+ require :NoError (t , err )
255+ local expected , err = ioutil .read_file (filepath .join (" test/data" , tostring (i ) .. " .expected.bin" ))
256+ require :NoError (t , err )
257+ local init , err = ioutil .read_file (filepath .join (" test/data" , tostring (i ) .. " .init.bin" ))
258+ require :NoError (t , err )
259+ local key , err = ioutil .read_file (filepath .join (" test/data" , tostring (i ) .. " .key.bin" ))
260+ require :NoError (t , err )
261+ t :Run (" TestAESEncryptFile " .. tostring (i ), function (t )
262+ local got , err = crypto .aes_encrypt (" CTR" , key , init , data )
263+ require :NoError (t , err )
264+ assert :Equal (t , expected , got )
265+
266+ local decrypted , err = crypto .aes_decrypt (" CTR" , key , init , got )
267+ t :Logf (' data: "%s", decrypted: "%s"' , data , decrypted )
268+ require :NoError (t , err )
269+ assert :Equal (t , data , decrypted )
270+ end )
271+ end
215272end
0 commit comments