Skip to content
This repository
Browse code

fix segfault error

  • Loading branch information...
commit 2064ae14bf914544a581773efe989fc3152ebc17 1 parent 3d7aef1
Brainfucker authored

Showing 1 changed file with 28 additions and 116 deletions. Show diff stats Hide diff stats

  1. +28 116 hashlib.cc
144 hashlib.cc
@@ -58,15 +58,7 @@ Handle<Value>
58 58 sha(const Arguments& args)
59 59 {
60 60 HandleScope scope;
61   - v8::Local<v8::String> str;
62   - if (!args[0]->IsString()) {
63   - v8::Local<v8::Object> self=args.This();
64   - v8::Handle<v8::Value> emptyArgs[] = {};
65   - str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
66   - } else {
67   - str=args[0]->ToString();
68   - }
69   - String::Utf8Value data(str);
  61 + String::Utf8Value data(args[0]->ToString());
70 62 SHA_CTX ctx;
71 63 unsigned char digest[20];
72 64 unsigned char hexdigest[40];
@@ -76,7 +68,7 @@ sha(const Arguments& args)
76 68 SHA_Final(digest, &ctx);
77 69
78 70 make_digest_ex(hexdigest, digest, 20);
79   -
  71 +
80 72 return scope.Close(String::New((char*)hexdigest,40));
81 73 }
82 74
@@ -85,15 +77,7 @@ sha1(const Arguments& args)
85 77 {
86 78 HandleScope scope;
87 79 using namespace sha1module;
88   - v8::Local<v8::String> str;
89   - if (!args[0]->IsString()) {
90   - v8::Local<v8::Object> self=args.This();
91   - v8::Handle<v8::Value> emptyArgs[] = {};
92   - str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
93   - } else {
94   - str=args[0]->ToString();
95   - }
96   - String::Utf8Value data(str);
  80 + String::Utf8Value data(args[0]->ToString());
97 81 unsigned char digest[40];
98 82 unsigned char hexdigest[40];
99 83 SHAobject *sha;
@@ -103,7 +87,7 @@ sha1(const Arguments& args)
103 87 sha_final(digest, sha);
104 88
105 89 make_digest_ex(hexdigest, digest, 20);
106   -
  90 +
107 91 return scope.Close(String::New((char*)hexdigest,40));
108 92 }
109 93
@@ -111,24 +95,15 @@ Handle<Value>
111 95 hmac_sha1(const Arguments& args)
112 96 {
113 97 HandleScope scope;
  98 +
114 99 using namespace sha1module;
115   - v8::Local<v8::String> str,str2;
116   - if (!args[0]->IsString()) {
117   - v8::Local<v8::Object> self=args.This();
118   - v8::Handle<v8::Value> emptyArgs[] = {};
119   - str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
120   - str2=args[0]->ToString();
121   - } else {
122   - str=args[0]->ToString();
123   - str2=args[1]->ToString();
124   - }
125   - String::Utf8Value data(str);
126   - String::Utf8Value key_input(str2);
  100 + String::Utf8Value data(args[0]->ToString());
  101 + String::Utf8Value key_input(args[1]->ToString());
127 102
128 103 unsigned char digest[40];
129 104 unsigned char hexdigest[40];
130 105 unsigned int i;
131   -
  106 +
132 107 const void *key = (unsigned char*) *key_input;
133 108 size_t keylen = key_input.length();
134 109
@@ -178,18 +153,8 @@ hmac_md5(const Arguments& args)
178 153 {
179 154 HandleScope scope;
180 155
181   - v8::Local<v8::String> str,str2;
182   - if (!args[0]->IsString()) {
183   - v8::Local<v8::Object> self=args.This();
184   - v8::Handle<v8::Value> emptyArgs[] = {};
185   - str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
186   - str2=args[0]->ToString();
187   - } else {
188   - str=args[0]->ToString();
189   - str2=args[1]->ToString();
190   - }
191   - String::Utf8Value data(str);
192   - String::Utf8Value key_input(str2);
  156 + String::Utf8Value data(args[0]->ToString());
  157 + String::Utf8Value key_input(args[1]->ToString());
193 158
194 159 unsigned char digest[16];
195 160 unsigned char hexdigest[32];
@@ -204,11 +169,11 @@ hmac_md5(const Arguments& args)
204 169 {
205 170 char optkeybuf[20];
206 171 MD5_CTX keyhash;
207   -
  172 +
208 173 MD5Init(&keyhash);
209 174 MD5Update(&keyhash, (unsigned char*) key, keylen);
210 175 MD5Final((unsigned char*) optkeybuf, &keyhash);
211   -
  176 +
212 177 keylen = 20;
213 178 key = optkeybuf;
214 179 }
@@ -223,14 +188,14 @@ hmac_md5(const Arguments& args)
223 188 ipad[i] ^= 0x36;
224 189 opad[i] ^= 0x5c;
225 190 }
226   -
  191 +
227 192 MD5_CTX context;
228   -
  193 +
229 194 MD5Init(&context);
230 195 MD5Update(&context, (unsigned char*) ipad, 64);
231 196 MD5Update(&context, (unsigned char*) *data, data.length());
232 197 MD5Final(digest, &context);
233   -
  198 +
234 199 MD5Init(&context);
235 200 MD5Update(&context, (unsigned char*) opad, 64);
236 201 MD5Update(&context, digest, 16);
@@ -247,15 +212,7 @@ sha256(const Arguments& args)
247 212 {
248 213 HandleScope scope;
249 214 using namespace sha256module;
250   - v8::Local<v8::String> str;
251   - if (!args[0]->IsString()) {
252   - v8::Local<v8::Object> self=args.This();
253   - v8::Handle<v8::Value> emptyArgs[] = {};
254   - str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
255   - } else {
256   - str=args[0]->ToString();
257   - }
258   - String::Utf8Value data(str);
  215 + String::Utf8Value data(args[0]->ToString());
259 216 unsigned char digest[64];
260 217 unsigned char hexdigest[64];
261 218 SHAobject *sha;
@@ -265,7 +222,7 @@ sha256(const Arguments& args)
265 222 sha_final(digest, sha);
266 223
267 224 make_digest_ex(hexdigest, digest, 32);
268   -
  225 +
269 226 return scope.Close(String::New((char*)hexdigest,64));
270 227 }
271 228
@@ -274,15 +231,7 @@ sha512(const Arguments& args)
274 231 {
275 232 HandleScope scope;
276 233 using namespace sha512module;
277   - v8::Local<v8::String> str;
278   - if (!args[0]->IsString()) {
279   - v8::Local<v8::Object> self=args.This();
280   - v8::Handle<v8::Value> emptyArgs[] = {};
281   - str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
282   - } else {
283   - str=args[0]->ToString();
284   - }
285   - String::Utf8Value data(str);
  234 + String::Utf8Value data(args[0]->ToString());
286 235 unsigned char digest[128];
287 236 unsigned char hexdigest[128];
288 237 SHAobject *sha;
@@ -292,7 +241,7 @@ sha512(const Arguments& args)
292 241 sha512_final(digest, sha);
293 242
294 243 make_digest_ex(hexdigest, digest, 64);
295   -
  244 +
296 245 return scope.Close(String::New((char*)hexdigest,128));
297 246 }
298 247
@@ -301,15 +250,7 @@ md4(const Arguments& args)
301 250 {
302 251 HandleScope scope;
303 252
304   - v8::Local<v8::String> str;
305   - if (!args[0]->IsString()) {
306   - v8::Local<v8::Object> self=args.This();
307   - v8::Handle<v8::Value> emptyArgs[] = {};
308   - str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
309   - } else {
310   - str=args[0]->ToString();
311   - }
312   - String::Utf8Value data(str);
  253 + String::Utf8Value data(args[0]->ToString());
313 254 MD4_CTX mdContext;
314 255 unsigned char digest[16];
315 256 unsigned char hexdigest[32];
@@ -328,23 +269,15 @@ Handle<Value>
328 269 md5(const Arguments& args)
329 270 {
330 271 HandleScope scope;
331   - v8::Local<v8::String> str;
332   - if (!args[0]->IsString()) {
333   - v8::Local<v8::Object> self=args.This();
334   - v8::Handle<v8::Value> emptyArgs[] = {};
335   - str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
336   - } else {
337   - str=args[0]->ToString();
338   - }
339   - String::Utf8Value data(str);
340   - int len=data.length();
  272 +
  273 + String::Utf8Value data(args[0]->ToString());
341 274 MD5_CTX mdContext;
342 275 unsigned char digest[16];
343 276 unsigned char hexdigest[32];
344 277
345 278 /* make an hash */
346 279 MD5Init(&mdContext);
347   - MD5Update(&mdContext, (unsigned char*)*data, len);
  280 + MD5Update(&mdContext, (unsigned char*)*data, data.length());
348 281 MD5Final(digest, &mdContext);
349 282
350 283 make_digest_ex(hexdigest, digest, 16);
@@ -357,15 +290,7 @@ md6(const Arguments& args)
357 290 {
358 291 HandleScope scope;
359 292
360   - v8::Local<v8::String> str;
361   - if (!args[0]->IsString()) {
362   - v8::Local<v8::Object> self=args.This();
363   - v8::Handle<v8::Value> emptyArgs[] = {};
364   - str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
365   - } else {
366   - str=args[0]->ToString();
367   - }
368   - String::Utf8Value data(str);
  293 + String::Utf8Value data(args[0]->ToString());
369 294
370 295 int len(32);
371 296 if (!args[1]->IsUndefined()) {
@@ -379,7 +304,7 @@ md6(const Arguments& args)
379 304 if (len%2!=0) half_len++;
380 305
381 306 make_digest_ex(hexdigest, digest, half_len);
382   -
  307 +
383 308 return scope.Close(String::New((char*)hexdigest,len));
384 309 }
385 310
@@ -433,6 +358,7 @@ Handle<Value> get_md5_file_async(char * path, void* data)
433 358
434 359 Handle<Value> get_md5_file(char * path)
435 360 {
  361 + HandleScope scope;
436 362 Unlocker unlock;
437 363 FILE *inFile = fopen (path, "rb");
438 364 MD5_CTX mdContext;
@@ -455,7 +381,7 @@ Handle<Value> get_md5_file(char * path)
455 381 make_digest_ex(hexdigest, digest, 16);
456 382 fclose (inFile);
457 383 Locker lock;
458   - return String::New((char*)hexdigest,32);
  384 + return scope.Close(String::New((char*)hexdigest,32));
459 385 }
460 386
461 387 Handle<Value>
@@ -478,7 +404,7 @@ md5_file(const Arguments& args)
478 404 file_data *fd=new file_data;
479 405 fd->byte = 0;
480 406 fd->environment = data;
481   -
  407 +
482 408 MD5Init(&fd->mdContext);
483 409 ev_ref(EV_DEFAULT_UC);
484 410 return scope.Close(get_md5_file_async(cpath,static_cast<void*>(fd)));
@@ -495,20 +421,6 @@ md5_file(const Arguments& args)
495 421 extern "C" void init (Handle<Object> target)
496 422 {
497 423 HandleScope scope;
498   -
499   -
500   - v8::Local<v8::Object> String = Context::GetCurrent()->Global()->Get(String::New("String"))->ToObject()->GetPrototype()->ToObject()->GetPrototype()->ToObject();
501   - String->Set(String::New("md4"), FunctionTemplate::New(md4)->GetFunction());
502   - String->Set(String::New("md5"), FunctionTemplate::New(md5)->GetFunction());
503   - String->Set(String::New("md6"), FunctionTemplate::New(md6)->GetFunction());
504   - String->Set(String::New("sha"), FunctionTemplate::New(sha)->GetFunction());
505   - String->Set(String::New("sha1"), FunctionTemplate::New(sha1)->GetFunction());
506   - String->Set(String::New("sha256"), FunctionTemplate::New(sha256)->GetFunction());
507   - String->Set(String::New("sha512"), FunctionTemplate::New(sha512)->GetFunction());
508   - String->Set(String::New("hmac_sha1"), FunctionTemplate::New(hmac_sha1)->GetFunction());
509   - String->Set(String::New("hmac_md5"), FunctionTemplate::New(hmac_md5)->GetFunction());
510   -
511   -
512 424 target->Set(String::New("md4"), FunctionTemplate::New(md4)->GetFunction());
513 425 target->Set(String::New("md5"), FunctionTemplate::New(md5)->GetFunction());
514 426 target->Set(String::New("md6"), FunctionTemplate::New(md6)->GetFunction());

0 comments on commit 2064ae1

Please sign in to comment.
Something went wrong with that request. Please try again.