-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
150 lines (130 loc) · 2.61 KB
/
hash.cpp
File metadata and controls
150 lines (130 loc) · 2.61 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
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
#include "hash.hpp"
#include <lua.hpp>
#include <iostream>
#include <algorithm>
#include "hash-library/sha256.h"
#include "hash-library/sha3.h"
#include "hash-library/sha1.h"
#include "hash-library/md5.h"
#include "hash-library/keccak.h"
#include "hash-library/crc32.h"
#include "hash-library/hmac.h"
#include "textmodule_lua.hpp"
#include "textmodule_string.hpp"
int hash_sha256(lua_State* L) {
try {
lua_Sstring str = tm_tostring(L, 1);
SHA256 hash;
lua_pushsstring(L, hash(str));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int hash_sha3(lua_State* L) {
try {
lua_Sstring str = tm_tostring(L, 1);
SHA3 hash;
lua_pushsstring(L, hash(str));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int hash_sha1(lua_State* L) {
try {
lua_Sstring str = tm_tostring(L, 1);
SHA1 hash;
lua_pushsstring(L, hash(str));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int hash_md5(lua_State* L) {
try {
lua_Sstring str = tm_tostring(L, 1);
MD5 hash;
lua_pushsstring(L, hash(str));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int hash_keccak(lua_State* L) {
try {
lua_Sstring str = tm_tostring(L, 1);
Keccak hash;
lua_pushsstring(L, hash(str));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int hash_crc32(lua_State* L) {
try {
lua_Sstring str = tm_tostring(L, 1);
CRC32 hash;
lua_pushsstring(L, hash(str));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int hash_hmac(lua_State* L) {
try {
lua_Sstring str = tm_tostring(L, 1);
lua_Sstring key = tm_tostring(L, 2);
lua_Sstring type = lowerString(tm_tostring_s(L, 3, "sha256"));
if (type == "sha256") {
lua_pushsstring(L, hmac<SHA256>(str, key));
}
else if (type == "sha1") {
lua_pushsstring(L, hmac<SHA1>(str, key));
}
else if (type == "md5") {
lua_pushsstring(L, hmac<MD5>(str, key));
}
else {
return 0;
}
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
static luaL_Reg TEXTMODULE_HASH_REG[] = {
{"sha256", hash_sha256},
{"sha3", hash_sha3},
{"sha1", hash_sha1},
{"md5", hash_md5},
{"keccak", hash_keccak},
{"crc32", hash_crc32},
{"hmac", hash_hmac},
{ nullptr, nullptr }
};
void luaReg_hash(lua_State* L, lua_Option opt) {
if (opt["api"]["hash"]) {
tm_debuglog_apiloaded(opt, "hash");
lua_newtable(L);
luaL_register(L, NULL, TEXTMODULE_HASH_REG);
lua_setfield(L, -2, "hash");
}
else {
tm_debuglog_apinoloaded(opt, "hash");
}
}