Software /
code /
prosody
Annotate
util-src/hashes.c @ 664:30ccc5e3d8c8
Don't use TLS connection handler when SSL/TLS not available or configured (thanks to Ricardo for finding)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 29 Dec 2008 22:31:10 +0000 |
parent | 520:e96ac4bb6dd8 |
child | 766:433a5226267f |
rev | line source |
---|---|
520 | 1 /* Prosody IM v0.1 |
2 -- Copyright (C) 2008 Matthew Wild | |
3 -- Copyright (C) 2008 Waqas Hussain | |
4 -- | |
5 -- This program is free software; you can redistribute it and/or | |
6 -- modify it under the terms of the GNU General Public License | |
7 -- as published by the Free Software Foundation; either version 2 | |
8 -- of the License, or (at your option) any later version. | |
9 -- | |
10 -- This program is distributed in the hope that it will be useful, | |
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 -- GNU General Public License for more details. | |
14 -- | |
15 -- You should have received a copy of the GNU General Public License | |
16 -- along with this program; if not, write to the Free Software | |
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
18 */ | |
19 | |
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 | 22 * hashes.c |
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 } |