Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 258 lines (209 sloc) 6.461 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>
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
12
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
13 extern "C" {
14 #include "sha.h"
15 #include "md4.h"
16 #include "md5.h"
17 }
4f02645 @brainfucker uploading project
authored
18
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
19 #include "lib/sha/shamodule.c"
20 #include "lib/sha/sha256module.c"
21 #include "lib/sha/sha512module.c"
22
23 #include "lib/md6/md6.h"
24 #include "lib/md6/md6_compress.c"
25 #include "lib/md6/md6_mode.c"
26
4f02645 @brainfucker uploading project
authored
27 #include <iostream>
b64d1a7 @brainfucker add md5_file function
authored
28 #include <stdio.h>
4f02645 @brainfucker uploading project
authored
29 using namespace v8;
30
98580af @brainfucker shitch to make_digest from PHP
authored
31 // make digest from php
32 static void
33 make_digest_ex(unsigned char *md5str, unsigned char *digest, int len)
34 {
35 static const char hexits[17] = "0123456789abcdef";
36 int i;
37
38 for (i = 0; i < len; i++) {
39 md5str[i * 2] = hexits[digest[i] >> 4];
40 md5str[(i * 2) + 1] = hexits[digest[i] & 0x0F];
41 }
42 md5str[len * 2] = '\0';
43 }
44
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
45 Handle<Value>
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
46 sha(const Arguments& args)
47 {
48 HandleScope scope;
49 String::Utf8Value data(args[0]->ToString());
50 SHA_CTX ctx;
51 unsigned char digest[20];
52 unsigned char hexdigest[40];
53
54 SHA_Init(&ctx);
55 SHA_Update(&ctx, (unsigned char*)*data, data.length());
56 SHA_Final(digest, &ctx);
57
58 make_digest_ex(hexdigest, digest, 20);
59
60 return String::New((char*)hexdigest,40);
61 }
62
63 Handle<Value>
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
64 sha1(const Arguments& args)
65 {
66 HandleScope scope;
67 using namespace sha1module;
68 String::Utf8Value data(args[0]->ToString());
69 unsigned char digest[40];
70 unsigned char hexdigest[40];
71 SHAobject *sha;
72 sha=new SHAobject;
73 sha_init(sha);
74 sha_update(sha, (unsigned char*) *data, data.length());
75 sha_final(digest, sha);
98580af @brainfucker shitch to make_digest from PHP
authored
76
77 make_digest_ex(hexdigest, digest, 20);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
78
79 return String::New((char*)hexdigest,40);
80 }
81
4f02645 @brainfucker uploading project
authored
82
83 Handle<Value>
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
84 sha256(const Arguments& args)
85 {
86 HandleScope scope;
87 using namespace sha256module;
88 String::Utf8Value data(args[0]->ToString());
89 unsigned char digest[64];
90 unsigned char hexdigest[64];
91 SHAobject *sha;
92 sha=new SHAobject;
93 sha_init(sha);
94 sha_update(sha, (unsigned char*) *data, data.length());
95 sha_final(digest, sha);
98580af @brainfucker shitch to make_digest from PHP
authored
96
97 make_digest_ex(hexdigest, digest, 32);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
98
99 return String::New((char*)hexdigest,64);
100 }
101
102 Handle<Value>
103 sha512(const Arguments& args)
104 {
105 HandleScope scope;
106 using namespace sha512module;
107 String::Utf8Value data(args[0]->ToString());
108 unsigned char digest[128];
109 unsigned char hexdigest[128];
110 SHAobject *sha;
111 sha=new SHAobject;
112 sha512_init(sha);
113 sha512_update(sha, (unsigned char*) *data, data.length());
114 sha512_final(digest, sha);
98580af @brainfucker shitch to make_digest from PHP
authored
115
116 make_digest_ex(hexdigest, digest, 64);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
117
118 return String::New((char*)hexdigest,128);
119 }
120
121 Handle<Value>
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
122 md4(const Arguments& args)
4f02645 @brainfucker uploading project
authored
123 {
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
124 HandleScope scope;
4f02645 @brainfucker uploading project
authored
125
126 String::Utf8Value data(args[0]->ToString());
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
127 MD4_CTX mdContext;
128 unsigned char digest[16];
129 unsigned char hexdigest[32];
98580af @brainfucker shitch to make_digest from PHP
authored
130
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
131 /* make an hash */
132 MD4Init(&mdContext);
133 MD4Update(&mdContext, (unsigned char*)*data, data.length());
134 MD4Final(digest, &mdContext);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
135
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
136 make_digest_ex(hexdigest, digest, 16);
137
138 return String::New((char*)hexdigest,32);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
139 }
4f02645 @brainfucker uploading project
authored
140
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
141 Handle<Value>
142 md5(const Arguments& args)
143 {
144 HandleScope scope;
145
146 String::Utf8Value data(args[0]->ToString());
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
147 MD5_CTX mdContext;
4f02645 @brainfucker uploading project
authored
148 unsigned char digest[16];
149 unsigned char hexdigest[32];
150
151 /* make an hash */
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
152 MD5Init(&mdContext);
153 MD5Update(&mdContext, (unsigned char*)*data, data.length());
154 MD5Final(digest, &mdContext);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
155
98580af @brainfucker shitch to make_digest from PHP
authored
156 make_digest_ex(hexdigest, digest, 16);
4f02645 @brainfucker uploading project
authored
157
158 return String::New((char*)hexdigest,32);
159 }
160
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
161 Handle<Value>
162 md6(const Arguments& args)
163 {
164 HandleScope scope;
165
166 String::Utf8Value data(args[0]->ToString());
167
168 int len(32);
169 if (!args[1]->IsUndefined()) {
170 len=args[1]->ToInteger()->Value();
171 }
172 unsigned char digest[len];
173 unsigned char hexdigest[len];
174 md6_hash(len*8, (unsigned char*) *data, data.length(), digest);
175
176 int half_len=len/2;
177 if (len%2!=0) half_len++;
178
179 make_digest_ex(hexdigest, digest, half_len);
180
181 return String::New((char*)hexdigest,len);
182 }
183
b64d1a7 @brainfucker add md5_file function
authored
184 Handle<Value> get_md5_file(char * path)
185 {
c810854 @brainfucker add v8::Unlocker to md5_file
authored
186 Unlocker unlock;
b64d1a7 @brainfucker add md5_file function
authored
187 FILE *inFile = fopen (path, "rb");
188 MD5_CTX mdContext;
189 unsigned char digest[16];
190 unsigned char hexdigest[32];
191 int bytes;
192 unsigned char data[1024];
193
194 if (inFile == NULL) {
195 std::string s="Cannot read ";
c810854 @brainfucker add v8::Unlocker to md5_file
authored
196 s+=path;
197 Locker lock;
b64d1a7 @brainfucker add md5_file function
authored
198 return ThrowException(Exception::Error(String::New(s.c_str())));
199 }
200
201 MD5Init (&mdContext);
202 while ((bytes = fread (data, 1, 1024, inFile)) != 0)
203 MD5Update (&mdContext, data, bytes);
204 MD5Final (digest, &mdContext);
205 make_digest_ex(hexdigest, digest, 16);
206 fclose (inFile);
c810854 @brainfucker add v8::Unlocker to md5_file
authored
207 Locker lock;
b64d1a7 @brainfucker add md5_file function
authored
208 return String::New((char*)hexdigest,32);
209 }
210
be070af @brainfucker add async callback to md5_file function
authored
211 void md5_file_callback(int revents, void *args) {
212 Persistent<Object> *data =
213 reinterpret_cast<Persistent<Object>*>(args);
214
215 String::Utf8Value path((*data)->Get(String::New("path")));
216 v8::Handle<v8::Function> callback = v8::Handle<v8::Function>::Cast((*data)->Get(String::New("callback")));
217 Handle<Object> recv = Handle<Object>::Cast((*data)->Get(String::New("recv")));
218 v8::Handle<v8::Value> outArgs[] = {get_md5_file(*path)};
219 callback->Call(recv, 1, outArgs);
220 data->Dispose();
221 }
222
b64d1a7 @brainfucker add md5_file function
authored
223 Handle<Value>
224 md5_file(const Arguments& args)
225 {
226 HandleScope scope;
227
228 String::Utf8Value path(args[0]->ToString());
229 if (args[1]->IsFunction()) {
f5551ad @brainfucker add async callback to md5_file function
authored
230 v8::Local<v8::Object> arguments = v8::Object::New();
231 arguments->Set(String::New("path"),args[0]->ToString());
232 arguments->Set(String::New("callback"),args[1]);
233 arguments->Set(String::New("recv"),args.This());
234 Persistent<Object> *data = new Persistent<Object>();
be070af @brainfucker add async callback to md5_file function
authored
235 *data = Persistent<Object>::New(arguments);
236
237 ev_once(0, EV_TIMEOUT, 0, md5_file_callback, (void*)data);
238
239 return v8::Boolean::New(true);
b64d1a7 @brainfucker add md5_file function
authored
240 } else {
241 return get_md5_file(*path);
242 }
243 }
244
4f02645 @brainfucker uploading project
authored
245 extern "C" void init (Handle<Object> target)
246 {
247 HandleScope scope;
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
248 target->Set(String::New("md4"), FunctionTemplate::New(md4)->GetFunction());
4f02645 @brainfucker uploading project
authored
249 target->Set(String::New("md5"), FunctionTemplate::New(md5)->GetFunction());
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
250 target->Set(String::New("md6"), FunctionTemplate::New(md6)->GetFunction());
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
251 target->Set(String::New("sha"), FunctionTemplate::New(sha)->GetFunction());
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
252 target->Set(String::New("sha1"), FunctionTemplate::New(sha1)->GetFunction());
253 target->Set(String::New("sha256"), FunctionTemplate::New(sha256)->GetFunction());
254 target->Set(String::New("sha512"), FunctionTemplate::New(sha512)->GetFunction());
b64d1a7 @brainfucker add md5_file function
authored
255
256 target->Set(String::New("md5_file"), FunctionTemplate::New(md5_file)->GetFunction());
4f02645 @brainfucker uploading project
authored
257 }
Something went wrong with that request. Please try again.