-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cc
More file actions
195 lines (150 loc) · 6.37 KB
/
functions.cc
File metadata and controls
195 lines (150 loc) · 6.37 KB
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
186
187
188
189
190
191
192
193
194
195
#include "functions.h"
#include <iostream>
#include "v8.h"
#include <string.h>
typedef struct {
unsigned char encxkey[261]; // static key
int offset; // everything decrypted till now (total)
int start; // where starts the buffer (so how much big is the header), this is the only one you need to zero
} enctypex_data_t;
extern "C" int enctypex_quick_encrypt(unsigned char *key, unsigned char *validate, unsigned char *data, size_t size);
extern "C" unsigned char *enctypex_encoder(unsigned char *key, unsigned char *validate, unsigned char *data, int *datalen, enctypex_data_t *enctypex_data);
extern "C" int enctype2_encoder(unsigned char *key, unsigned char *data, int size);
using namespace std;
using namespace v8;
using namespace node;
NAN_METHOD(encode) {
// Validate things
if (info.Length() < 4) return;
if (!info[3]->IsFunction()) return;
Nan::Callback callback(Local<Function>::Cast(info[3]));
if (info[0].IsEmpty() || !info[0]->IsObject()){
Local<Value> argv[1] = { Nan::New<String>("Argument 1 (inputData) is empty or not a Buffer").ToLocalChecked() };
callback.Call(1, argv);
return;
}
if (info[1].IsEmpty() || !info[1]->IsObject()){
Local<Value> argv[1] = { Nan::New<String>("Argument 2 (key) is empty or not a Buffer").ToLocalChecked() };
callback.Call(1, argv);
return;
}
if (info[2].IsEmpty() || !info[2]->IsObject()){
Local<Value> argv[1] = { Nan::New<String>("Argument 3 (validate) is empty or not a Buffer").ToLocalChecked() };
callback.Call(1, argv);
return;
}
// get the key from buffer argument
Local<Object> keyBuffer = info[1]->ToObject();
int len = Buffer::Length(keyBuffer);
unsigned char* key = new unsigned char[len + 1];
memcpy((void*)key, (void*)Buffer::Data(keyBuffer), len);
key[len] = 0;
// get the validate from buffer argument
Local<Object> validateBuffer = info[2]->ToObject();
len = Buffer::Length(validateBuffer);
unsigned char* validate = new unsigned char[len + 1];
memcpy((void*)validate, (void*)Buffer::Data(validateBuffer), len);
validate[len] = 0;
// get our data from the buffer
Local<Object> dataBuffer = info[0]->ToObject();
int dataLen = Buffer::Length(dataBuffer);
unsigned char* data = new unsigned char[dataLen + 1];
memcpy((void*)data, (void*)Buffer::Data(dataBuffer), dataLen);
data[dataLen] = 0;
// realloc to account for header the following method will inject
data = (unsigned char*)realloc(data, dataLen + 23);
int newSize = enctypex_quick_encrypt(key, validate, data, dataLen);
delete key;
delete validate;
// Call our callback
Local<Value> argv[2] = { Nan::Null(), Nan::NewBuffer((char*)data, newSize).ToLocalChecked() };
callback.Call(2, argv);
}
NAN_METHOD(decode) {
// Validate things
if (info.Length() < 4) return;
if (!info[3]->IsFunction()) return;
Nan::Callback callback(Local<Function>::Cast(info[3]));
if (info[0].IsEmpty() || !info[0]->IsObject()){
Local<Value> argv[1] = { Nan::New<String>("Argument 1 (inputData) is empty or not a Buffer").ToLocalChecked() };
callback.Call(1, argv);
return;
}
if (info[1].IsEmpty() || !info[1]->IsObject()){
Local<Value> argv[1] = { Nan::New<String>("Argument 2 (key) is empty or not a Buffer").ToLocalChecked() };
callback.Call(1, argv);
return;
}
if (info[2].IsEmpty() || !info[2]->IsObject()){
Local<Value> argv[1] = { Nan::New<String>("Argument 3 (validate) is empty or not a Buffer").ToLocalChecked() };
callback.Call(1, argv);
return;
}
// get the key from buffer argument
Local<Object> keyBuffer = info[1]->ToObject();
int len = Buffer::Length(keyBuffer);
unsigned char* key = new unsigned char[len + 1];
memcpy((void*)key, (void*)Buffer::Data(keyBuffer), len);
key[len] = 0;
// get the validate from buffer argument
Local<Object> validateBuffer = info[2]->ToObject();
len = Buffer::Length(validateBuffer);
unsigned char* validate = new unsigned char[len + 1];
memcpy((void*)validate, (void*)Buffer::Data(validateBuffer), len);
validate[len] = 0;
// get our data from the buffer
Local<Object> dataBuffer = info[0]->ToObject();
int dataLen = Buffer::Length(dataBuffer);
unsigned char* data = new unsigned char[dataLen + 1];
memcpy((void*)data, (void*)Buffer::Data(dataBuffer), dataLen);
data[dataLen] = 0;
// realloc to account for header the following method will inject
data = enctypex_encoder(key, validate, data, &dataLen, NULL);
delete key;
delete validate;
// Call our callback
Local<Value> argv[2] = { Nan::Null(), Nan::NewBuffer((char*)data, strlen((char*)data)).ToLocalChecked() };
callback.Call(2, argv);
}
/*
*
*
* ALL WELCOME ENCTYPE2
*
*
*/
NAN_METHOD(encode2) {
// Validate things
if (info.Length() < 4) return;
if (!info[3]->IsFunction()) return;
Nan::Callback callback(Local<Function>::Cast(info[3]));
if (info[0].IsEmpty() || !info[0]->IsObject()){
Local<Value> argv[1] = { Nan::New<String>("Argument 1 (inputData) is empty or not a Buffer").ToLocalChecked() };
callback.Call(1, argv);
return;
}
if (info[1].IsEmpty() || !info[1]->IsObject()){
Local<Value> argv[1] = { Nan::New<String>("Argument 2 (key) is empty or not a Buffer").ToLocalChecked() };
callback.Call(1, argv);
return;
}
// get the key from buffer argument
Local<Object> keyBuffer = info[1]->ToObject();
int len = Buffer::Length(keyBuffer);
unsigned char* key = new unsigned char[len + 1];
memcpy((void*)key, (void*)Buffer::Data(keyBuffer), len);
key[len] = 0;
// get our data from the buffer
Local<Object> dataBuffer = info[0]->ToObject();
int dataLen = Buffer::Length(dataBuffer);
unsigned char* data = new unsigned char[dataLen + 1];
memcpy((void*)data, (void*)Buffer::Data(dataBuffer), dataLen);
data[dataLen] = 0;
// realloc to account for header the following method will inject
data = (unsigned char*)realloc(data, dataLen + 15);
int newSize = enctype2_encoder(key, data, dataLen);
delete key;
// Call our callback
Local<Value> argv[2] = { Nan::Null(), Nan::NewBuffer((char*)data, newSize).ToLocalChecked() };
callback.Call(2, argv);
}