Annotate

util-src/hashes.c @ 5289:ec50ad93e280

net.server_select: Remove useless duplicated settimeout() call
author Kim Alvefur <zash@zash.se>
date Mon, 07 Jan 2013 02:21:03 +0100
parent 4829:0ebc636faa59
child 5537:15464633d8fb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
1 /* Prosody IM
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
2 -- Copyright (C) 2009-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
3 -- Copyright (C) 2009-2010 Waqas Hussain
520
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
4828
f3e841521436 util.hashes: Use defined hash function output lengths.
Kim Alvefur <zash@zash.se>
parents: 2923
diff changeset
49 MAKE_HASH_FUNCTION(Lsha1, SHA1, SHA_DIGEST_LENGTH)
4829
0ebc636faa59 util.hashes: Add sha224, sha384, sha512
Kim Alvefur <zash@zash.se>
parents: 4828
diff changeset
50 MAKE_HASH_FUNCTION(Lsha224, SHA224, SHA224_DIGEST_LENGTH)
4828
f3e841521436 util.hashes: Use defined hash function output lengths.
Kim Alvefur <zash@zash.se>
parents: 2923
diff changeset
51 MAKE_HASH_FUNCTION(Lsha256, SHA256, SHA256_DIGEST_LENGTH)
4829
0ebc636faa59 util.hashes: Add sha224, sha384, sha512
Kim Alvefur <zash@zash.se>
parents: 4828
diff changeset
52 MAKE_HASH_FUNCTION(Lsha384, SHA384, SHA384_DIGEST_LENGTH)
0ebc636faa59 util.hashes: Add sha224, sha384, sha512
Kim Alvefur <zash@zash.se>
parents: 4828
diff changeset
53 MAKE_HASH_FUNCTION(Lsha512, SHA512, SHA512_DIGEST_LENGTH)
4828
f3e841521436 util.hashes: Use defined hash function output lengths.
Kim Alvefur <zash@zash.se>
parents: 2923
diff changeset
54 MAKE_HASH_FUNCTION(Lmd5, MD5, MD5_DIGEST_LENGTH)
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
55
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
56 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
57 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
58 { "sha1", Lsha1 },
4829
0ebc636faa59 util.hashes: Add sha224, sha384, sha512
Kim Alvefur <zash@zash.se>
parents: 4828
diff changeset
59 { "sha224", Lsha224 },
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
60 { "sha256", Lsha256 },
4829
0ebc636faa59 util.hashes: Add sha224, sha384, sha512
Kim Alvefur <zash@zash.se>
parents: 4828
diff changeset
61 { "sha384", Lsha384 },
0ebc636faa59 util.hashes: Add sha224, sha384, sha512
Kim Alvefur <zash@zash.se>
parents: 4828
diff changeset
62 { "sha512", Lsha512 },
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
63 { "md5", Lmd5 },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
64 { NULL, NULL }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
65 };
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
66
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
67 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
68 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
69 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
70 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
71 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
72 lua_settable(L,-3);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
73 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 520
diff changeset
74 }