Skip to content

Commit aeda23c

Browse files
committed
hex use of aes.
1 parent e1e1904 commit aeda23c

File tree

5 files changed

+89
-15
lines changed

5 files changed

+89
-15
lines changed

crypto/api.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ func SHA256(L *lua.LState) int {
2828

2929
// AESEncrypt implements AES encryption in Lua.
3030
func AESEncrypt(l *lua.LState) int {
31+
modeStr := l.CheckString(1)
32+
m, err := parseString(modeStr)
33+
if err != nil {
34+
l.ArgError(1, err.Error())
35+
}
36+
key := []byte(l.CheckString(2))
37+
iv := []byte(l.CheckString(3))
38+
data := []byte(l.CheckString(4))
39+
enc, err := encryptAES(m, key, iv, data)
40+
if err != nil {
41+
l.Push(lua.LNil)
42+
l.Push(lua.LString(fmt.Sprintf("failed to encrypt: %v", err)))
43+
return 2
44+
}
45+
l.Push(lua.LString(enc))
46+
return 1
47+
}
48+
49+
// AESEncryptHex implements AES encryption in Lua.
50+
func AESEncryptHex(l *lua.LState) int {
3151
m, key, iv, data, err := decodeParams(l)
3252
if err != nil {
3353
l.Push(lua.LNil)
@@ -47,6 +67,26 @@ func AESEncrypt(l *lua.LState) int {
4767

4868
// AESDecrypt implement AES decryption in Lua.
4969
func AESDecrypt(l *lua.LState) int {
70+
modeStr := l.CheckString(1)
71+
m, err := parseString(modeStr)
72+
if err != nil {
73+
l.ArgError(1, err.Error())
74+
}
75+
key := []byte(l.CheckString(2))
76+
iv := []byte(l.CheckString(3))
77+
data := []byte(l.CheckString(4))
78+
dec, err := decryptAES(m, key, iv, data)
79+
if err != nil {
80+
l.Push(lua.LNil)
81+
l.Push(lua.LString(fmt.Sprintf("failed to decrypt: %v", err)))
82+
return 2
83+
}
84+
l.Push(lua.LString(dec))
85+
return 1
86+
}
87+
88+
// AESDecryptHex implement AES decryption in Lua.
89+
func AESDecryptHex(l *lua.LState) int {
5090
m, key, iv, data, err := decodeParams(l)
5191
if err != nil {
5292
l.Push(lua.LNil)

crypto/api_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"github.com/vadv/gopher-lua-libs/hex"
78
"github.com/vadv/gopher-lua-libs/tests"
89
)
910

1011
func TestApi(t *testing.T) {
11-
preload := tests.SeveralPreloadFuncs(Preload)
12+
preload := tests.SeveralPreloadFuncs(Preload, hex.Preload)
1213
assert.NotZero(t, tests.RunLuaTestFile(t, preload, "./test/test_api.lua"))
1314
}

crypto/loader.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ func Loader(L *lua.LState) int {
1919
}
2020

2121
var api = map[string]lua.LGFunction{
22-
"md5": MD5,
23-
"sha256": SHA256,
24-
"aes_encrypt": AESEncrypt,
25-
"aes_decrypt": AESDecrypt,
22+
"md5": MD5,
23+
"sha256": SHA256,
24+
"aes_encrypt_hex": AESEncryptHex,
25+
"aes_decrypt_hex": AESDecryptHex,
26+
"aes_encrypt": AESEncrypt,
27+
"aes_decrypt": AESDecrypt,
2628
}

crypto/test/test_api.lua

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local crypto = require("crypto")
22
local assert = require 'assert'
3+
local hex = require 'hex'
4+
local require = require 'require'
35

46
function TestMD5(t)
57
local tests = {
@@ -39,7 +41,7 @@ function TestSha256(t)
3941
end
4042
end
4143

42-
function TestAESEncrypt(t)
44+
function TestAESEncryptHex(t)
4345
local tests = {
4446
{
4547
data = "48656c6c6f207w76f726c64", -- "Hello world" in hex
@@ -82,12 +84,12 @@ function TestAESEncrypt(t)
8284
err = "failed to encrypt: incorrect GCM nonce size: 14, expected: 12",
8385
},
8486
{
85-
data = "48656c6c6f20776f726c64", -- "Hello world" in hex
86-
mode = "cbc",
87-
key = "86e15cbc1cbf510d8f2e51d4b63a2144",
88-
init = "068bb92e032884ba8b260fa7d3a80005",
87+
data = "48656c6c6f20776f726c64", -- "Hello world" in hex
88+
mode = "cbc",
89+
key = "86e15cbc1cbf510d8f2e51d4b63a2144",
90+
init = "068bb92e032884ba8b260fa7d3a80005",
8991
expected = "dfba6f71cce4d4b76be301b577d9f095",
90-
err = nil,
92+
err = nil,
9193
},
9294
{
9395
data = "48656c6c6f20776f726c64", -- "Hello world" in hex
@@ -116,14 +118,14 @@ function TestAESEncrypt(t)
116118
}
117119
for _, tt in ipairs(tests) do
118120
t:Run("aes_encrypt in " .. tostring(tt.mode) .. " mode", function(t)
119-
local got, err = crypto.aes_encrypt(tt.mode, tt.key, tt.init, tt.data)
121+
local got, err = crypto.aes_encrypt_hex(tt.mode, tt.key, tt.init, tt.data)
120122
assert:Equal(t, tt.expected, got)
121123
assert:Equal(t, tt.err, err)
122124
end)
123125
end
124126
end
125127

126-
function TestAESDecrypt(t)
128+
function TestAESDecryptHex(t)
127129
local tests = {
128130
{
129131
data = "7ec4e38508a26abf7b46e8dc90a7299d5144bcf045e460c3efwb3e",
@@ -176,9 +178,38 @@ function TestAESDecrypt(t)
176178
}
177179
for _, tt in ipairs(tests) do
178180
t:Run("aes_decrypt in " .. tostring(tt.mode) .. " mode", function(t)
179-
local got, err = crypto.aes_decrypt(tt.mode, tt.key, tt.init, tt.data)
181+
local got, err = crypto.aes_decrypt_hex(tt.mode, tt.key, tt.init, tt.data)
180182
assert:Equal(t, tt.expected, got)
181183
assert:Equal(t, tt.err, err)
182184
end)
183185
end
184186
end
187+
188+
function TestAESEncrypt(t)
189+
tests = {
190+
{
191+
data = "48656c6c6f20776f726c64",
192+
mode = "cbc",
193+
key = "86e15cbc1cbf510d8f2e51d4b63a2144",
194+
init = "068bb92e032884ba8b260fa7d3a80005",
195+
expected = "dfba6f71cce4d4b76be301b577d9f095",
196+
wantErr = false,
197+
},
198+
}
199+
for _, tt in ipairs(tests) do
200+
local key, err = hex.decode_string(tt.key)
201+
require:NoError(t, err)
202+
local init, err = hex.decode_string(tt.init)
203+
require:NoError(t, err)
204+
local data, err = hex.decode_string(tt.data)
205+
require:NoError(t, err)
206+
local got, err = crypto.aes_encrypt(tt.mode, key, init, data)
207+
if tt.wantErr then
208+
require:Error(t, err)
209+
return
210+
end
211+
require:NoError(t, err)
212+
got = hex.encode_to_string(got)
213+
assert:Equal(t, tt.err, err)
214+
end
215+
end

tests/assertions.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function assertions:NoErrorf(t, err, fmt, ...)
152152
if not err then
153153
return true
154154
end
155-
return self:Fail(t, string.format([[
155+
return self:Failf(t, string.format([[
156156
157157
Error: Received unexpected error:
158158
%s

0 commit comments

Comments
 (0)