11#define _GNU_SOURCE_WAS_NOT_DEFINED
28#if defined(__linux__) || defined(__windows__)
32#include <sys/sysinfo.h>
34#ifdef _GNU_SOURCE_WAS_NOT_DEFINED
43static char*
__internal_root_key =
"ENOFGLUCUNZJADRDMCZZSACRBKXAGSLTTFCICPALKHMWWGLVUIFYSLQJIHHXSHOZGRABFNBHGVTOMEBTVIPXZJHHEKIYWNVTWQKERROCTXFGMMLYUJSJFWLFCHQQMMUM";
51 if ((rootkey_size % 2) != 0) {
52 n_log(
LOG_ERR,
"key size %d is not a multiple of 2", rootkey_size);
56 time_t t = time(NULL);
61 srand((
unsigned int)t);
62 for (
size_t it = 0; it < rootkey_size; it++) {
63 output->
data[it] = (char)(
'A' + rand() % 26);
79 bIsRetrieved = GetVolumeInformation(NULL, NULL, 0, &dwVolSerial, NULL, NULL, NULL, 0);
82 nstrprintf(output,
"%X", (
unsigned int)dwVolSerial);
84 n_log(
LOG_ERR,
"Could not retrieve current directory disk serial id");
89 char* curdir = getcwd(NULL, 0);
92 nstrprintf(cmd,
"df -T \"%s\" | awk '/^\\/dev/ {print $1}'", curdir);
95 N_STR* cur_disk = NULL;
100 nstrprintf(cmd,
"udevadm info --query=all --name=%s | egrep '(ID_SERIAL=|ID_FS_UUID=)' | awk -F'=' '{print $2}'",
_nstr(cur_disk));
104 N_STR* hd_serial = NULL;
110 for (
unsigned int it = 0; it < hd_serial->
written; it++) {
111 if (isspace(hd_serial->
data[it])) {
112 hd_serial->
data[it] =
'-';
119 char* curdir = getcwd(NULL, 0);
120 N_STR* hostid = NULL;
122 n_popen(
"hostid", 1024, (
void**)&hostid, &ret);
135 N_STR* cpu_id = NULL;
138 unsigned int a[4] = {0, 0, 0, 0};
140 if (__get_cpuid_max(0, NULL) >= 1) {
141 __get_cpuid(1, &a[0], &a[1], &a[2], &a[3]);
148 memset(&binaryNum, 0,
sizeof(binaryNum));
150 unsigned int n = a[3];
154 binaryNum[i] = n % 2;
162 binaryNum[i] = n % 2;
170 binaryNum[i] = n % 2;
178 binaryNum[i] = n % 2;
191 nstrprintf(cpu_id,
"%d%d%d%d-%d%d%d%d-%d%d%d%d-%d%d", binaryNum[3], binaryNum[2], binaryNum[1], binaryNum[0],
192 binaryNum[7], binaryNum[6], binaryNum[5], binaryNum[4],
193 binaryNum[11], binaryNum[10], binaryNum[9], binaryNum[8],
194 binaryNum[13], binaryNum[12]);
197 static const char devname[] =
"/dev/cpu/self/cpuid";
200 uint32_t r_eax, r_ebx, r_ecx, r_edx;
205 if ((d = open(devname, O_RDONLY)) != -1) {
206 if (pread(d, rp,
sizeof(*rp), 0) ==
sizeof(*rp)) {
207 s = (
char*)&rp->r_ebx;
254 size_t end_pos = params->
end_pos;
257 size_t keyIndexMod = 0;
259 for (
size_t i = start_pos; i < end_pos; i++) {
261 char offset = cIsUpper ?
'A' :
'a';
262 keyIndexMod = i %
key->written;
264 dest->
data[i] = (char)((
n_mathmod(((input->
data[i] + k) - offset), 26)) + offset);
268 for (
size_t i = start_pos; i < end_pos; i++) {
270 char offset = cIsUpper ?
'A' :
'a';
271 keyIndexMod = i %
key->written;
273 dest->
data[i] = (char)((
n_mathmod(((input->
data[i] - k) - offset), 26)) + offset);
300 if ((
key->written % 2) != 0) {
305 for (
size_t i = 0; i <
key->written; ++i) {
306 if (!isalpha(
key->data[i])) {
321 GetSystemInfo(&sysinfo);
322 size_t nb_procs = (size_t)sysinfo.dwNumberOfProcessors;
324 size_t nb_procs = (size_t)get_nprocs();
332 size_t bytesteps = input->
written / nb_procs;
333 if (bytesteps == 0) bytesteps = input->
written;
336 for (
size_t it = 0; it < input->
written; it += bytesteps) {
337 end_pos = it + bytesteps;
344 params->
input = input;
352 n_log(
LOG_ERR,
"Error adding n_vigenere_cypher_thread to thread pool");
357 if (end_pos < input->written) {
360 params->
input = input;
368 n_log(
LOG_ERR,
"Error adding n_vigenere_cypher_thread to thread pool");
481 if ((question_size % 2) != 0) {
482 n_log(
LOG_ERR,
"odd number of elements for question size, %d", question_size);
486 N_STR* output = NULL;
496 output =
new_nstr(question_size + 1);
497 for (it = 0; it < question_size; it++) {
498 int val = tmpoutput->
data[it % tmpoutput->
written];
499 while (val <
'A') val += 26;
500 while (val >
'Z') val -= 26;
501 output->
data[it] = (char)val;
503 output->
written = question_size;
507 output =
new_nstr(question_size + 1);
508 for (it = 0; it < question_size; it++) {
509 int val = rand() % 256;
510 while (val <
'A') val += 26;
511 while (val >
'Z') val -= 26;
512 output->
data[it] = (char)val;
520 while (it < encoded_data->written) {
521 if (encoded_data->
data[it] ==
'=') {
522 encoded_data->
data[it] =
'\0';
547 n_log(
LOG_ERR,
"question key of size %d is not a multiple of root key of size %d", decoded_question->
written, root_key->
written);
553 for (uint32_t it = 0; it < root_key->
written; it++) {
554 int32_t val = (root_key->
data[it % root_key->
written] -
'A') + (decoded_question->
data[it % decoded_question->
written] -
'A');
557 answer->
data[it] = (char)val;
576 if ((
key->written % 2) != 0) {
619 if ((
key->written % 2) != 0) {
663 n_log(
LOG_ERR,
"could not base64 decode: input_data %p", input_data);
668 if (!encoded_with_answer_data) {
669 n_log(
LOG_ERR,
"could not encode: input_data %p", input_data);
677 n_log(
LOG_ERR,
"could not decode: input_data %p", input_data);
704 n_log(
LOG_ERR,
"could not encode input_data %p", input_data);
710 if (!encoded_with_answer_data) {
711 n_log(
LOG_ERR,
"could not decode: input_data %p", input_data);
719 n_log(
LOG_ERR,
"could not base64 decode: input_data %p", input_data);
THREAD_POOL * thread_pool
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define _str(__PTR)
define true
#define Free(__ptr)
Free Handler to get errors.
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
int n_popen(char *cmd, size_t read_buf_size, void **nstr_output, int *ret)
launch a command abd return output and status
char n_tolower(char c)
is_alpha
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
bool n_isupper(char c)
test if char c is uppercase
char n_toupper(char c)
is_alpha
bool n_islower(char c)
test if char c is lowercase
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
N_STR * n_vigenere_encode_qa(N_STR *input_data, N_STR *question, N_STR *answer)
directly vigenere encode a file using key
int n_vigenere_decode_file_qa(N_STR *in, N_STR *out, N_STR *question, N_STR *answer)
directly vigenere decode a file using question and answer
N_STR * n_vigenere_quick_encode(N_STR *decoded_data)
quick encode data
N_STR * n_vigenere_decode(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
#define n_mathmod(__a, __b)
The % operator returns a result that adopts the sign of the dividend, a true mathematical modulus ado...
N_STR * n_vigenere_get_answer(N_STR *root_key, N_STR *question)
get an answer from a root key and a question
int n_vigenere_encode_file_qa(N_STR *in, N_STR *out, N_STR *question, N_STR *answer)
directly vigenere encode a file using question and answer
N_STR * n_vigenere_decode_qa(N_STR *input_data, N_STR *question, N_STR *answer)
directly vigenere decode a file using key
N_STR * n_vigenere_encode(N_STR *string, N_STR *key)
encode input using vigenere cypher and key
int n_vigenere_encode_file(N_STR *in, N_STR *out, N_STR *key)
directly vigenere encode a file using key
int n_vigenere_decode_file(N_STR *in, N_STR *out, N_STR *key)
directly vigenere decode a file using key
N_STR * n_vigenere_get_rootkey(size_t rootkey_size)
get a rootkey randomly generated
N_STR * n_vigenere_get_question(size_t question_size)
get a question generated from the current machine hardware (disk&cpu)
N_STR * n_vigenere_quick_decode(N_STR *encoded_data)
quick decode data
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
size_t written
size of the written data inside the string
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
int nstr_to_file(N_STR *str, char *filename)
Write a N_STR content into a file.
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
#define nstrprintf_cat(__nstr_var, __format,...)
Macro to quickly allocate and sprintf and cat to a N_STR.
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
A box including a string and his lenght.
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
int add_threaded_process(THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode)
add a function and params to a thread pool
int refresh_thread_pool(THREAD_POOL *thread_pool)
try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing
int destroy_threaded_pool(THREAD_POOL **pool, unsigned int delay)
delete a thread_pool, exit the threads and free the structs
#define DIRECT_PROC
processing mode for added func, direct start, not queued
int wait_for_threaded_pool(THREAD_POOL *thread_pool, unsigned int delay)
Wait for all the launched process in the thread pool to terminate.
Structure of a trhead pool.
Base64 encoding and decoding functions using N_STR.
Common headers and low-level functions & define.
static char * __internal_root_key
static internal root key, just to obfuscate text so we do not care if is is public
N_STR * n_vigenere_cypher(N_STR *input, N_STR *key, bool encipher, bool in_place)
encode or decode input using vigenere cypher
bool encipher
1 encoding, 0 decoding
void * n_vigenere_cypher_thread(void *ptr)
encode or decode threaded func using params from ptr
N_STR * n_vigenere_decode_in_place(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
N_STR * n_get_current_dir_hd_serial()
get the serial of the current running dir hosting disk
size_t start_pos
input starting point
N_STR * key
encoding/decoding key
N_STR * n_vigenere_encode_in_place(N_STR *string, N_STR *key)
encode string in place using vigenere cypher and key
size_t end_pos
output starting point
N_STR * n_get_cpu_id()
get the CPU id
structure of a n_vigenere_cypher_thread param
Vigenere encoding and decoding functions using N_STR/files.