Advertisement
Guest User

Untitled

a guest
Aug 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.85 KB | None | 0 0
  1. public static class SHA    //256
  2.         {
  3.             static void DBL_INT_ADD(ref uint a, ref uint b, uint c)
  4.             {
  5.                 if (a > 0xffffffff - c) ++b; a += c;
  6.             }
  7.  
  8.             static uint ROTLEFT(uint a, byte b)
  9.             {
  10.                 return ((a << b) | (a >> (32 - b)));
  11.             }
  12.  
  13.             static uint ROTRIGHT(uint a, byte b)
  14.             {
  15.                 return (((a) >> (b)) | ((a) << (32 - (b))));
  16.             }
  17.  
  18.             static uint CH(uint x, uint y, uint z)
  19.             {
  20.                 return (((x) & (y)) ^ (~(x) & (z)));
  21.             }
  22.  
  23.             static uint MAJ(uint x, uint y, uint z)
  24.             {
  25.                 return (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)));
  26.             }
  27.  
  28.             static uint EP0(uint x)
  29.             {
  30.                 return (ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22));
  31.             }
  32.  
  33.             static uint EP1(uint x)
  34.             {
  35.                 return (ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25));
  36.             }
  37.  
  38.             static uint SIG0(uint x)
  39.             {
  40.                 return (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3));
  41.             }
  42.  
  43.             static uint SIG1(uint x)
  44.             {
  45.                 return (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10));
  46.             }
  47.  
  48.             struct SHA256_CTX
  49.             {
  50.                 public byte[] data;
  51.                 public uint datalen;
  52.                 public uint[] bitlen;
  53.                 public uint[] state;
  54.             }
  55.  
  56.             static uint[] k = {
  57.     0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
  58.     0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
  59.     0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
  60.     0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
  61.     0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
  62.     0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
  63.     0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
  64.     0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
  65. };
  66.  
  67.             static void SHA256Transform(ref SHA256_CTX ctx, byte[] data)
  68.             {
  69.                 uint a, b, c, d, e, f, g, h, i, j, t1, t2;
  70.                 uint[] m = new uint[64];
  71.  
  72.                 for (i = 0, j = 0; i < 16; ++i, j += 4)
  73.                     m[i] = (uint)((data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]));
  74.  
  75.                 for (; i < 64; ++i)
  76.                     m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
  77.  
  78.                 a = ctx.state[0];
  79.                 b = ctx.state[1];
  80.                 c = ctx.state[2];
  81.                 d = ctx.state[3];
  82.                 e = ctx.state[4];
  83.                 f = ctx.state[5];
  84.                 g = ctx.state[6];
  85.                 h = ctx.state[7];
  86.  
  87.                 for (i = 0; i < 64; ++i)
  88.                 {
  89.                     t1 = h + EP1(e) + CH(e, f, g) + k[i] + m[i];
  90.                     t2 = EP0(a) + MAJ(a, b, c);
  91.                     h = g;
  92.                     g = f;
  93.                     f = e;
  94.                     e = d + t1;
  95.                     d = c;
  96.                     c = b;
  97.                     b = a;
  98.                     a = t1 + t2;
  99.                 }
  100.  
  101.                 ctx.state[0] += a;
  102.                 ctx.state[1] += b;
  103.                 ctx.state[2] += c;
  104.                 ctx.state[3] += d;
  105.                 ctx.state[4] += e;
  106.                 ctx.state[5] += f;
  107.                 ctx.state[6] += g;
  108.                 ctx.state[7] += h;
  109.             }
  110.  
  111.             static void SHA256Init(ref SHA256_CTX ctx)
  112.             {
  113.                 ctx.datalen = 0;
  114.                 ctx.bitlen[0] = 0;
  115.                 ctx.bitlen[1] = 0;
  116.                 ctx.state[0] = 0x6a09e667;
  117.                 ctx.state[1] = 0xbb67ae85;
  118.                 ctx.state[2] = 0x3c6ef372;
  119.                 ctx.state[3] = 0xa54ff53a;
  120.                 ctx.state[4] = 0x510e527f;
  121.                 ctx.state[5] = 0x9b05688c;
  122.                 ctx.state[6] = 0x1f83d9ab;
  123.                 ctx.state[7] = 0x5be0cd19;
  124.             }
  125.  
  126.             static void SHA256Update(ref SHA256_CTX ctx, byte[] data, uint len)
  127.             {
  128.                 for (uint i = 0; i < len; ++i)
  129.                 {
  130.                     ctx.data[ctx.datalen] = data[i];
  131.                     ctx.datalen++;
  132.  
  133.                     if (ctx.datalen == 64)
  134.                     {
  135.                         SHA256Transform(ref ctx, ctx.data);
  136.                         DBL_INT_ADD(ref ctx.bitlen[0], ref ctx.bitlen[1], 512);
  137.                         ctx.datalen = 0;
  138.                     }
  139.                 }
  140.             }
  141.  
  142.             static void SHA256Final(ref SHA256_CTX ctx, byte[] hash)
  143.             {
  144.                 uint i = ctx.datalen;
  145.  
  146.                 if (ctx.datalen < 56)
  147.                 {
  148.                     ctx.data[i++] = 0x80;
  149.  
  150.                     while (i < 56)
  151.                         ctx.data[i++] = 0x00;
  152.                 }
  153.                 else
  154.                 {
  155.                     ctx.data[i++] = 0x80;
  156.  
  157.                     while (i < 64)
  158.                         ctx.data[i++] = 0x00;
  159.  
  160.                     SHA256Transform(ref ctx, ctx.data);
  161.                 }
  162.  
  163.                 DBL_INT_ADD(ref ctx.bitlen[0], ref ctx.bitlen[1], ctx.datalen * 8);
  164.                 ctx.data[63] = (byte)(ctx.bitlen[0]);
  165.                 ctx.data[62] = (byte)(ctx.bitlen[0] >> 8);
  166.                 ctx.data[61] = (byte)(ctx.bitlen[0] >> 16);
  167.                 ctx.data[60] = (byte)(ctx.bitlen[0] >> 24);
  168.                 ctx.data[59] = (byte)(ctx.bitlen[1]);
  169.                 ctx.data[58] = (byte)(ctx.bitlen[1] >> 8);
  170.                 ctx.data[57] = (byte)(ctx.bitlen[1] >> 16);
  171.                 ctx.data[56] = (byte)(ctx.bitlen[1] >> 24);
  172.                 SHA256Transform(ref ctx, ctx.data);
  173.  
  174.                 for (i = 0; i < 4; ++i)
  175.                 {
  176.                     hash[i] = (byte)(((ctx.state[0]) >> (int)(24 - i * 8)) & 0x000000ff);
  177.                     hash[i + 4] = (byte)(((ctx.state[1]) >> (int)(24 - i * 8)) & 0x000000ff);
  178.                     hash[i + 8] = (byte)(((ctx.state[2]) >> (int)(24 - i * 8)) & 0x000000ff);
  179.                     hash[i + 12] = (byte)((ctx.state[3] >> (int)(24 - i * 8)) & 0x000000ff);
  180.                     hash[i + 16] = (byte)((ctx.state[4] >> (int)(24 - i * 8)) & 0x000000ff);
  181.                     hash[i + 20] = (byte)((ctx.state[5] >> (int)(24 - i * 8)) & 0x000000ff);
  182.                     hash[i + 24] = (byte)((ctx.state[6] >> (int)(24 - i * 8)) & 0x000000ff);
  183.                     hash[i + 28] = (byte)((ctx.state[7] >> (int)(24 - i * 8)) & 0x000000ff);
  184.                 }
  185.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement