Comparison

util-src/hashes.c @ 417:d60e63379284

Removed commented code
author Waqas Hussain <waqas20@gmail.com>
date Wed, 26 Nov 2008 01:55:07 +0500
parent 416:7cc2d8a8ae97
child 432:fb19a5c14d44
comparison
equal deleted inserted replaced
416:7cc2d8a8ae97 417:d60e63379284
1 /* 1 /*
2 * xxpath.c 2 * hashes.c
3 * An implementation of a subset of xpath for Lua 5.1 3 * Lua library for sha1, sha256 and md5 hashes
4 * Waqas Hussain <waqas20@gmail.com>
5 * 05 Oct 2008 15:28:15
6 */ 4 */
7 5
8 #include <string.h> 6 #include <string.h>
9 7
10 #include "lua.h" 8 #include "lua.h"
11 #include "lauxlib.h" 9 #include "lauxlib.h"
12 #include <openssl/sha.h> 10 #include <openssl/sha.h>
13 #include <openssl/md5.h> 11 #include <openssl/md5.h>
14 12
15 /*//typedef unsigned int uint32;
16 #define uint32 unsigned int
17
18 #define chrsz 8
19 #define hexcase 0
20
21 uint32 safe_add(uint32 x, uint32 y) {
22 uint32 lsw = (x & 0xFFFF) + (y & 0xFFFF);
23 uint32 msw = (x >> 16) + (y >> 16) + (lsw >> 16);
24 return (msw << 16) | (lsw & 0xFFFF);
25 }
26
27 uint32 S (uint32 X, uint32 n) { return ( X >> n ) | (X << (32 - n)); }
28 uint32 R (uint32 X, uint32 n) { return ( X >> n ); }
29 uint32 Ch(uint32 x, uint32 y, uint32 z) { return ((x & y) ^ ((~x) & z)); }
30 uint32 Maj(uint32 x, uint32 y, uint32 z) { return ((x & y) ^ (x & z) ^ (y & z)); }
31 uint32 Sigma0256(uint32 x) { return (S(x, 2) ^ S(x, 13) ^ S(x, 22)); }
32 uint32 Sigma1256(uint32 x) { return (S(x, 6) ^ S(x, 11) ^ S(x, 25)); }
33 uint32 Gamma0256(uint32 x) { return (S(x, 7) ^ S(x, 18) ^ R(x, 3)); }
34 uint32 Gamma1256(uint32 x) { return (S(x, 17) ^ S(x, 19) ^ R(x, 10)); }
35
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};
37
38 void core_sha256 (char* m, uint32 l, uint32 m_length, uint32 out[8]) {
39
40 uint32 HASH[8] = {0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19};
41 uint32 W[64];
42 uint32 a, b, c, d, e, f, g, h, i, j;
43 uint32 T1, T2;
44 //uint32 i, j;
45 printf("core_sha256: start\n");
46
47 m[l >> 5] |= 0x80 << (24 - l % 32);
48 m[((l + 64 >> 9) << 4) + 15] = l;
49
50 printf("core_sha256: 1\n");
51 for ( i = 0; i<m_length; i+=16 ) {
52 a = HASH[0];
53 b = HASH[1];
54 c = HASH[2];
55 d = HASH[3];
56 e = HASH[4];
57 f = HASH[5];
58 g = HASH[6];
59 h = HASH[7];
60
61 for ( j = 0; j<64; j++) {
62 if (j < 16) W[j] = m[j + i];
63 else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);
64
65 T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);
66 T2 = safe_add(Sigma0256(a), Maj(a, b, c));
67
68 h = g;
69 g = f;
70 f = e;
71 e = safe_add(d, T1);
72 d = c;
73 c = b;
74 b = a;
75 a = safe_add(T1, T2);
76 }
77
78 HASH[0] = safe_add(a, HASH[0]);
79 HASH[1] = safe_add(b, HASH[1]);
80 HASH[2] = safe_add(c, HASH[2]);
81 HASH[3] = safe_add(d, HASH[3]);
82 HASH[4] = safe_add(e, HASH[4]);
83 HASH[5] = safe_add(f, HASH[5]);
84 HASH[6] = safe_add(g, HASH[6]);
85 HASH[7] = safe_add(h, HASH[7]);
86 }
87 printf("core_sha256: 2\n");
88
89 out[0] = HASH[0];
90 out[1] = HASH[1];
91 out[2] = HASH[2];
92 out[3] = HASH[3];
93 out[4] = HASH[4];
94 out[5] = HASH[5];
95 out[6] = HASH[6];
96 out[7] = HASH[7];
97
98 printf("core_sha256: end\n");
99 }
100
101 void binb2hex (const uint32 binarray[8], char str[65]) {
102 const char* hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
103 uint32 pos = 0;
104 int i;
105 printf("binb2hex: start\n");
106 //var str = "";
107 for(i = 0; i < 8 * 4; i++) {
108 str[pos++] = hex_tab[(binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF];
109 str[pos++] = hex_tab[(binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF];
110 }
111 //return str;
112 str[64] = 0;
113 printf("binb2hex: end\n");
114 }
115
116 static void sha256(const char* s, uint32 s_length, char output[65]) {
117 uint32 hash[8];
118 char* copy;
119
120 printf("sha256: start\n");
121
122 copy = (char*) malloc(s_length + 1);
123 strcpy(copy, s);
124 core_sha256(copy, s_length * chrsz, s_length, hash);
125 free(copy);
126
127 binb2hex(hash, output);
128
129 printf("sha256: end\n");
130
131
132
133 //s = Utf8Encode(s);
134 //return binb2hex(core_sha256(str2binb(s), s.length * chrsz));
135
136 }
137 */
138
139 //static int Lsha256(lua_State *L) /** sha256(s) */
140 /*{
141 size_t l;
142 const char *s = luaL_checklstring(L, 1, &l);
143 int len = strlen(s);
144 char hash[32];
145 char result[65];
146
147 //sha256(s, len, hash);
148 SHA256(s, len, hash);
149 toHex(hash, 32, result);
150
151 //printf("input: %s, length: %d, outlen: %d\n", s, len, strlen(result));
152
153 lua_pushstring(L, result);
154 return 1;
155 }*/
156
157
158 const char* hex_tab = "0123456789abcdef"; 13 const char* hex_tab = "0123456789abcdef";
159 void toHex(const char* in, int length, char* out) { 14 void toHex(const char* in, int length, char* out) {
160 int i; 15 int i;
161 for (i = 0; i < length; i++) { 16 for (i = 0; i < length; i++) {
162 out[i*2] = hex_tab[(in[i] >> 4) & 0xF]; 17 out[i*2] = hex_tab[(in[i] >> 4) & 0xF];
163 out[i*2+1] = hex_tab[(in[i]) & 0xF]; 18 out[i*2+1] = hex_tab[(in[i]) & 0xF];
164 } 19 }
165 //out[i*2] = 0;
166 } 20 }
167 21
168 #define MAKE_HASH_FUNCTION(myFunc, func, size) \ 22 #define MAKE_HASH_FUNCTION(myFunc, func, size) \
169 static int myFunc(lua_State *L) { \ 23 static int myFunc(lua_State *L) { \
170 size_t len; \ 24 size_t len; \