-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmlib.cc
185 lines (163 loc) · 5.34 KB
/
wmlib.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* FAST nodejs(http://github.com/ry/node/) library for making hashes
*
* @package wmlib
* @link http://github.com/brainfucker/hashlib
* @autor Oleg Illarionov <[email protected]>
* @version 1.0
*/
#include <iostream>
#include <stdio.h>
#include <v8.h>
#include <ev.h>
#include <eio.h>
#include <fcntl.h>
#include "stdafx.h"
#include "stdio.h"
#include "signer.h"
#include <errno.h>
#include <stdlib.h>
#include "base64.h"
#include "cmdbase.h"
using namespace v8;
void NormStr(char *str);
static char pszOut[MAXBUF + 1] = "";
Handle<Value> sign(const Arguments& args) {
HandleScope scope;
String::Utf8Value uLogin(args[0]->ToString());
String::Utf8Value uPwd(args[1]->ToString());
String::Utf8Value uKeyData(args[2]->ToString());
String::Utf8Value uSignData(args[3]->ToString());
szptr szLogin, szPwd, szFileName, szIn, szSign;
char szBufforInv[MAXSTR + 1] = "";
char szKeyData[MAXBUF + 1] = ""; // Buffer for Signre-s key
int ErrorCode = 0;
bool result = FALSE;
char szLoginCL[MAXSTR+1] = "";
char szPwdCL[MAXSTR+1] = "";
strncpy( szLoginCL, (char*) *uLogin, MAXSTR);
strncpy( szPwdCL, (char*) *uPwd, MAXSTR);
szLogin = szLoginCL;
szPwd = szPwdCL;
size_t Bytes = 0;
char KeyBuffer[512];
if (strlen((char*) *uKeyData) != 220)
return scope.Close(ThrowException(Exception::Error(String::New((char*)"Key string has illegal length!"))));
strcpy(szKeyData, (char*) *uKeyData);
Bytes = code64(ENCODE, KeyBuffer, 512, szKeyData, 220);
if (Bytes != 164)
return scope.Close(ThrowException(Exception::Error(String::New((char*)"Bad key string in parameter!"))));
memcpy(szKeyData, KeyBuffer, 164);
if( strlen( (char*) *uSignData )) {
strncpy( szBufforInv, (char*) *uSignData, MAXSTR);
}
NormStr(szBufforInv);
szIn = szBufforInv;
//----------------------------------------------------
// sigining (new)
Signer sign(szLogin, szPwd, szFileName);
sign.isIgnoreKeyFile = false;
sign.isIgnoreIniFile = true;
sign.isKWMFileFromCL = false;
sign.Key64Flag = true;
sign.SetKeyFromCL(TRUE, szKeyData);
result = sign.Sign(szIn, szSign);
ErrorCode = sign.ErrorCode();
if (result) {
strncpy(pszOut, szSign, MAXSTR);
//printf("%s", pszOut);
return scope.Close(String::New((char*) pszOut));
} else {
sprintf(pszOut, "WMSigner Error: %d\n", ErrorCode);
//printf("%s", pszOut);
return scope.Close(ThrowException(Exception::Error(String::New(pszOut))));
}
}
//Handle<Value> md6(const Arguments& args) {
// HandleScope scope;
//
// String::Utf8Value data(args[0]->ToString());
//
// int len(32);
// if (!args[1]->IsUndefined()) {
// len=args[1]->ToInteger()->Value();
// }
// unsigned char digest[len];
// unsigned char hexdigest[len];
// md6_hash(len*8, (unsigned char*) *data, data.length(), digest);
//
// int half_len=len/2;
// if (len%2!=0) half_len++;
//
// make_digest_ex(hexdigest, digest, half_len);
//
// return scope.Close(String::New((char*)hexdigest,len));
//}
//int read_cb(eio_req *req) {
// file_data *fd=(file_data *)req->data;
// unsigned char *buf = (unsigned char *)EIO_BUF (req);
// int bytes=(int)EIO_RESULT(req);
// MD5Update (&fd->mdContext, buf, bytes);
// if (bytes==10240) {
// // Read next block
// fd->byte+=bytes;
// eio_read(fd->fd, 0, 10240, fd->byte, EIO_PRI_DEFAULT, read_cb, static_cast<void*>(fd));
// } else {
// // Final
// unsigned char digest[16];
// unsigned char hexdigest[32];
// MD5Final(digest, &fd->mdContext);
// make_digest_ex(hexdigest, digest, 16);
//
// Persistent<Object> *data = reinterpret_cast<Persistent<Object>*>(fd->environment);
//
// v8::Handle<v8::Function> callback = v8::Handle<v8::Function>::Cast((*data)->Get(String::New("callback")));
// Handle<Object> recv = Handle<Object>::Cast((*data)->Get(String::New("recv")));
// v8::Handle<v8::Value> outArgs[] = {String::New((char *)hexdigest,32)};
// callback->Call(recv, 1, outArgs);
// data->Dispose();
// eio_close(fd->fd, 0, 0, (void*)"close");
// ev_unref(EV_DEFAULT_UC);
// }
//
// return 0;
//}
//Handle<Value> get_md5_file_async(char * path, void* data) {
// eio_open (path, O_RDONLY, 0777, 0, open_cb, data);
// return v8::Boolean::New(true);
//}
//Handle<Value> md5_file(const Arguments& args) {
// HandleScope scope;
// struct stat stFileInfo;
// String::Utf8Value path(args[0]->ToString());
// char* cpath=*path;
// int intStat = stat(cpath,&stFileInfo);
// if (intStat == 0) {
// if (args[1]->IsFunction()) {
// v8::Local<v8::Object> arguments = v8::Object::New();
// arguments->Set(String::New("path"),args[0]->ToString());
// arguments->Set(String::New("callback"),args[1]);
// arguments->Set(String::New("recv"),args.This());
// Persistent<Object> *data = new Persistent<Object>();
// *data = Persistent<Object>::New(arguments);
//
// 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)));
// } else {
// return scope.Close(get_md5_file(cpath));
// }
// } else {
// std::string s="Cannot read ";
// s+=cpath;
// return scope.Close(ThrowException(Exception::Error(String::New(s.c_str()))));
// }
//}
extern "C" void init(Handle<Object> target) {
HandleScope scope;
target->Set(String::New("sign"), FunctionTemplate::New(sign)->GetFunction());
}