Permalink
Browse files

fix segfault error

  • Loading branch information...
1 parent 3d7aef1 commit 2064ae14bf914544a581773efe989fc3152ebc17 Brainfucker committed Jul 25, 2010
Showing with 28 additions and 116 deletions.
  1. +28 −116 hashlib.cc
View
144 hashlib.cc
@@ -58,15 +58,7 @@ Handle<Value>
sha(const Arguments& args)
{
HandleScope scope;
- v8::Local<v8::String> str;
- if (!args[0]->IsString()) {
- v8::Local<v8::Object> self=args.This();
- v8::Handle<v8::Value> emptyArgs[] = {};
- str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
- } else {
- str=args[0]->ToString();
- }
- String::Utf8Value data(str);
+ String::Utf8Value data(args[0]->ToString());
SHA_CTX ctx;
unsigned char digest[20];
unsigned char hexdigest[40];
@@ -76,7 +68,7 @@ sha(const Arguments& args)
SHA_Final(digest, &ctx);
make_digest_ex(hexdigest, digest, 20);
-
+
return scope.Close(String::New((char*)hexdigest,40));
}
@@ -85,15 +77,7 @@ sha1(const Arguments& args)
{
HandleScope scope;
using namespace sha1module;
- v8::Local<v8::String> str;
- if (!args[0]->IsString()) {
- v8::Local<v8::Object> self=args.This();
- v8::Handle<v8::Value> emptyArgs[] = {};
- str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
- } else {
- str=args[0]->ToString();
- }
- String::Utf8Value data(str);
+ String::Utf8Value data(args[0]->ToString());
unsigned char digest[40];
unsigned char hexdigest[40];
SHAobject *sha;
@@ -103,32 +87,23 @@ sha1(const Arguments& args)
sha_final(digest, sha);
make_digest_ex(hexdigest, digest, 20);
-
+
return scope.Close(String::New((char*)hexdigest,40));
}
Handle<Value>
hmac_sha1(const Arguments& args)
{
HandleScope scope;
+
using namespace sha1module;
- v8::Local<v8::String> str,str2;
- if (!args[0]->IsString()) {
- v8::Local<v8::Object> self=args.This();
- v8::Handle<v8::Value> emptyArgs[] = {};
- str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
- str2=args[0]->ToString();
- } else {
- str=args[0]->ToString();
- str2=args[1]->ToString();
- }
- String::Utf8Value data(str);
- String::Utf8Value key_input(str2);
+ String::Utf8Value data(args[0]->ToString());
+ String::Utf8Value key_input(args[1]->ToString());
unsigned char digest[40];
unsigned char hexdigest[40];
unsigned int i;
-
+
const void *key = (unsigned char*) *key_input;
size_t keylen = key_input.length();
@@ -178,18 +153,8 @@ hmac_md5(const Arguments& args)
{
HandleScope scope;
- v8::Local<v8::String> str,str2;
- if (!args[0]->IsString()) {
- v8::Local<v8::Object> self=args.This();
- v8::Handle<v8::Value> emptyArgs[] = {};
- str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
- str2=args[0]->ToString();
- } else {
- str=args[0]->ToString();
- str2=args[1]->ToString();
- }
- String::Utf8Value data(str);
- String::Utf8Value key_input(str2);
+ String::Utf8Value data(args[0]->ToString());
+ String::Utf8Value key_input(args[1]->ToString());
unsigned char digest[16];
unsigned char hexdigest[32];
@@ -204,11 +169,11 @@ hmac_md5(const Arguments& args)
{
char optkeybuf[20];
MD5_CTX keyhash;
-
+
MD5Init(&keyhash);
MD5Update(&keyhash, (unsigned char*) key, keylen);
MD5Final((unsigned char*) optkeybuf, &keyhash);
-
+
keylen = 20;
key = optkeybuf;
}
@@ -223,14 +188,14 @@ hmac_md5(const Arguments& args)
ipad[i] ^= 0x36;
opad[i] ^= 0x5c;
}
-
+
MD5_CTX context;
-
+
MD5Init(&context);
MD5Update(&context, (unsigned char*) ipad, 64);
MD5Update(&context, (unsigned char*) *data, data.length());
MD5Final(digest, &context);
-
+
MD5Init(&context);
MD5Update(&context, (unsigned char*) opad, 64);
MD5Update(&context, digest, 16);
@@ -247,15 +212,7 @@ sha256(const Arguments& args)
{
HandleScope scope;
using namespace sha256module;
- v8::Local<v8::String> str;
- if (!args[0]->IsString()) {
- v8::Local<v8::Object> self=args.This();
- v8::Handle<v8::Value> emptyArgs[] = {};
- str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
- } else {
- str=args[0]->ToString();
- }
- String::Utf8Value data(str);
+ String::Utf8Value data(args[0]->ToString());
unsigned char digest[64];
unsigned char hexdigest[64];
SHAobject *sha;
@@ -265,7 +222,7 @@ sha256(const Arguments& args)
sha_final(digest, sha);
make_digest_ex(hexdigest, digest, 32);
-
+
return scope.Close(String::New((char*)hexdigest,64));
}
@@ -274,15 +231,7 @@ sha512(const Arguments& args)
{
HandleScope scope;
using namespace sha512module;
- v8::Local<v8::String> str;
- if (!args[0]->IsString()) {
- v8::Local<v8::Object> self=args.This();
- v8::Handle<v8::Value> emptyArgs[] = {};
- str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
- } else {
- str=args[0]->ToString();
- }
- String::Utf8Value data(str);
+ String::Utf8Value data(args[0]->ToString());
unsigned char digest[128];
unsigned char hexdigest[128];
SHAobject *sha;
@@ -292,7 +241,7 @@ sha512(const Arguments& args)
sha512_final(digest, sha);
make_digest_ex(hexdigest, digest, 64);
-
+
return scope.Close(String::New((char*)hexdigest,128));
}
@@ -301,15 +250,7 @@ md4(const Arguments& args)
{
HandleScope scope;
- v8::Local<v8::String> str;
- if (!args[0]->IsString()) {
- v8::Local<v8::Object> self=args.This();
- v8::Handle<v8::Value> emptyArgs[] = {};
- str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
- } else {
- str=args[0]->ToString();
- }
- String::Utf8Value data(str);
+ String::Utf8Value data(args[0]->ToString());
MD4_CTX mdContext;
unsigned char digest[16];
unsigned char hexdigest[32];
@@ -328,23 +269,15 @@ Handle<Value>
md5(const Arguments& args)
{
HandleScope scope;
- v8::Local<v8::String> str;
- if (!args[0]->IsString()) {
- v8::Local<v8::Object> self=args.This();
- v8::Handle<v8::Value> emptyArgs[] = {};
- str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
- } else {
- str=args[0]->ToString();
- }
- String::Utf8Value data(str);
- int len=data.length();
+
+ String::Utf8Value data(args[0]->ToString());
MD5_CTX mdContext;
unsigned char digest[16];
unsigned char hexdigest[32];
/* make an hash */
MD5Init(&mdContext);
- MD5Update(&mdContext, (unsigned char*)*data, len);
+ MD5Update(&mdContext, (unsigned char*)*data, data.length());
MD5Final(digest, &mdContext);
make_digest_ex(hexdigest, digest, 16);
@@ -357,15 +290,7 @@ md6(const Arguments& args)
{
HandleScope scope;
- v8::Local<v8::String> str;
- if (!args[0]->IsString()) {
- v8::Local<v8::Object> self=args.This();
- v8::Handle<v8::Value> emptyArgs[] = {};
- str=v8::Handle<v8::Function>::Cast(self->Get(String::New("toString")))->Call(self,1,emptyArgs)->ToString();
- } else {
- str=args[0]->ToString();
- }
- String::Utf8Value data(str);
+ String::Utf8Value data(args[0]->ToString());
int len(32);
if (!args[1]->IsUndefined()) {
@@ -379,7 +304,7 @@ md6(const Arguments& args)
if (len%2!=0) half_len++;
make_digest_ex(hexdigest, digest, half_len);
-
+
return scope.Close(String::New((char*)hexdigest,len));
}
@@ -433,6 +358,7 @@ Handle<Value> get_md5_file_async(char * path, void* data)
Handle<Value> get_md5_file(char * path)
{
+ HandleScope scope;
Unlocker unlock;
FILE *inFile = fopen (path, "rb");
MD5_CTX mdContext;
@@ -455,7 +381,7 @@ Handle<Value> get_md5_file(char * path)
make_digest_ex(hexdigest, digest, 16);
fclose (inFile);
Locker lock;
- return String::New((char*)hexdigest,32);
+ return scope.Close(String::New((char*)hexdigest,32));
}
Handle<Value>
@@ -478,7 +404,7 @@ md5_file(const Arguments& args)
file_data *fd=new file_data;
fd->byte = 0;
fd->environment = data;
-
+
MD5Init(&fd->mdContext);
ev_ref(EV_DEFAULT_UC);
return scope.Close(get_md5_file_async(cpath,static_cast<void*>(fd)));
@@ -495,20 +421,6 @@ md5_file(const Arguments& args)
extern "C" void init (Handle<Object> target)
{
HandleScope scope;
-
-
- v8::Local<v8::Object> String = Context::GetCurrent()->Global()->Get(String::New("String"))->ToObject()->GetPrototype()->ToObject()->GetPrototype()->ToObject();
- String->Set(String::New("md4"), FunctionTemplate::New(md4)->GetFunction());
- String->Set(String::New("md5"), FunctionTemplate::New(md5)->GetFunction());
- String->Set(String::New("md6"), FunctionTemplate::New(md6)->GetFunction());
- String->Set(String::New("sha"), FunctionTemplate::New(sha)->GetFunction());
- String->Set(String::New("sha1"), FunctionTemplate::New(sha1)->GetFunction());
- String->Set(String::New("sha256"), FunctionTemplate::New(sha256)->GetFunction());
- String->Set(String::New("sha512"), FunctionTemplate::New(sha512)->GetFunction());
- String->Set(String::New("hmac_sha1"), FunctionTemplate::New(hmac_sha1)->GetFunction());
- String->Set(String::New("hmac_md5"), FunctionTemplate::New(hmac_md5)->GetFunction());
-
-
target->Set(String::New("md4"), FunctionTemplate::New(md4)->GetFunction());
target->Set(String::New("md5"), FunctionTemplate::New(md5)->GetFunction());
target->Set(String::New("md6"), FunctionTemplate::New(md6)->GetFunction());

0 comments on commit 2064ae1

Please sign in to comment.