forked from newphp/lua-mcrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcrypt.c
More file actions
103 lines (74 loc) · 1.73 KB
/
Copy pathmcrypt.c
File metadata and controls
103 lines (74 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#define DDEBUG 0
#include "ddebug.h"
#include <lua.h>
#include <lauxlib.h>
#include <mcrypt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//m.bf_cfb_en(k, iv, '<<in>>')
static int
bf_cfb(lua_State *L, int enc) {
char *k;
size_t klen;
char *iv;
char *value;
size_t len;
char *p;
MCRYPT td;
int i;
if (lua_gettop(L) != 3) {
return luaL_error(L, "must give 3 arguments");
}
p = (char *) luaL_checklstring(L, 1, &len);
if (len < 8 || len > 128) {
return luaL_error(L, "error k len");
}
k = p;
klen = len;
p = (char *) luaL_checklstring(L, 2, &len);
if (len != 8) {
return luaL_error(L, "error iv len");
}
iv = p;
p = (char *) luaL_checklstring(L, 3, &len);
if (len == 0) {
return 1;
}
value = p;
td = mcrypt_module_open("blowfish", NULL, "cfb", NULL);
if (td == MCRYPT_FAILED) {
return luaL_error(L, "mcrypt module open error");
}
i = mcrypt_generic_init(td, k, klen, iv);
if (i < 0) {
return luaL_error(L, "mcrypt init fail");
}
if (enc) {
mcrypt_generic(td, value, len);
} else {
mdecrypt_generic(td, value, len);
}
mcrypt_generic_end(td);
lua_pushlstring(L, (char *) value, len);
return 1;
}
int
bf_cfb_en(lua_State *L) {
return bf_cfb((lua_State *) L, 1);
}
int
bf_cfb_de(lua_State *L) {
return bf_cfb((lua_State *) L, 0);
}
static const struct luaL_Reg mcrypt[] = {
{"bf_cfb_en", bf_cfb_en},
{"bf_cfb_de", bf_cfb_de},
{NULL, NULL}
};
int
luaopen_mcrypt(lua_State *L)
{
luaL_register(L, "mcrypt", mcrypt);
return 1;
}