Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <algorithm>
- #include <functional>
- #include <iomanip>
- #include <iostream>
- #include <random>
- #include <sstream>
- #include <string>
- #include <vector>
- uint32_t generate_name_checksum(std::string const& name)
- {
- uint32_t key = 0xDEADB00B;
- uint32_t checksum = 0;
- for (std::size_t i = 0; i < name.length(); i++)
- {
- checksum += name.at(i) ^ key;
- key = _rotr(key, 8);
- }
- return checksum;
- }
- uint32_t generate_serial_checksum(std::string const& serial)
- {
- std::vector<uint8_t> digits(serial.begin(), serial.end());
- std::for_each(digits.begin(), digits.end(), [](uint8_t& ch) -> void
- {
- if (ch >= 'A' && ch <= 'F')
- {
- ch += 3;
- if (ch > 'F')
- ch += ('A' - 'F');
- }
- });
- uint32_t checksum_key = 0;
- for (uint32_t i = 0, key = 0; i < digits.size(); i++)
- {
- uint8_t digit = digits.at(i);
- checksum_key = key + (digit <= '9' ? (digit - '0') : (digit - '7'));
- key = (checksum_key << 4);
- }
- uint32_t xor_mutator = (_rotr(checksum_key & 0xF0000000, 24) >> 4);
- uint32_t checksum = (xor_mutator << 4);
- for (std::size_t i = 1, j = (checksum_key << 4); i < 8; i++, j <<= 4)
- {
- uint32_t xor_key = _rotr(j & 0xF0000000, 24) >> 4;
- checksum += (xor_mutator ^ xor_key);
- if (i != (8 - 1))
- checksum <<= 4;
- xor_mutator = xor_key;
- }
- return checksum;
- }
- std::string generate_serial(uint32_t checksum)
- {
- std::vector<uint8_t> keys;
- for (uint32_t i = 0, mutator = 0; i < sizeof(uint32_t); i++)
- {
- uint8_t byte = reinterpret_cast<uint8_t*>(&checksum)[sizeof(uint32_t) - (1 + i)];
- for (uint32_t j = 0; j < 2; j++)
- {
- std::function<uint8_t(uint8_t)> lodigit = [](uint8_t byte) -> uint8_t { return (byte & 0xF); };
- std::function<uint8_t(uint8_t)> hidigit = [](uint8_t byte) -> uint8_t { return ((byte >> 4) & 0xF); };
- mutator ^= (j == 0 ? hidigit : lodigit)(byte);
- keys.push_back(mutator);
- }
- }
- std::for_each(keys.begin(), keys.end(), [](uint8_t& ch) -> void
- {
- if (ch >= 0 && ch <= 9)
- ch += '0';
- else if (ch >= 10 && ch <= 15)
- ch += ('A' - 10);
- if (ch >= 'A' && ch <= 'F')
- {
- ch -= 3;
- if (ch < 'A')
- ch += ('F' - 'A');
- }
- });
- return std::string(keys.begin(), keys.end());
- }
- bool evaluate(std::string const& name, std::string const& serial)
- {
- uint32_t x = generate_name_checksum(name);
- std::cout << std::hex << std::uppercase << "Checksum (from name):\t" << x << std::endl;
- uint32_t y = generate_serial_checksum(serial);
- std::cout << std::hex << std::uppercase << "Checksum (from serial):\t" << y << std::endl << std::endl;
- return (x == y);
- }
- void test()
- {
- std::string serial_name;
- std::cout << "Enter serial name:\t";
- std::getline(std::cin, serial_name);
- uint32_t checksum = generate_name_checksum(serial_name);
- std::string serial = generate_serial(checksum);
- std::cout << "Matching serial:\t" << serial << std::endl << std::endl;
- std::cout << "Evaluation result: " << evaluate(serial_name, serial) << std::endl;
- }
- #include <Windows.h>
- int main()
- {
- RECT desktop;
- SystemParametersInfo(SPI_GETWORKAREA, 0, &desktop, 0);
- MoveWindow(GetConsoleWindow(), 0, 0, 1000, (desktop.bottom - desktop.top), TRUE);
- test();
- std::cin.ignore();
- std::cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement