Advertisement
Guest User

Untitled

a guest
Aug 29th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <stdint.h>
  2.  
  3. #include <algorithm>
  4. #include <functional>
  5. #include <iomanip>
  6. #include <iostream>
  7. #include <random>
  8. #include <sstream>
  9. #include <string>
  10. #include <vector>
  11.  
  12. uint32_t generate_name_checksum(std::string const& name)
  13. {
  14.     uint32_t key = 0xDEADB00B;
  15.     uint32_t checksum = 0;
  16.  
  17.     for (std::size_t i = 0; i < name.length(); i++)
  18.     {
  19.         checksum += name.at(i) ^ key;
  20.         key = _rotr(key, 8);
  21.     }
  22.  
  23.     return checksum;
  24. }
  25.  
  26. uint32_t generate_serial_checksum(std::string const& serial)
  27. {
  28.     std::vector<uint8_t> digits(serial.begin(), serial.end());
  29.  
  30.     std::for_each(digits.begin(), digits.end(), [](uint8_t& ch) -> void
  31.     {
  32.         if (ch >= 'A' && ch <= 'F')
  33.         {
  34.             ch += 3;
  35.  
  36.             if (ch > 'F')
  37.                 ch += ('A' - 'F');
  38.         }
  39.     });
  40.  
  41.     uint32_t checksum_key = 0;
  42.  
  43.     for (uint32_t i = 0, key = 0; i < digits.size(); i++)
  44.     {
  45.         uint8_t digit = digits.at(i);
  46.  
  47.         checksum_key = key + (digit <= '9' ? (digit - '0') : (digit - '7'));
  48.         key = (checksum_key << 4);
  49.     }
  50.  
  51.     uint32_t xor_mutator = (_rotr(checksum_key & 0xF0000000, 24) >> 4);
  52.     uint32_t checksum = (xor_mutator << 4);
  53.  
  54.     for (std::size_t i = 1, j = (checksum_key << 4); i < 8; i++, j <<= 4)
  55.     {
  56.         uint32_t xor_key = _rotr(j & 0xF0000000, 24) >> 4;
  57.  
  58.         checksum += (xor_mutator ^ xor_key);
  59.  
  60.         if (i != (8 - 1))
  61.             checksum <<= 4;
  62.  
  63.         xor_mutator = xor_key;
  64.     }
  65.  
  66.     return checksum;
  67. }
  68.  
  69. std::string generate_serial(uint32_t checksum)
  70. {
  71.     std::vector<uint8_t> keys;
  72.  
  73.     for (uint32_t i = 0, mutator = 0; i < sizeof(uint32_t); i++)
  74.     {
  75.         uint8_t byte = reinterpret_cast<uint8_t*>(&checksum)[sizeof(uint32_t) - (1 + i)];
  76.  
  77.         for (uint32_t j = 0; j < 2; j++)
  78.         {
  79.             std::function<uint8_t(uint8_t)> lodigit = [](uint8_t byte) -> uint8_t { return (byte & 0xF); };
  80.             std::function<uint8_t(uint8_t)> hidigit = [](uint8_t byte) -> uint8_t { return ((byte >> 4) & 0xF); };
  81.  
  82.             mutator ^= (j == 0 ? hidigit : lodigit)(byte);
  83.  
  84.             keys.push_back(mutator);
  85.         }
  86.     }
  87.  
  88.     std::for_each(keys.begin(), keys.end(), [](uint8_t& ch) -> void
  89.     {
  90.         if (ch >= 0 && ch <= 9)
  91.             ch += '0';
  92.         else if (ch >= 10 && ch <= 15)
  93.             ch += ('A' - 10);
  94.  
  95.         if (ch >= 'A' && ch <= 'F')
  96.         {
  97.             ch -= 3;
  98.  
  99.             if (ch < 'A')
  100.                 ch += ('F' - 'A');
  101.         }
  102.     });
  103.  
  104.     return std::string(keys.begin(), keys.end());
  105. }
  106.  
  107. bool evaluate(std::string const& name, std::string const& serial)
  108. {
  109.     uint32_t x = generate_name_checksum(name);
  110.     std::cout << std::hex << std::uppercase << "Checksum (from name):\t" << x << std::endl;
  111.  
  112.     uint32_t y = generate_serial_checksum(serial);
  113.     std::cout << std::hex << std::uppercase << "Checksum (from serial):\t" << y << std::endl << std::endl;
  114.  
  115.     return (x == y);
  116. }
  117.  
  118. void test()
  119. {
  120.     std::string serial_name;
  121.  
  122.     std::cout << "Enter serial name:\t";
  123.     std::getline(std::cin, serial_name);
  124.  
  125.     uint32_t checksum = generate_name_checksum(serial_name);
  126.     std::string serial = generate_serial(checksum);
  127.  
  128.     std::cout << "Matching serial:\t" << serial << std::endl << std::endl;
  129.     std::cout << "Evaluation result: " << evaluate(serial_name, serial) << std::endl;
  130. }
  131.  
  132. #include <Windows.h>
  133.  
  134. int main()
  135. {
  136.     RECT desktop;
  137.     SystemParametersInfo(SPI_GETWORKAREA, 0, &desktop, 0);
  138.     MoveWindow(GetConsoleWindow(), 0, 0, 1000, (desktop.bottom - desktop.top), TRUE);
  139.    
  140.     test();
  141.  
  142.     std::cin.ignore();
  143.     std::cin.get();
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement