Annotate

util-src/hashes.c @ 584:eb0cea29c8d7

Temporary hack for global modules
author Matthew Wild <mwild1@gmail.com>
date Sat, 06 Dec 2008 23:15:48 +0000
parent 520:e96ac4bb6dd8
child 766:433a5226267f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
520
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
1 /* Prosody IM v0.1
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
2 -- Copyright (C) 2008 Matthew Wild
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
3 -- Copyright (C) 2008 Waqas Hussain
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
4 --
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
5 -- This program is free software; you can redistribute it and/or
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
6 -- modify it under the terms of the GNU General Public License
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
7 -- as published by the Free Software Foundation; either version 2
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
8 -- of the License, or (at your option) any later version.
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
9 --
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
10 -- This program is distributed in the hope that it will be useful,
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
13 -- GNU General Public License for more details.
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
14 --
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
15 -- You should have received a copy of the GNU General Public License
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
16 -- along with this program; if not, write to the Free Software
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
18 */
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
19
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
20
416
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 /*
417
d60e63379284 Removed commented code
Waqas Hussain <waqas20@gmail.com>
parents: 416
diff changeset
22 * hashes.c
d60e63379284 Removed commented code
Waqas Hussain <waqas20@gmail.com>
parents: 416
diff changeset
23 * Lua library for sha1, sha256 and md5 hashes
416
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 */
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 #include <string.h>
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 #include "lua.h"
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 #include "lauxlib.h"
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 #include <openssl/sha.h>
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 #include <openssl/md5.h>
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 const char* hex_tab = "0123456789abcdef";
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 void toHex(const char* in, int length, char* out) {
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 int i;
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 for (i = 0; i < length; i++) {
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 out[i*2] = hex_tab[(in[i] >> 4) & 0xF];
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 out[i*2+1] = hex_tab[(in[i]) & 0xF];
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 }
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 }
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 #define MAKE_HASH_FUNCTION(myFunc, func, size) \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 static int myFunc(lua_State *L) { \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 size_t len; \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 const char *s = luaL_checklstring(L, 1, &len); \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 int hex_out = lua_toboolean(L, 2); \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 char hash[size]; \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 char result[size*2]; \
441
4089b62b510c Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents: 432
diff changeset
49 func((const unsigned char*)s, len, (unsigned char*)hash); \
416
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 if (hex_out) { \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 toHex(hash, size, result); \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 lua_pushlstring(L, result, size*2); \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 } else { \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 lua_pushlstring(L, hash, size);\
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 } \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 return 1; \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 }
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 MAKE_HASH_FUNCTION(Lsha1, SHA1, 20)
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 MAKE_HASH_FUNCTION(Lsha256, SHA256, 32)
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 MAKE_HASH_FUNCTION(Lmd5, MD5, 16)
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 static const luaL_Reg Reg[] =
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 {
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 { "sha1", Lsha1 },
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 { "sha256", Lsha256 },
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 { "md5", Lmd5 },
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 { NULL, NULL }
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 };
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70
432
fb19a5c14d44 Fix for hashes.c and encodings.c to rename luaopen for util_*
Matthew Wild <mwild1@gmail.com>
parents: 417
diff changeset
71 LUALIB_API int luaopen_util_hashes(lua_State *L)
416
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 {
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 luaL_register(L, "hashes", Reg);
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 lua_pushliteral(L, "version"); /** version */
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 lua_pushliteral(L, "-3.14");
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 lua_settable(L,-3);
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 return 1;
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 }