-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha2lib.c
149 lines (126 loc) · 3.35 KB
/
sha2lib.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
#include <lua.h>
#include <lauxlib.h>
#include "sha2.h"
// source: md5lib.c from md5 Lua library
/**
* X-Or. Does a bit-a-bit exclusive-or of two strings.
* @param s1: arbitrary binary string.
* @param s2: arbitrary binary string with same length as s1.
* @return a binary string with same length as s1 and s2,
* where each bit is the exclusive-or of the corresponding bits in s1-s2.
*/
static int ex_or (lua_State *L) {
size_t l1, l2;
const char *s1 = luaL_checklstring(L, 1, &l1);
const char *s2 = luaL_checklstring(L, 2, &l2);
luaL_Buffer b;
luaL_argcheck( L, l1 == l2, 2, "lengths must be equal" );
luaL_buffinit(L, &b);
while (l1--) luaL_putchar(&b, (*s1++)^(*s2++));
luaL_pushresult(&b);
return 1;
}
/**
* @param: an arbitrary binary string.
* @return: an SHA-256 hash string.
*/
static int sha256(lua_State *L) {
size_t len;
const char *data = luaL_checklstring(L, 1, &len);
SHA256_CTX context;
char digest[SHA256_DIGEST_LENGTH];
SHA256_Init(&context);
SHA256_Update(&context, data, len);
SHA256_Final(digest, &context);
lua_pushlstring(L, digest, SHA256_DIGEST_LENGTH);
return 1;
}
/**
* @param: an arbitrary binary string.
* @return: an SHA-256 hash string.
*/
static int sha256hex(lua_State *L) {
size_t len;
const char *data = luaL_checklstring(L, 1, &len);
char digest[SHA256_DIGEST_STRING_LENGTH];
SHA256_Data(data, len, digest);
lua_pushlstring(L, digest, SHA256_DIGEST_STRING_LENGTH - 1);
return 1;
}
/**
* @param: an arbitrary binary string.
* @return: an SHA-384 hash string.
*/
static int sha384(lua_State *L) {
size_t len;
const char *data = luaL_checklstring(L, 1, &len);
SHA384_CTX context;
char digest[SHA384_DIGEST_LENGTH];
SHA384_Init(&context);
SHA384_Update(&context, data, len);
SHA384_Final(digest, &context);
lua_pushlstring(L, digest, SHA384_DIGEST_LENGTH);
return 1;
}
/**
* @param: an arbitrary binary string.
* @return: an SHA-384 hash string.
*/
static int sha384hex(lua_State *L) {
size_t len;
const char *data = luaL_checklstring(L, 1, &len);
char digest[SHA384_DIGEST_STRING_LENGTH];
SHA384_Data(data, len, digest);
lua_pushlstring(L, digest, SHA384_DIGEST_STRING_LENGTH - 1);
return 1;
}
/**
* @param: an arbitrary binary string.
* @return: an SHA-512 hash string.
*/
static int sha512(lua_State *L) {
size_t len;
const char *data = luaL_checklstring(L, 1, &len);
SHA512_CTX context;
char digest[SHA512_DIGEST_LENGTH];
SHA512_Init(&context);
SHA512_Update(&context, data, len);
SHA512_Final(digest, &context);
lua_pushlstring(L, digest, SHA512_DIGEST_LENGTH);
return 1;
}
/**
* @param: an arbitrary binary string.
* @return: an SHA-512 hash string.
*/
static int sha512hex(lua_State *L) {
size_t len;
const char *data = luaL_checklstring(L, 1, &len);
char digest[SHA512_DIGEST_STRING_LENGTH];
SHA512_Data(data, len, digest);
lua_pushlstring(L, digest, SHA512_DIGEST_STRING_LENGTH - 1);
return 1;
}
/*
** Assumes the table is on top of the stack.
*/
static void set_info (lua_State *L) {
lua_pushliteral (L, "_VERSION");
lua_pushliteral (L, "sha2 0.1.0");
lua_settable (L, -3);
}
static struct luaL_reg reg[] = {
{"exor", ex_or},
{"sha256", sha256},
{"sha256hex", sha256hex},
{"sha384", sha384},
{"sha384hex", sha384hex},
{"sha512", sha512},
{"sha512hex", sha512hex},
{NULL, NULL}
};
int luaopen_sha2(lua_State *L) {
luaL_openlib(L, "sha2", reg, 0);
set_info (L);
return 1;
}