Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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