Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 185 lines (147 sloc) 4.367 kb
4f02645 @brainfucker uploading project
authored
1 #include <v8.h>
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
2
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
3 //#include "lib/md5/md5.c"
4 extern "C" {
5 #include "sha.h"
6 #include "md4.h"
7 #include "md5.h"
8 }
4f02645 @brainfucker uploading project
authored
9
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
10 #include "lib/sha/shamodule.c"
11 #include "lib/sha/sha256module.c"
12 #include "lib/sha/sha512module.c"
13
14 #include "lib/md6/md6.h"
15 #include "lib/md6/md6_compress.c"
16 #include "lib/md6/md6_mode.c"
17
4f02645 @brainfucker uploading project
authored
18 #include <iostream>
19 using namespace v8;
20
98580af @brainfucker shitch to make_digest from PHP
authored
21 // make digest from php
22 static void
23 make_digest_ex(unsigned char *md5str, unsigned char *digest, int len)
24 {
25 static const char hexits[17] = "0123456789abcdef";
26 int i;
27
28 for (i = 0; i < len; i++) {
29 md5str[i * 2] = hexits[digest[i] >> 4];
30 md5str[(i * 2) + 1] = hexits[digest[i] & 0x0F];
31 }
32 md5str[len * 2] = '\0';
33 }
34
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
35 Handle<Value>
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
36 sha(const Arguments& args)
37 {
38 HandleScope scope;
39 String::Utf8Value data(args[0]->ToString());
40 SHA_CTX ctx;
41 unsigned char digest[20];
42 unsigned char hexdigest[40];
43
44 SHA_Init(&ctx);
45 SHA_Update(&ctx, (unsigned char*)*data, data.length());
46 SHA_Final(digest, &ctx);
47
48 make_digest_ex(hexdigest, digest, 20);
49
50 return String::New((char*)hexdigest,40);
51 }
52
53 Handle<Value>
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
54 sha1(const Arguments& args)
55 {
56 HandleScope scope;
57 using namespace sha1module;
58 String::Utf8Value data(args[0]->ToString());
59 unsigned char digest[40];
60 unsigned char hexdigest[40];
61 SHAobject *sha;
62 sha=new SHAobject;
63 sha_init(sha);
64 sha_update(sha, (unsigned char*) *data, data.length());
65 sha_final(digest, sha);
98580af @brainfucker shitch to make_digest from PHP
authored
66
67 make_digest_ex(hexdigest, digest, 20);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
68
69 return String::New((char*)hexdigest,40);
70 }
71
4f02645 @brainfucker uploading project
authored
72
73 Handle<Value>
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
74 sha256(const Arguments& args)
75 {
76 HandleScope scope;
77 using namespace sha256module;
78 String::Utf8Value data(args[0]->ToString());
79 unsigned char digest[64];
80 unsigned char hexdigest[64];
81 SHAobject *sha;
82 sha=new SHAobject;
83 sha_init(sha);
84 sha_update(sha, (unsigned char*) *data, data.length());
85 sha_final(digest, sha);
98580af @brainfucker shitch to make_digest from PHP
authored
86
87 make_digest_ex(hexdigest, digest, 32);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
88
89 return String::New((char*)hexdigest,64);
90 }
91
92 Handle<Value>
93 sha512(const Arguments& args)
94 {
95 HandleScope scope;
96 using namespace sha512module;
97 String::Utf8Value data(args[0]->ToString());
98 unsigned char digest[128];
99 unsigned char hexdigest[128];
100 SHAobject *sha;
101 sha=new SHAobject;
102 sha512_init(sha);
103 sha512_update(sha, (unsigned char*) *data, data.length());
104 sha512_final(digest, sha);
98580af @brainfucker shitch to make_digest from PHP
authored
105
106 make_digest_ex(hexdigest, digest, 64);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
107
108 return String::New((char*)hexdigest,128);
109 }
110
111 Handle<Value>
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
112 md4(const Arguments& args)
4f02645 @brainfucker uploading project
authored
113 {
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
114 HandleScope scope;
4f02645 @brainfucker uploading project
authored
115
116 String::Utf8Value data(args[0]->ToString());
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
117 MD4_CTX mdContext;
118 unsigned char digest[16];
119 unsigned char hexdigest[32];
98580af @brainfucker shitch to make_digest from PHP
authored
120
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
121 /* make an hash */
122 MD4Init(&mdContext);
123 MD4Update(&mdContext, (unsigned char*)*data, data.length());
124 MD4Final(digest, &mdContext);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
125
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
126 make_digest_ex(hexdigest, digest, 16);
127
128 return String::New((char*)hexdigest,32);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
129 }
4f02645 @brainfucker uploading project
authored
130
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
131 Handle<Value>
132 md5(const Arguments& args)
133 {
134 HandleScope scope;
135
136 String::Utf8Value data(args[0]->ToString());
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
137 MD5_CTX mdContext;
4f02645 @brainfucker uploading project
authored
138 unsigned char digest[16];
139 unsigned char hexdigest[32];
140
141 /* make an hash */
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
142 MD5Init(&mdContext);
143 MD5Update(&mdContext, (unsigned char*)*data, data.length());
144 MD5Final(digest, &mdContext);
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
145
98580af @brainfucker shitch to make_digest from PHP
authored
146 make_digest_ex(hexdigest, digest, 16);
4f02645 @brainfucker uploading project
authored
147
148 return String::New((char*)hexdigest,32);
149 }
150
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
151 Handle<Value>
152 md6(const Arguments& args)
153 {
154 HandleScope scope;
155
156 String::Utf8Value data(args[0]->ToString());
157
158 int len(32);
159 if (!args[1]->IsUndefined()) {
160 len=args[1]->ToInteger()->Value();
161 }
162 unsigned char digest[len];
163 unsigned char hexdigest[len];
164 md6_hash(len*8, (unsigned char*) *data, data.length(), digest);
165
166 int half_len=len/2;
167 if (len%2!=0) half_len++;
168
169 make_digest_ex(hexdigest, digest, half_len);
170
171 return String::New((char*)hexdigest,len);
172 }
173
4f02645 @brainfucker uploading project
authored
174 extern "C" void init (Handle<Object> target)
175 {
176 HandleScope scope;
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
177 target->Set(String::New("md4"), FunctionTemplate::New(md4)->GetFunction());
4f02645 @brainfucker uploading project
authored
178 target->Set(String::New("md5"), FunctionTemplate::New(md5)->GetFunction());
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
179 target->Set(String::New("md6"), FunctionTemplate::New(md6)->GetFunction());
67c3b1f @brainfucker move to freeBSD md5 lib, add md4, sha
authored
180 target->Set(String::New("sha"), FunctionTemplate::New(sha)->GetFunction());
d79aad2 @brainfucker Adding sha1, sha256, sha512 and md6 hashes
authored
181 target->Set(String::New("sha1"), FunctionTemplate::New(sha1)->GetFunction());
182 target->Set(String::New("sha256"), FunctionTemplate::New(sha256)->GetFunction());
183 target->Set(String::New("sha512"), FunctionTemplate::New(sha512)->GetFunction());
4f02645 @brainfucker uploading project
authored
184 }
Something went wrong with that request. Please try again.