Guest User

Scrypt Hasher

a guest
Sep 15th, 2013
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.55 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Security.Cryptography;
  7. using System.Windows.Forms;
  8.  
  9. class Hasher
  10. {
  11.     private MAC mac = new MAC();
  12.     private byte[] H = new byte[32];
  13.     private byte[] B = new byte[128 + 4];
  14.     private int[] X = new int[32];
  15.     private int[] V = new int[32 * 1024];
  16.     private byte[] Z = new byte[84];
  17.     int[] w = new int[16];
  18.  
  19.     private int[] TZ = new int[32];
  20.     Dictionary<int[], int[]> dkk = new Dictionary<int[], int[]>();
  21.     Dictionary<int[], int[]> jkk = new Dictionary<int[], int[]>();
  22.     public byte[] hash(byte[] header, int nonce)
  23.     {
  24.         int i, j, k, l = 0;
  25.         Array.Copy(header, 0, B, 0, 80);
  26.         B[76] = (byte)(nonce >> 0);
  27.         B[77] = (byte)(nonce >> 8);
  28.         B[78] = (byte)(nonce >> 16);
  29.         B[79] = (byte)(nonce >> 24);
  30.         byte[] key = new byte[80];
  31.         Array.Copy(B, 0, key, 0, 80);
  32.         mac.init(key);
  33.         B[80] = 0;
  34.         B[81] = 0;
  35.         B[82] = 0;
  36.  
  37.         for (i = 0; i < 4; i++)
  38.         {
  39.             B[83] = (byte)(i + 1);
  40.             Array.Copy(B, 0, Z, 0, 84);
  41.             mac.update(B, 0, 84);
  42.             H = mac.doFinal();
  43.             mac.init(key);
  44.             //MessageBox.Show(HexToString(H));
  45.  
  46.             for (j = 0; j < 8; j++)
  47.             {
  48.                 X[i * 8 + j] = (H[j * 4 + 0] & 0xff) << 0 | (H[j * 4 + 1] & 0xff) << 8 | (H[j * 4 + 2] & 0xff) << 16 | (H[j * 4 + 3] & 0xff) << 24;
  49.             }
  50.         }
  51.         int[] TX = new int[32];
  52.  
  53.        
  54.         //else
  55.             for (i = 0; i < 1024; i++)
  56.             {
  57.                 Array.Copy(X, 0, V, i * 32, 32);
  58.  
  59.                 //==============xor Salsa===================
  60.                 for (l = 0; l < 16; l++)
  61.                     w[l] = (X[l] ^= X[16 + l]);
  62.                 //Array.Copy(X, 0, TX, 0, 32);
  63.                 /*if (hashed.found.ContainsKey(TX))
  64.                 {
  65.                     hashed.found.TryGetValue(TX, out TZ);
  66.                     Array.Copy(TZ, 0, X, 0, 32);
  67.                     continue;
  68.                 }*/
  69.                 xorSalsa8();
  70.                 for (l = 0; l < 16; ++l)
  71.                     X[l] += w[l];
  72.  
  73.                 for (l = 0; l < 16; l++)
  74.                     w[l] = (X[16 + l] ^= X[l]);
  75.                 xorSalsa8();
  76.                 for (l = 0; l < 16; ++l)
  77.                     X[16 + l] += w[l];
  78.                 //==========================================
  79.                 //Array.Copy(X, 0, TZ, 0, 32);
  80.                 //hashed.found.Add(TX, TZ);
  81.             }
  82.         for (i = 0; i < 1024; i++)
  83.         {
  84.             k = (X[16] & 1023) * 32;
  85.             for (j = 0; j < 32; j++)
  86.             {
  87.                 X[j] ^= V[k + j];
  88.             }
  89.             //==============xor Salsa===================
  90.             for (l = 0; l < 16; l++)
  91.                 w[l] = (X[l] ^= X[16 + l]);
  92.             xorSalsa8();
  93.             for (l = 0; l < 16; ++l)
  94.                 X[l] += w[l];
  95.  
  96.             for (l = 0; l < 16; l++)
  97.                 w[l] = (X[16 + l] ^= X[l]);
  98.             xorSalsa8();
  99.             for (l = 0; l < 16; ++l)
  100.                 X[16 + l] += w[l];
  101.             //==========================================
  102.         }/**/
  103.  
  104.         for (i = 0; i < 32; i++)
  105.         {
  106.             B[i * 4 + 0] = (byte)(X[i] >> 0);
  107.             B[i * 4 + 1] = (byte)(X[i] >> 8);
  108.             B[i * 4 + 2] = (byte)(X[i] >> 16);
  109.             B[i * 4 + 3] = (byte)(X[i] >> 24);
  110.         }
  111.         B[128 + 3] = 1;
  112.         mac.update(B, 0, 128 + 4);
  113.         H = mac.doFinal();
  114.         return H;
  115.     }
  116.  
  117.     public static int rotateLeft(int i, int distance)
  118.     {
  119.         uint val = (uint)i;
  120.         return (int)((val << distance) | (val >> (32 - distance)));
  121.     }
  122.     public int rotr(int i, int distance)
  123.     {
  124.         uint val = (uint)i;
  125.         return (int)((val >> distance) | (val << (32 - distance)));
  126.     }
  127.  
  128.     public string HexToString(byte[] ba)
  129.     {
  130.         StringBuilder sb = new StringBuilder(ba.Length * 2);
  131.         foreach (byte b in ba)
  132.         {
  133.             sb.AppendFormat("{0:x2}", b);
  134.         }
  135.         return sb.ToString();
  136.     }
  137.     public string intAtoS(int[] arr)
  138.     {
  139.         return (arr == null) ? null : arr.Skip(1).Aggregate(arr[0].ToString(), (s, i) => s + "," + i.ToString());
  140.     }
  141.     //=================================================================================================================
  142.     public void xorSalsa8()
  143.     {
  144.  
  145.         for (int i = 0; i < 8; i += 2)
  146.         {
  147.             /* Operate on columns. */
  148.             w[4] ^= R(w[0] + w[12], 7); w[9] ^= R(w[5] + w[1], 7); w[14] ^= R(w[10] + w[6], 7); w[3] ^= R(w[15] + w[11], 7);
  149.             w[8] ^= R(w[4] + w[0], 9); w[13] ^= R(w[9] + w[5], 9); w[2] ^= R(w[14] + w[10], 9); w[7] ^= R(w[3] + w[15], 9);
  150.             w[12] ^= R(w[8] + w[4], 13); w[1] ^= R(w[13] + w[9], 13); w[6] ^= R(w[2] + w[14], 13); w[11] ^= R(w[7] + w[3], 13);
  151.             w[0] ^= R(w[12] + w[8], 18); w[5] ^= R(w[1] + w[13], 18); w[10] ^= R(w[6] + w[2], 18); w[15] ^= R(w[11] + w[7], 18);
  152.  
  153.             /* Operate on rows. */
  154.             w[1] ^= R(w[0] + w[3], 7); w[6] ^= R(w[5] + w[4], 7); w[11] ^= R(w[10] + w[9], 7); w[12] ^= R(w[15] + w[14], 7);
  155.             w[2] ^= R(w[1] + w[0], 9); w[7] ^= R(w[6] + w[5], 9); w[8] ^= R(w[11] + w[10], 9); w[13] ^= R(w[12] + w[15], 9);
  156.             w[3] ^= R(w[2] + w[1], 13); w[4] ^= R(w[7] + w[6], 13); w[9] ^= R(w[8] + w[11], 13); w[14] ^= R(w[13] + w[12], 13);
  157.             w[0] ^= R(w[3] + w[2], 18); w[5] ^= R(w[4] + w[7], 18); w[10] ^= R(w[9] + w[8], 18); w[15] ^= R(w[14] + w[13], 18);
  158.         }
  159.  
  160.     }
  161.     public static int R(int i, int distance)
  162.     {
  163.         uint val = (uint)i;
  164.         return (int)((val << distance) | (val >> (32 - distance)));
  165.     }
  166.  
  167.     static unsafe void Copy(byte[] src, int srcIndex,
  168.         byte[] dst, int dstIndex, int count)
  169.     {
  170.  
  171.         // The following fixed statement pins the location of
  172.         // the src and dst objects in memory so that they will
  173.         // not be moved by garbage collection.          
  174.         fixed (byte* pSrc = src, pDst = dst)
  175.         {
  176.             byte* ps = pSrc;
  177.             byte* pd = pDst;
  178.  
  179.             // Loop over the count in blocks of 4 bytes, copying an
  180.             // integer (4 bytes) at a time:
  181.             for (int n = 0; n < count / 4; n++)
  182.             {
  183.                 *((int*)pd) = *((int*)ps);
  184.                 pd += 4;
  185.                 ps += 4;
  186.             }
  187.  
  188.             // Complete the copy by moving any bytes that weren't
  189.             // moved in blocks of 4:
  190.             for (int n = 0; n < count % 4; n++)
  191.             {
  192.                 *pd = *ps;
  193.                 pd++;
  194.                 ps++;
  195.             }
  196.         }
  197.     }
  198.  
  199.     static unsafe void Copy(int[] src, int srcIndex,
  200.         int[] dst, int dstIndex, int count)
  201.     {
  202.  
  203.         // The following fixed statement pins the location of
  204.         // the src and dst objects in memory so that they will
  205.         // not be moved by garbage collection.          
  206.         fixed (int* pSrc = src, pDst = dst)
  207.         {
  208.             int* ps = pSrc;
  209.             int* pd = pDst;
  210.  
  211.             // Loop over the count in blocks of 4 bytes, copying an
  212.             // integer (4 bytes) at a time:
  213.             for (int n = 0; n < count; n++)
  214.             {
  215.                 *((int*)pd) = *((int*)ps);
  216.                 pd += 1;
  217.                 ps += 1;
  218.             }
  219.         }
  220.     }
  221.     //==================================================================================================================================
  222. }
Advertisement
Add Comment
Please, Sign In to add comment