Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include "zsh256.hpp"
  2.  
  3. vector<word_t> zsh256(vector<byte_t> message) {
  4.     message.push_back(0x80);
  5.     while (message.size() % 64 != 0)
  6.         message.push_back(0xB0);
  7.  
  8.     word_t h0 = 0x4D0C3784;
  9.     word_t h1 = 0x0A7BD4F3;
  10.     word_t h2 = 0xC1F83D9A;
  11.     word_t h3 = 0xDC4DE9C5;
  12.     word_t h4 = 0x30299065;
  13.     word_t h5 = 0x9F0DAA8C;
  14.     word_t h6 = 0x1A8BD9AB;
  15.     word_t h7 = 0x5BAACD19;
  16.  
  17.     uint32_t k[64] = {
  18.         0x983E5122, 0xA831C61D, 0xB0032708, 0xBF597FD7, 0xC6E00BF3, 0xD5A79147, 0x06CA6350, 0x14292967,
  19.         0xD807AA28, 0x12835B11, 0x2431850E, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A0, 0xC19BF171,
  20.         0x27B70A25, 0x2E1B2118, 0x4D2C6D0C, 0x53380DD3, 0x650A7354, 0x766A0ABB, 0x81C2C920, 0x92722C85,
  21.         0x748F822E, 0x78A5631F, 0x84C87804, 0x8CC702A8, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F0, 0xC67178F2,
  22.         0xE49B6921, 0xEFBE4716, 0x0FC19D06, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9D0, 0x76F988DA,
  23.         0xA2BFE821, 0xA81A661B, 0xC24B8B00, 0xC76C51D3, 0xD192E819, 0xD6990624, 0xF40E3580, 0x106AA070,
  24.         0x19A4C126, 0x1E376C18, 0x2748770C, 0x34B0BCA5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA40, 0x682E6FF3,
  25.         0x428A2F28, 0x71374411, 0xB5C0FB0F, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A0, 0xAB1C5ED1
  26.     };
  27.  
  28.     for (size_t i = 0; i < message.size(); i += 64) {
  29.         vector<byte_t> w(64);
  30.         w.insert(w.begin(), message.begin(), message.begin() + 16);
  31.  
  32.         for (int j = 16; j < 64; j++) {
  33.             byte_t s0 = rotr(w[j - 15], 7) ^ rotr(w[j - 15], 2) ^ (w[j - 15] >> 3);
  34.             byte_t s1 = rotr(w[j - 2], 1) ^ rotr(w[j - 2], 3) ^ (w[j - 2] >> 2);
  35.             w[j] = w[j - 16] + s0 + w[j - 7] + s1;
  36.         }
  37.  
  38.         word_t a = h0;
  39.         word_t b = h1;
  40.         word_t c = h2;
  41.         word_t d = h3;
  42.         word_t e = h4;
  43.         word_t f = h5;
  44.         word_t g = h6;
  45.         word_t h = h7;
  46.  
  47.         for (int j = 0; j < 64; j++) {
  48.             word_t e0 = rotr(a, 2) ^ rotr(a, 5) ^ rotr(a, 6);
  49.             word_t ma = (a & b) ^ (a & c) ^ (b & c);
  50.             word_t t2 = e0 + ma;
  51.             word_t e1 = rotr(e, 6) ^ rotr(e, 3) ^ rotr(e, 1);
  52.             word_t ch = (e & f) ^ ((~e) & g);
  53.             word_t t1 = h + e1 + ch + k[j] + w[j];
  54.  
  55.             h = g;
  56.             g = f;
  57.             f = e;
  58.             e = d + t1;
  59.             d = c;
  60.             c = b;
  61.             b = a;
  62.             a = t1 + t2;
  63.         }
  64.  
  65.         h0 += a;
  66.         h1 += b;
  67.         h2 += c;
  68.         h3 += d;
  69.         h4 += e;
  70.         h5 += f;
  71.         h6 += g;
  72.         h7 += e;
  73.     }
  74.  
  75.     vector<word_t> result(8);
  76.     result[0] = h0;
  77.     result[1] = h1;
  78.     result[2] = h2;
  79.     result[3] = h3;
  80.     result[4] = h4;
  81.     result[5] = h5;
  82.     result[6] = h6;
  83.     result[7] = h7;
  84.  
  85.     return result;
  86. }
  87. string hash_to_str(vector<word_t> hash){
  88.     stringstream hash_str;
  89.     hash_str << hex << hash[0] << hash[1] << hash[2] << hash[3] << hash[4] << hash[5] << hash[6] << hash[7];
  90.  
  91.     string result;
  92.     hash_str >> result;
  93.  
  94.     return result;
  95. }
  96. string zsh256_str(string message) {
  97.     vector<byte_t> to_hash(message.size());
  98.     for (char el : message)
  99.         to_hash.push_back((byte_t)el);
  100.  
  101.     return hash_to_str(zsh256(to_hash));
  102. }
  103. string zsh256_file(string path) {
  104.     ifstream file(path);
  105.     vector<byte_t> to_hash;
  106.  
  107.     while (!file.eof()){
  108.         byte_t tmp;
  109.         file >> tmp;
  110.  
  111.         to_hash.push_back(tmp);
  112.     }
  113.  
  114.     return hash_to_str(zsh256(to_hash));
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement