-
Notifications
You must be signed in to change notification settings - Fork 0
/
fingerprint.cc
42 lines (36 loc) · 1.06 KB
/
fingerprint.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
#include <napi.h>
#include <iostream>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <string>
std::string hash(std::string const& s) {
const int p = 31;
const int m = 1e9 + 9;
long long hash_value = 0;
long long p_pow = 1;
for (char c : s) {
hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
p_pow = (p_pow * p) % m;
}
return std::to_string(hash_value);
}
std::string getFringerprint(void) {
char buffer[1024];
size_t size = sizeof(buffer);
if (sysctlbyname("machdep.cpu.brand_string", &buffer, &size, NULL, 0) < 0) {
return "";
}
std::string res = buffer;
return hash(res);
}
Napi::String Method(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string fringerprint = getFringerprint();
return Napi::String::New(env, fringerprint);
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "getFringerprint"),
Napi::Function::New(env, Method));
return exports;
}
NODE_API_MODULE(fringerprint, Init)