Annotate

util-src/hashes.c @ 1020:8bf71f8bd0d1

util.logger: Fix for attempting to call a table
author Matthew Wild <mwild1@gmail.com>
date Tue, 21 Apr 2009 20:29:38 +0100
parent 896:2c0b9e3c11c3
child 2923:b7049746bd29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
896
2c0b9e3c11c3 0.3->0.4
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
1 /* Prosody IM v0.4
520
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 --
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
6 -- COPYING file in the source package for more information.
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
7 --
520
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 441
diff changeset
8 */
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
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
11 /*
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
12 * hashes.c
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
13 * Lua library for sha1, sha256 and md5 hashes
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
14 */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
15
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
16 #include <string.h>
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
17
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
18 #include "lua.h"
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
19 #include "lauxlib.h"
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
20 #include <openssl/sha.h>
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
21 #include <openssl/md5.h>
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
22
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
23 const char* hex_tab = "0123456789abcdef";
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
24 void toHex(const char* in, int length, char* out) {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
25 int i;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
26 for (i = 0; i < length; i++) {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
27 out[i*2] = hex_tab[(in[i] >> 4) & 0xF];
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
28 out[i*2+1] = hex_tab[(in[i]) & 0xF];
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
29 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
30 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
31
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
32 #define MAKE_HASH_FUNCTION(myFunc, func, size) \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
33 static int myFunc(lua_State *L) { \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
34 size_t len; \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
35 const char *s = luaL_checklstring(L, 1, &len); \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
36 int hex_out = lua_toboolean(L, 2); \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
37 char hash[size]; \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
38 char result[size*2]; \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
39 func((const unsigned char*)s, len, (unsigned char*)hash); \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
40 if (hex_out) { \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
41 toHex(hash, size, result); \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
42 lua_pushlstring(L, result, size*2); \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
43 } else { \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
44 lua_pushlstring(L, hash, size);\
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
45 } \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
46 return 1; \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
47 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
48
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
49 MAKE_HASH_FUNCTION(Lsha1, SHA1, 20)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
50 MAKE_HASH_FUNCTION(Lsha256, SHA256, 32)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
51 MAKE_HASH_FUNCTION(Lmd5, MD5, 16)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
52
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
53 static const luaL_Reg Reg[] =
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
54 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
55 { "sha1", Lsha1 },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
56 { "sha256", Lsha256 },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
57 { "md5", Lmd5 },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
58 { NULL, NULL }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
59 };
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
60
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
61 LUALIB_API int luaopen_util_hashes(lua_State *L)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
62 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
63 luaL_register(L, "hashes", Reg);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
64 lua_pushliteral(L, "version"); /** version */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
65 lua_pushliteral(L, "-3.14");
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
66 lua_settable(L,-3);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
67 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
68 }