forked from cloudfreexiao/lcrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmac.c
executable file
·170 lines (129 loc) · 4.85 KB
/
hmac.c
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "lcrypt.h"
/*
# define MD5_DIGEST_LENGTH 16
# define SHA_DIGEST_LENGTH 20
# define SHA224_DIGEST_LENGTH 28
# define SHA256_DIGEST_LENGTH 32
# define SHA384_DIGEST_LENGTH 48
# define SHA512_DIGEST_LENGTH 64
*/
int lhmac_md4(lua_State *L) {
size_t key_sz = 0;
size_t text_sz = 0;
const char * key = luaL_checklstring(L, 1, &key_sz);
if (!key || key_sz <= 0)
return luaL_error(L, "Invalid key value.");
const char * text = luaL_checklstring(L, 2, &text_sz);
if (!text || text_sz <= 0)
return luaL_error(L, "Invalid text value.");
uint32_t result_len = MD4_DIGEST_LENGTH;
unsigned char result[result_len];
HMAC(EVP_md4(), key, key_sz, (const unsigned char*)text, text_sz, result, &result_len);
lua_pushlstring(L, (const char *)result, result_len);
return 1;
};
int lhmac_md5(lua_State *L) {
size_t key_sz = 0;
size_t text_sz = 0;
const char * key = luaL_checklstring(L, 1, &key_sz);
if (!key || key_sz <= 0)
return luaL_error(L, "Invalid key value.");
const char * text = luaL_checklstring(L, 2, &text_sz);
if (!text || text_sz <= 0)
return luaL_error(L, "Invalid text value.");
uint32_t result_len = MD5_DIGEST_LENGTH;
unsigned char result[result_len];
HMAC(EVP_md5(), key, key_sz, (const unsigned char*)text, text_sz, result, &result_len);
lua_pushlstring(L, (const char *)result, result_len);
return 1;
};
int lhmac_sha128(lua_State *L) {
size_t key_sz = 0;
size_t text_sz = 0;
const char * key = luaL_checklstring(L, 1, &key_sz);
if (!key || key_sz <= 0)
return luaL_error(L, "Invalid key value.");
const char * text = luaL_checklstring(L, 2, &text_sz);
if (!text || text_sz <= 0)
return luaL_error(L, "Invalid text value.");
uint32_t result_len = SHA_DIGEST_LENGTH;
unsigned char result[result_len];
HMAC(EVP_sha1(), key, key_sz, (const unsigned char*)text, text_sz, result, &result_len);
lua_pushlstring(L, (const char *)result, result_len);
return 1;
};
int lhmac_sha224(lua_State *L) {
size_t key_sz = 0;
size_t text_sz = 0;
const char * key = luaL_checklstring(L, 1, &key_sz);
if (!key || key_sz <= 0)
return luaL_error(L, "Invalid key value.");
const char * text = luaL_checklstring(L, 2, &text_sz);
if (!text || text_sz <= 0)
return luaL_error(L, "Invalid text value.");
uint32_t result_len = SHA224_DIGEST_LENGTH;
unsigned char result[result_len];
HMAC(EVP_sha224(), key, key_sz, (const unsigned char*)text, text_sz, result, &result_len);
lua_pushlstring(L, (const char *)result, result_len);
return 1;
};
int lhmac_sha256(lua_State *L) {
size_t key_sz = 0;
size_t text_sz = 0;
const char * key = luaL_checklstring(L, 1, &key_sz);
if (!key || key_sz <= 0)
return luaL_error(L, "Invalid key value.");
const char * text = luaL_checklstring(L, 2, &text_sz);
if (!text || text_sz <= 0)
return luaL_error(L, "Invalid text value.");
uint32_t result_len = SHA256_DIGEST_LENGTH;
unsigned char result[result_len];
HMAC(EVP_sha256(), key, key_sz, (const unsigned char*)text, text_sz, result, &result_len);
lua_pushlstring(L, (const char *)result, result_len);
return 1;
};
int lhmac_sha384(lua_State *L) {
size_t key_sz = 0;
size_t text_sz = 0;
const char * key = luaL_checklstring(L, 1, &key_sz);
if (!key || key_sz <= 0)
return luaL_error(L, "Invalid key value.");
const char * text = luaL_checklstring(L, 2, &text_sz);
if (!text || text_sz <= 0)
return luaL_error(L, "Invalid text value.");
uint32_t result_len = SHA384_DIGEST_LENGTH;
unsigned char result[result_len];
HMAC(EVP_sha384(), key, key_sz, (const unsigned char*)text, text_sz, result, &result_len);
lua_pushlstring(L, (const char *)result, result_len);
return 1;
};
int lhmac_sha512(lua_State *L) {
size_t key_sz = 0;
size_t text_sz = 0;
const char * key = luaL_checklstring(L, 1, &key_sz);
if (!key || key_sz <= 0)
return luaL_error(L, "Invalid key value.");
const char * text = luaL_checklstring(L, 2, &text_sz);
if (!text || text_sz <= 0)
return luaL_error(L, "Invalid text value.");
uint32_t result_len = SHA512_DIGEST_LENGTH;
unsigned char result[result_len];
HMAC(EVP_sha512(), key, key_sz, (const unsigned char*)text, text_sz, result, &result_len);
lua_pushlstring(L, (const char *)result, result_len);
return 1;
};
int lhmac_ripemd160(lua_State *L) {
size_t key_sz = 0;
size_t text_sz = 0;
const char * key = luaL_checklstring(L, 1, &key_sz);
if (!key || key_sz <= 0)
return luaL_error(L, "Invalid key value.");
const char * text = luaL_checklstring(L, 2, &text_sz);
if (!text || text_sz <= 0)
return luaL_error(L, "Invalid text value.");
uint32_t result_len = RIPEMD160_DIGEST_LENGTH;
unsigned char result[result_len];
HMAC(EVP_ripemd160(), key, key_sz, (const unsigned char*)text, text_sz, result, &result_len);
lua_pushlstring(L, (const char *)result, result_len);
return 1;
};