Added a HWID generation class
This commit is contained in:
parent
453802698e
commit
64da306f18
3
bugs
3
bugs
@ -22,3 +22,6 @@ Tasks designer:
|
||||
|
||||
Notice:
|
||||
electron-package-manager must be at 8.7.2 (Node 6 support)!
|
||||
|
||||
|
||||
FIXME: Test the new voice resampler!
|
@ -4,6 +4,7 @@ set(MODULE_NAME "teaclient_connection")
|
||||
set(SOURCE_FILES
|
||||
src/logger.cpp
|
||||
src/EventLoop.cpp
|
||||
src/hwuid.cpp
|
||||
|
||||
src/connection/ft/FileTransferManager.cpp
|
||||
src/connection/ft/FileTransferObject.cpp
|
||||
@ -139,3 +140,8 @@ target_compile_definitions(${MODULE_NAME} PUBLIC -DNODEJS_API)
|
||||
|
||||
add_executable(Audio-Test ${SOURCE_FILES} test/audio/main.cpp)
|
||||
target_link_libraries(Audio-Test ${REQUIRED_LIBRARIES})
|
||||
|
||||
add_executable(HW-UID-Test src/hwuid.cpp)
|
||||
target_link_libraries(HW-UID-Test
|
||||
${REQUIRED_LIBRARIES}
|
||||
)
|
129
native/serverconnection/src/hwuid.cpp
Normal file
129
native/serverconnection/src/hwuid.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
#include "hwuid.h"
|
||||
#include <tomcrypt.h>
|
||||
#include <array>
|
||||
#include <fstream>
|
||||
#include <misc/strobf.h>
|
||||
#include <misc/base64.h>
|
||||
|
||||
#ifndef WIN32
|
||||
#ifdef DARWIN
|
||||
/*
|
||||
#include <mach-o/arch.h>
|
||||
unsigned short getCpuHash() {
|
||||
const NXArchInfo* info = NXGetLocalArchInfo();
|
||||
unsigned short val = 0;
|
||||
val += (unsigned short)info->cputype;
|
||||
val += (unsigned short)info->cpusubtype;
|
||||
return val;
|
||||
}
|
||||
*/
|
||||
#else // !DARWIN
|
||||
static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
|
||||
unsigned int *ecx, unsigned int *edx)
|
||||
{
|
||||
/* ecx is often an input as well as an output. */
|
||||
asm volatile("cpuid"
|
||||
: "=a" (*eax),
|
||||
"=b" (*ebx),
|
||||
"=c" (*ecx),
|
||||
"=d" (*edx)
|
||||
: "0" (*eax), "2" (*ecx));
|
||||
}
|
||||
|
||||
uint32_t calculate_cpu_hash() {
|
||||
uint32_t cpuinfo[4] = { 0, 0, 0, 0 };
|
||||
native_cpuid(cpuinfo, cpuinfo + 1, cpuinfo + 2, cpuinfo + 3);
|
||||
|
||||
uint32_t hash = 0;
|
||||
uint32_t* ptr = (&cpuinfo[0]);
|
||||
for (uint32_t i = 0; i < 4; i++)
|
||||
hash += ptr[i];
|
||||
|
||||
return hash;
|
||||
}
|
||||
#endif // !DARWIN
|
||||
|
||||
constexpr uint8_t hex_value(char c) {
|
||||
if(c >= '0' && c <= '9')
|
||||
return c - '0';
|
||||
if(c >= 'a' && c <= 'f')
|
||||
return c - 'A' + 10;
|
||||
if(c >= 'A' && c <= 'F')
|
||||
return c - '0' + 10;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool read_machine_id(uint8_t(&uuid)[16]) {
|
||||
strobf_define(_path, "/etc/machine-id");
|
||||
|
||||
memset(uuid, 0, 16);
|
||||
std::ifstream in(strobf_val(_path).string());
|
||||
if(in) {
|
||||
char buffer[32];
|
||||
if(!in.read(buffer, 32)) return false;
|
||||
|
||||
auto it = buffer;
|
||||
auto index = 0;
|
||||
while(index < 16) {
|
||||
uuid[index] = hex_value(*it) << 4UL;
|
||||
uuid[index] = hex_value(*it);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool generate_uuid(std::string& result, uint32_t& check_sum) {
|
||||
uint8_t buffer[16];
|
||||
if(!read_machine_id(buffer))
|
||||
memcpy(buffer, "AAAABBBBCCCCDDDD", 16);
|
||||
|
||||
auto cpu_hash = calculate_cpu_hash();
|
||||
auto it = (uint32_t*) buffer;
|
||||
for(int i = 0; i < 4; i++)
|
||||
*it++ = cpu_hash;
|
||||
|
||||
result = base64::encode((char*) buffer, 16);
|
||||
|
||||
{
|
||||
crc32_state state;
|
||||
crc32_init(&state);
|
||||
crc32_update(&state, (u_char*) result.data(), result.length());
|
||||
|
||||
crc32_finish(&state, &check_sum, sizeof(check_sum));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#else
|
||||
#error Implement me!
|
||||
#endif
|
||||
|
||||
inline bool check_uuid(std::string& uuid, uint32_t check_sum) {
|
||||
crc32_state state;
|
||||
crc32_init(&state);
|
||||
crc32_update(&state, (u_char*) uuid.data(), uuid.length());
|
||||
|
||||
uint32_t result;
|
||||
crc32_finish(&state, &result, sizeof(result));
|
||||
|
||||
return result == check_sum;
|
||||
}
|
||||
|
||||
static std::string _cached_system_uuid{};
|
||||
static uint32_t _cached_system_uuid_cksm = 0;
|
||||
std::string system_uuid() {
|
||||
if(!_cached_system_uuid.empty() && check_uuid(_cached_system_uuid, _cached_system_uuid_cksm))
|
||||
return _cached_system_uuid;
|
||||
|
||||
if(!generate_uuid(_cached_system_uuid, _cached_system_uuid_cksm))
|
||||
_cached_system_uuid = "";
|
||||
return _cached_system_uuid;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "UUID: " << system_uuid() << "\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
//ADaX2mIRrC1uv83h
|
4
native/serverconnection/src/hwuid.h
Normal file
4
native/serverconnection/src/hwuid.h
Normal file
@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
extern std::string system_uuid();
|
Loading…
Reference in New Issue
Block a user