#include "Utils.h" #include // for strtol #include // for operator<<, setfill, setw #include // for basic_ostream, hex #include // for stringstream #include // for string #include // for enable_if<>::type #include #ifndef _WIN32 #include #endif unsigned long long getCurrentTimestamp() { return std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) .count(); } uint64_t hton64(uint64_t value) { int num = 42; if (*(char*)&num == 42) { uint32_t high_part = htonl((uint32_t)(value >> 32)); uint32_t low_part = htonl((uint32_t)(value & 0xFFFFFFFFLL)); return (((uint64_t)low_part) << 32) | high_part; } else { return value; } } std::vector stringHexToBytes(const std::string& s) { std::vector v; v.reserve(s.length() / 2); for (std::string::size_type i = 0; i < s.length(); i += 2) { std::string byteString = s.substr(i, 2); uint8_t byte = (uint8_t)strtol(byteString.c_str(), NULL, 16); v.push_back(byte); } return v; } std::string bytesToHexString(const std::vector& v) { std::stringstream ss; ss << std::hex << std::setfill('0'); std::vector::const_iterator it; for (it = v.begin(); it != v.end(); it++) { ss << std::setw(2) << static_cast(*it); } return ss.str(); } std::vector bigNumAdd(std::vector num, int n) { auto carry = n; for (int x = num.size() - 1; x >= 0; x--) { int res = num[x] + carry; if (res < 256) { carry = 0; num[x] = res; } else { // Carry the rest of the division carry = res / 256; num[x] = res % 256; // extend the vector at the last index if (x == 0) { num.insert(num.begin(), carry); return num; } } } return num; } std::vector bigNumDivide(std::vector num, int n) { auto carry = 0; for (int x = 0; x < num.size(); x++) { int res = num[x] + carry * 256; if (res < n) { carry = res; num[x] = 0; } else { // Carry the rest of the division carry = res % n; num[x] = res / n; } } return num; } std::vector bigNumMultiply(std::vector num, int n) { auto carry = 0; for (int x = num.size() - 1; x >= 0; x--) { int res = num[x] * n + carry; if (res < 256) { carry = 0; num[x] = res; } else { // Carry the rest of the division carry = res / 256; num[x] = res % 256; // extend the vector at the last index if (x == 0) { num.insert(num.begin(), carry); return num; } } } return num; } unsigned char h2int(char c) { if (c >= '0' && c <= '9') { return ((unsigned char)c - '0'); } if (c >= 'a' && c <= 'f') { return ((unsigned char)c - 'a' + 10); } if (c >= 'A' && c <= 'F') { return ((unsigned char)c - 'A' + 10); } return (0); } std::string urlDecode(std::string str) { std::string encodedString = ""; char c; char code0; char code1; for (int i = 0; i < str.length(); i++) { c = str[i]; if (c == '+') { encodedString += ' '; } else if (c == '%') { i++; code0 = str[i]; i++; code1 = str[i]; c = (h2int(code0) << 4) | h2int(code1); encodedString += c; } else { encodedString += c; } } return encodedString; }