Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 352 lines (288 sloc) 8.81 kb
c810854 @brainfucker add v8::Unlocker to md5_file
authored
1 /**
2 * FAST nodejs(http://github.com/ry/node/) library for making hashes
3 *
4 * @package hashlib
5 * @link http://github.com/brainfucker/hashlib
6 * @autor Oleg Illarionov <oleg@emby.ru>
7 * @version 1.0
8 */
9
4f02645 @brainfucker uploading project
authored
10 #include <v8.h>
be070af @brainfucker add async callback to md5_file function
authored
11 #include <ev.h>
28f5ba3 @brainfucker real async for md5_file now
authored
12 #include <eio.h>
13 #include <fcntl.h>
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
14
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
15 extern "C" {
16 #include "sha.h"
17 #include "md4.h"
18 #include "md5.h"
19 }
4f02645 @brainfucker uploading project
authored
20
5b260de @brainfucker adding basic hmac_sha1 support
authored
21 #include "lib/sha/HMAC_SHA1.cpp"
22 #include "lib/sha/SHA1.cpp"
23
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
24 #include "lib/sha/shamodule.c"
25 #include "lib/sha/sha256module.c"
26 #include "lib/sha/sha512module.c"
27
28 #include "lib/md6/md6.h"
29 #include "lib/md6/md6_compress.c"
30 #include "lib/md6/md6_mode.c"
31
4f02645 @brainfucker uploading project
authored
32 #include <iostream>
b64d1a7 @brainfucker add md5_file function
authored
33 #include <stdio.h>
28f5ba3 @brainfucker real async for md5_file now
authored
34
35 class file_data {
36 public:
37 int fd;
38 int byte;
39 MD5_CTX mdContext;
40 void* environment;
41 };
42
43
4f02645 @brainfucker uploading project
authored
44 using namespace v8;
45
98580af @brainfucker shitch to make_digest from PHP
authored
46 // make digest from php
47 static void
48 make_digest_ex(unsigned char *md5str, unsigned char *digest, int len)
49 {
50 static const char hexits[17] = "0123456789abcdef";
51 int i;
52
53 for (i = 0; i < len; i++) {
54 md5str[i * 2] = hexits[digest[i] >> 4];
55 md5str[(i * 2) + 1] = hexits[digest[i] & 0x0F];
56 }
57 md5str[len * 2] = '\0';
58 }
59
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
60 Handle<Value>
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
61 sha(const Arguments& args)
62 {
63 HandleScope scope;
64 String::Utf8Value data(args[0]->ToString());
65 SHA_CTX ctx;
66 unsigned char digest[20];
67 unsigned char hexdigest[40];
68
69 SHA_Init(&ctx);
70 SHA_Update(&ctx, (unsigned char*)*data, data.length());
71 SHA_Final(digest, &ctx);
72
73 make_digest_ex(hexdigest, digest, 20);
74
75 return String::New((char*)hexdigest,40);
76 }
77
78 Handle<Value>
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
79 sha1(const Arguments& args)
80 {
81 HandleScope scope;
82 using namespace sha1module;
83 String::Utf8Value data(args[0]->ToString());
84 unsigned char digest[40];
85 unsigned char hexdigest[40];
86 SHAobject *sha;
87 sha=new SHAobject;
88 sha_init(sha);
89 sha_update(sha, (unsigned char*) *data, data.length());
90 sha_final(digest, sha);
98580af @brainfucker shitch to make_digest from PHP
authored
91
92 make_digest_ex(hexdigest, digest, 20);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
93
94 return String::New((char*)hexdigest,40);
95 }
96
5b260de @brainfucker adding basic hmac_sha1 support
authored
97 Handle<Value>
98 hmac_sha1(const Arguments& args)
99 {
100 HandleScope scope;
101 using namespace sha1module;
102 String::Utf8Value data(args[1]->ToString());
103 String::Utf8Value ckey(args[0]->ToString());
104 //unsigned char digest[40];
105 //unsigned char hexdigest[40];
106
107 //BYTE Key[20];
108 //strcpy(Key,*ckey);
109 BYTE digest[20];
110
111 //memset(Key, 20, 0x0b) ;
112 CHMAC_SHA1 HMAC_SHA1 ;
113 //HMAC_SHA1.HMAC_SHA1((BYTE*)*data, strlen(*data), Key, sizeof(Key), digest);
114 HMAC_SHA1.HMAC_SHA1((BYTE*)*data, strlen(*data), (BYTE*)*ckey, strlen(*ckey), digest);
115
116
117 //SHAobject *sha;
118 //sha=new SHAobject;
119 //sha_init(sha);
120 //sha_update(sha, (unsigned char*) *data, data.length());
121 //sha_final(digest, sha);
122
123 //make_digest_ex(hexdigest, digest, 20);
124
125 return String::New((char*)digest,20);
126 }
127
4f02645 @brainfucker uploading project
authored
128
129 Handle<Value>
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
130 sha256(const Arguments& args)
131 {
132 HandleScope scope;
133 using namespace sha256module;
134 String::Utf8Value data(args[0]->ToString());
135 unsigned char digest[64];
136 unsigned char hexdigest[64];
137 SHAobject *sha;
138 sha=new SHAobject;
139 sha_init(sha);
140 sha_update(sha, (unsigned char*) *data, data.length());
141 sha_final(digest, sha);
98580af @brainfucker shitch to make_digest from PHP
authored
142
143 make_digest_ex(hexdigest, digest, 32);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
144
145 return String::New((char*)hexdigest,64);
146 }
147
148 Handle<Value>
149 sha512(const Arguments& args)
150 {
151 HandleScope scope;
152 using namespace sha512module;
153 String::Utf8Value data(args[0]->ToString());
154 unsigned char digest[128];
155 unsigned char hexdigest[128];
156 SHAobject *sha;
157 sha=new SHAobject;
158 sha512_init(sha);
159 sha512_update(sha, (unsigned char*) *data, data.length());
160 sha512_final(digest, sha);
98580af @brainfucker shitch to make_digest from PHP
authored
161
162 make_digest_ex(hexdigest, digest, 64);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
163
164 return String::New((char*)hexdigest,128);
165 }
166
167 Handle<Value>
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
168 md4(const Arguments& args)
4f02645 @brainfucker uploading project
authored
169 {
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
170 HandleScope scope;
4f02645 @brainfucker uploading project
authored
171
172 String::Utf8Value data(args[0]->ToString());
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
173 MD4_CTX mdContext;
174 unsigned char digest[16];
175 unsigned char hexdigest[32];
98580af @brainfucker shitch to make_digest from PHP
authored
176
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
177 /* make an hash */
178 MD4Init(&mdContext);
179 MD4Update(&mdContext, (unsigned char*)*data, data.length());
180 MD4Final(digest, &mdContext);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
181
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
182 make_digest_ex(hexdigest, digest, 16);
183
184 return String::New((char*)hexdigest,32);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
185 }
4f02645 @brainfucker uploading project
authored
186
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
187 Handle<Value>
188 md5(const Arguments& args)
189 {
190 HandleScope scope;
191
192 String::Utf8Value data(args[0]->ToString());
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
193 MD5_CTX mdContext;
4f02645 @brainfucker uploading project
authored
194 unsigned char digest[16];
195 unsigned char hexdigest[32];
196
197 /* make an hash */
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
198 MD5Init(&mdContext);
199 MD5Update(&mdContext, (unsigned char*)*data, data.length());
200 MD5Final(digest, &mdContext);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
201
98580af @brainfucker shitch to make_digest from PHP
authored
202 make_digest_ex(hexdigest, digest, 16);
4f02645 @brainfucker uploading project
authored
203
204 return String::New((char*)hexdigest,32);
205 }
206
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
207 Handle<Value>
208 md6(const Arguments& args)
209 {
210 HandleScope scope;
211
212 String::Utf8Value data(args[0]->ToString());
213
214 int len(32);
215 if (!args[1]->IsUndefined()) {
216 len=args[1]->ToInteger()->Value();
217 }
218 unsigned char digest[len];
219 unsigned char hexdigest[len];
220 md6_hash(len*8, (unsigned char*) *data, data.length(), digest);
221
222 int half_len=len/2;
223 if (len%2!=0) half_len++;
224
225 make_digest_ex(hexdigest, digest, half_len);
226
227 return String::New((char*)hexdigest,len);
228 }
28f5ba3 @brainfucker real async for md5_file now
authored
229 int read_cb (eio_req *req)
230 {
231 file_data *fd=(file_data *)req->data;
232 unsigned char *buf = (unsigned char *)EIO_BUF (req);
233 int bytes=(int)EIO_RESULT(req);
234 MD5Update (&fd->mdContext, buf, bytes);
dd58114 @brainfucker improve error reporting and fix readonly files segfault
authored
235 if (bytes==10240) {
28f5ba3 @brainfucker real async for md5_file now
authored
236 // Read next block
237 fd->byte+=bytes;
dd58114 @brainfucker improve error reporting and fix readonly files segfault
authored
238 eio_read(fd->fd, 0, 10240, fd->byte, EIO_PRI_DEFAULT, read_cb, static_cast<void*>(fd));
28f5ba3 @brainfucker real async for md5_file now
authored
239 } else {
240 // Final
241 unsigned char digest[16];
242 unsigned char hexdigest[32];
243 MD5Final(digest, &fd->mdContext);
244 make_digest_ex(hexdigest, digest, 16);
245
246 Persistent<Object> *data = reinterpret_cast<Persistent<Object>*>(fd->environment);
247
248 v8::Handle<v8::Function> callback = v8::Handle<v8::Function>::Cast((*data)->Get(String::New("callback")));
249 Handle<Object> recv = Handle<Object>::Cast((*data)->Get(String::New("recv")));
250 v8::Handle<v8::Value> outArgs[] = {String::New((char *)hexdigest,32)};
251 callback->Call(recv, 1, outArgs);
252 data->Dispose();
dd58114 @brainfucker improve error reporting and fix readonly files segfault
authored
253 eio_close(fd->fd, 0, 0, (void*)"close");
28f5ba3 @brainfucker real async for md5_file now
authored
254 ev_unref(EV_DEFAULT_UC);
255 }
256
257 return 0;
258 }
259
260 int open_cb (eio_req *req)
261 {
262 file_data *fd=(file_data *)req->data;
263
264 fd->fd = EIO_RESULT (req);
265 void* data = static_cast<void*>(fd);
dd58114 @brainfucker improve error reporting and fix readonly files segfault
authored
266 eio_read (fd->fd, 0, 10240, fd->byte, EIO_PRI_DEFAULT, read_cb, (void*)data);
28f5ba3 @brainfucker real async for md5_file now
authored
267
268 return 0;
269 }
270
271 Handle<Value> get_md5_file_async(char * path, void* data)
272 {
dd58114 @brainfucker improve error reporting and fix readonly files segfault
authored
273 eio_open (path, O_RDONLY, 0777, 0, open_cb, data);
28f5ba3 @brainfucker real async for md5_file now
authored
274 return v8::Boolean::New(true);
275 }
276
b64d1a7 @brainfucker add md5_file function
authored
277 Handle<Value> get_md5_file(char * path)
278 {
c810854 @brainfucker add v8::Unlocker to md5_file
authored
279 Unlocker unlock;
b64d1a7 @brainfucker add md5_file function
authored
280 FILE *inFile = fopen (path, "rb");
281 MD5_CTX mdContext;
282 unsigned char digest[16];
283 unsigned char hexdigest[32];
284 int bytes;
dd58114 @brainfucker improve error reporting and fix readonly files segfault
authored
285 unsigned char data[10240];
b64d1a7 @brainfucker add md5_file function
authored
286
287 if (inFile == NULL) {
288 std::string s="Cannot read ";
c810854 @brainfucker add v8::Unlocker to md5_file
authored
289 s+=path;
290 Locker lock;
b64d1a7 @brainfucker add md5_file function
authored
291 return ThrowException(Exception::Error(String::New(s.c_str())));
292 }
293
294 MD5Init (&mdContext);
dd58114 @brainfucker improve error reporting and fix readonly files segfault
authored
295 while ((bytes = fread (data, 1, 10240, inFile)) != 0)
b64d1a7 @brainfucker add md5_file function
authored
296 MD5Update (&mdContext, data, bytes);
297 MD5Final (digest, &mdContext);
298 make_digest_ex(hexdigest, digest, 16);
299 fclose (inFile);
c810854 @brainfucker add v8::Unlocker to md5_file
authored
300 Locker lock;
b64d1a7 @brainfucker add md5_file function
authored
301 return String::New((char*)hexdigest,32);
302 }
303
304 Handle<Value>
305 md5_file(const Arguments& args)
306 {
307 HandleScope scope;
dd58114 @brainfucker improve error reporting and fix readonly files segfault
authored
308 struct stat stFileInfo;
b64d1a7 @brainfucker add md5_file function
authored
309 String::Utf8Value path(args[0]->ToString());
dd58114 @brainfucker improve error reporting and fix readonly files segfault
authored
310 char* cpath=*path;
311 int intStat = stat(cpath,&stFileInfo);
312 if (intStat == 0) {
313 if (args[1]->IsFunction()) {
314 v8::Local<v8::Object> arguments = v8::Object::New();
315 arguments->Set(String::New("path"),args[0]->ToString());
316 arguments->Set(String::New("callback"),args[1]);
317 arguments->Set(String::New("recv"),args.This());
318 Persistent<Object> *data = new Persistent<Object>();
319 *data = Persistent<Object>::New(arguments);
320
321 file_data *fd=new file_data;
322 fd->byte = 0;
323 fd->environment = data;
324
325 MD5Init(&fd->mdContext);
326 ev_ref(EV_DEFAULT_UC);
327 return get_md5_file_async(cpath,static_cast<void*>(fd));
328 } else {
329 return get_md5_file(cpath);
330 }
b64d1a7 @brainfucker add md5_file function
authored
331 } else {
dd58114 @brainfucker improve error reporting and fix readonly files segfault
authored
332 std::string s="Cannot read ";
333 s+=cpath;
334 return ThrowException(Exception::Error(String::New(s.c_str())));
b64d1a7 @brainfucker add md5_file function
authored
335 }
336 }
337
4f02645 @brainfucker uploading project
authored
338 extern "C" void init (Handle<Object> target)
339 {
340 HandleScope scope;
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
341 target->Set(String::New("md4"), FunctionTemplate::New(md4)->GetFunction());
4f02645 @brainfucker uploading project
authored
342 target->Set(String::New("md5"), FunctionTemplate::New(md5)->GetFunction());
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
343 target->Set(String::New("md6"), FunctionTemplate::New(md6)->GetFunction());
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
344 target->Set(String::New("sha"), FunctionTemplate::New(sha)->GetFunction());
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
345 target->Set(String::New("sha1"), FunctionTemplate::New(sha1)->GetFunction());
5b260de @brainfucker adding basic hmac_sha1 support
authored
346 target->Set(String::New("hmac_sha1"), FunctionTemplate::New(hmac_sha1)->GetFunction());
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
347 target->Set(String::New("sha256"), FunctionTemplate::New(sha256)->GetFunction());
348 target->Set(String::New("sha512"), FunctionTemplate::New(sha512)->GetFunction());
b64d1a7 @brainfucker add md5_file function
authored
349
350 target->Set(String::New("md5_file"), FunctionTemplate::New(md5_file)->GetFunction());
4f02645 @brainfucker uploading project
authored
351 }
Something went wrong with that request. Please try again.