Annotate

util-src/hashes.c @ 474:666e22848890

Use unsigned int instead of uint because uint is already in use on OS X.
author Tobias Markmann <tm@ayena.de>
date Fri, 28 Nov 2008 19:24:08 +0100
parent 441:4089b62b510c
child 520:e96ac4bb6dd8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
416
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 /*
417
d60e63379284 Removed commented code
Waqas Hussain <waqas20@gmail.com>
parents: 416
diff changeset
2 * hashes.c
d60e63379284 Removed commented code
Waqas Hussain <waqas20@gmail.com>
parents: 416
diff changeset
3 * 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
4 */
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 #include <string.h>
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 #include "lua.h"
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 #include "lauxlib.h"
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 #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
11 #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
12
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 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
14 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
15 int i;
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 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
17 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
18 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
19 }
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 }
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 #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
23 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
24 size_t len; \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 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
26 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
27 char hash[size]; \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 char result[size*2]; \
441
4089b62b510c Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents: 432
diff changeset
29 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
30 if (hex_out) { \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 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
32 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
33 } else { \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 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
35 } \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 return 1; \
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 }
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 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
40 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
41 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
42
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 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
44 {
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 { "sha1", Lsha1 },
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 { "sha256", Lsha256 },
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 { "md5", Lmd5 },
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 { NULL, NULL }
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 };
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50
432
fb19a5c14d44 Fix for hashes.c and encodings.c to rename luaopen for util_*
Matthew Wild <mwild1@gmail.com>
parents: 417
diff changeset
51 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
52 {
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 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
54 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
55 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
56 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
57 return 1;
7cc2d8a8ae97 Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 }