-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
79 lines (68 loc) · 2.01 KB
/
utils.h
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
#pragma once
#include <cstdlib>
#include <cstdint>
#include <string>
#include <iostream>
#include <cstring>
using block_t = uint8_t*;
std::pair<uint8_t*, size_t> convert_str_to_bytes(const std::string& str) {
auto result = new uint8_t[str.length() * 2];
for (size_t i = 0; i < str.length(); i++) {
result[i * 2] = ((uint8_t)str[i]) >> 4;
}
return std::make_pair(result, str.length() * 2);
}
void print_hex(const uint8_t* str, int length, const std::string& msg="") {
std::cout.flags(std::ios_base::hex);
std::cout << msg;
for (int i = 0; i < length; i++) {
std::cout << ((int)str[i] >> 4) << ((int)str[i] % (1<<4));
}
std::cout << std::endl;
}
uint8_t* concat(const uint8_t* str, int length) {
auto result = new uint8_t[length / 2];
for (int i = 0; i < length / 2; i++) {
result[i] = (str[2 * i] << 4) | str[2 * i + 1];
}
return result;
}
uint8_t* parse_hex_string(const std::string& str) {
auto result = new uint8_t[str.length()];
char chr;
for (size_t i = 0; i < str.length(); i++) {
chr = (char)tolower(str[i]);
if (isdigit(chr)) {
result[i] = (uint8_t)(chr - '0');
} else {
result[i] = (uint8_t)(chr - 'a' + 10);
}
}
return concat(result, (int)str.length());
}
block_t copy(const block_t block, const size_t length = 16) {
auto result = new uint8_t[length];
memcpy(result + length - 16, block, 16);
return result + length - 16;
}
template <typename T>
T* add2(T *block, T *a) {
for (int i = 0; i < 16 / sizeof(T); i++) {
block[i] ^= a[i];
}
return block;
}
__uint128_t convert128(const block_t data) {
static uint8_t arr[16];
memcpy(arr, data, 16);
for(int i = 0; i < 8; i++) {
std::swap(arr[i], arr[15 - i]);
}
return *((__uint128_t*)arr);
}
void deconvert128(const __uint128_t value, uint8_t *data) {
memcpy(data, (uint8_t*)(&value), 16);
for (int i = 0; i < 8; i++) {
std::swap(data[i], data[15 - i]);
}
}