Software /
code /
prosody
Annotate
util-src/hashes.c @ 416:7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Wed, 26 Nov 2008 01:46:16 +0500 |
child | 417:d60e63379284 |
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 /* |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 * xxpath.c |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 * An implementation of a subset of xpath for Lua 5.1 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 * Waqas Hussain <waqas20@gmail.com> |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 * 05 Oct 2008 15:28:15 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 */ |
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 <string.h> |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 #include "lua.h" |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 #include "lauxlib.h" |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 #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
|
13 #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
|
14 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 /*//typedef unsigned int uint32; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 #define uint32 unsigned int |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 #define chrsz 8 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 #define hexcase 0 |
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 uint32 safe_add(uint32 x, uint32 y) { |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 uint32 lsw = (x & 0xFFFF) + (y & 0xFFFF); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 uint32 msw = (x >> 16) + (y >> 16) + (lsw >> 16); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 return (msw << 16) | (lsw & 0xFFFF); |
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 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 uint32 S (uint32 X, uint32 n) { return ( X >> n ) | (X << (32 - n)); } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 uint32 R (uint32 X, uint32 n) { return ( X >> n ); } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 uint32 Ch(uint32 x, uint32 y, uint32 z) { return ((x & y) ^ ((~x) & z)); } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 uint32 Maj(uint32 x, uint32 y, uint32 z) { return ((x & y) ^ (x & z) ^ (y & z)); } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 uint32 Sigma0256(uint32 x) { return (S(x, 2) ^ S(x, 13) ^ S(x, 22)); } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 uint32 Sigma1256(uint32 x) { return (S(x, 6) ^ S(x, 11) ^ S(x, 25)); } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 uint32 Gamma0256(uint32 x) { return (S(x, 7) ^ S(x, 18) ^ R(x, 3)); } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 uint32 Gamma1256(uint32 x) { return (S(x, 17) ^ S(x, 19) ^ R(x, 10)); } |
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 static const uint32 K[64] = {0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0xFC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x6CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2}; |
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 void core_sha256 (char* m, uint32 l, uint32 m_length, uint32 out[8]) { |
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 uint32 HASH[8] = {0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19}; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 uint32 W[64]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 uint32 a, b, c, d, e, f, g, h, i, j; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 uint32 T1, T2; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 //uint32 i, j; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 printf("core_sha256: start\n"); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 m[l >> 5] |= 0x80 << (24 - l % 32); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 m[((l + 64 >> 9) << 4) + 15] = l; |
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 printf("core_sha256: 1\n"); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 for ( i = 0; i<m_length; i+=16 ) { |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 a = HASH[0]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 b = HASH[1]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 c = HASH[2]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 d = HASH[3]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 e = HASH[4]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 f = HASH[5]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 g = HASH[6]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 h = HASH[7]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 for ( j = 0; j<64; j++) { |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 if (j < 16) W[j] = m[j + i]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]); |
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 T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 T2 = safe_add(Sigma0256(a), Maj(a, b, c)); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 h = g; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 g = f; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 f = e; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 e = safe_add(d, T1); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 d = c; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 c = b; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 b = a; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 a = safe_add(T1, T2); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 HASH[0] = safe_add(a, HASH[0]); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 HASH[1] = safe_add(b, HASH[1]); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 HASH[2] = safe_add(c, HASH[2]); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 HASH[3] = safe_add(d, HASH[3]); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 HASH[4] = safe_add(e, HASH[4]); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 HASH[5] = safe_add(f, HASH[5]); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 HASH[6] = safe_add(g, HASH[6]); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 HASH[7] = safe_add(h, HASH[7]); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 printf("core_sha256: 2\n"); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 out[0] = HASH[0]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 out[1] = HASH[1]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 out[2] = HASH[2]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 out[3] = HASH[3]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 out[4] = HASH[4]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 out[5] = HASH[5]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 out[6] = HASH[6]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 out[7] = HASH[7]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 printf("core_sha256: end\n"); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 void binb2hex (const uint32 binarray[8], char str[65]) { |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 const char* hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 uint32 pos = 0; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 int i; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 printf("binb2hex: start\n"); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 //var str = ""; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 for(i = 0; i < 8 * 4; i++) { |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 str[pos++] = hex_tab[(binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 str[pos++] = hex_tab[(binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 //return str; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 str[64] = 0; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 printf("binb2hex: end\n"); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 static void sha256(const char* s, uint32 s_length, char output[65]) { |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 uint32 hash[8]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 char* copy; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 printf("sha256: start\n"); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 copy = (char*) malloc(s_length + 1); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 strcpy(copy, s); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 core_sha256(copy, s_length * chrsz, s_length, hash); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 free(copy); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 binb2hex(hash, output); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 printf("sha256: end\n"); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 //s = Utf8Encode(s); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 //return binb2hex(core_sha256(str2binb(s), s.length * chrsz)); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 */ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 //static int Lsha256(lua_State *L) /** sha256(s) */ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 /*{ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 size_t l; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 const char *s = luaL_checklstring(L, 1, &l); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 int len = strlen(s); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 char hash[32]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 char result[65]; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 //sha256(s, len, hash); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 SHA256(s, len, hash); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 toHex(hash, 32, result); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 //printf("input: %s, length: %d, outlen: %d\n", s, len, strlen(result)); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 lua_pushstring(L, result); |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 return 1; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 }*/ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 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
|
159 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
|
160 int i; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 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
|
162 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
|
163 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
|
164 } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 //out[i*2] = 0; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 #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
|
169 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
|
170 size_t len; \ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
171 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
|
172 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
|
173 char hash[size]; \ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 char result[size*2]; \ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
175 func(s, len, hash); \ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
176 if (hex_out) { \ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
177 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
|
178 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
|
179 } else { \ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 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
|
181 } \ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 return 1; \ |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 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
|
186 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
|
187 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
|
188 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 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
|
190 { |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 { "sha1", Lsha1 }, |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 { "sha256", Lsha256 }, |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 { "md5", Lmd5 }, |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 { NULL, NULL } |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 }; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
196 |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 LUALIB_API int luaopen_hashes(lua_State *L) |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 { |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 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
|
200 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
|
201 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
|
202 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
|
203 return 1; |
7cc2d8a8ae97
Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
204 } |