-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc.cc
More file actions
69 lines (65 loc) · 1.63 KB
/
src.cc
File metadata and controls
69 lines (65 loc) · 1.63 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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
extern "C"
{
#include "md5.h"
}
#include <napi.h>
#include <string>
using namespace std;
Napi::String md5_file(const Napi::CallbackInfo &info)
{
uint8_t *result;
Napi::Env env = info.Env();
std::string path_str = info[0].As<Napi::String>();
if (path_str.length() == 0)
{
return Napi::String::New(env, "Error::Argument Not Set");
}
const char *path = path_str.c_str();
FILE *fptr = fopen(path, "rb");
if (fptr == NULL)
{
return Napi::String::New(env, "Error::Invalid File");
}
result = md5File(fptr);
char md5_hash[33];
for (int i = 0; i < 16; i++)
{
sprintf(&md5_hash[2 * i], "%02X", result[i]);
}
md5_hash[32] = '\0';
fclose(fptr);
free(result);
return Napi::String::New(env, md5_hash);
}
Napi::String md5_str(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
uint8_t *result;
std::string str = info[0].As<Napi::String>();
int len = str.length();
if (len == 0)
{
return Napi::String::New(env, "Error::Argument Not Set");
}
char *src = (char *)malloc(sizeof(char *) * (len + 1));
strcpy(src, str.c_str());
result = md5String(src);
char md5_hash[33];
for (int i = 0; i < 16; i++)
{
sprintf(&md5_hash[2 * i], "%02X", result[i]);
}
md5_hash[32] = '\0';
free(src);
return Napi::String::New(env, md5_hash);
}
Napi::Object Init(Napi::Env env, Napi::Object exports)
{
exports.Set(Napi::String::New(env, "md5File"), Napi::Function::New(env, md5_file));
exports.Set(Napi::String::New(env, "md5Str"), Napi::Function::New(env, md5_str));
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init);