Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- using System.Windows.Forms;
- class Hasher
- {
- private MAC mac = new MAC();
- private byte[] H = new byte[32];
- private byte[] B = new byte[128 + 4];
- private int[] X = new int[32];
- private int[] V = new int[32 * 1024];
- private byte[] Z = new byte[84];
- int[] w = new int[16];
- private int[] TZ = new int[32];
- Dictionary<int[], int[]> dkk = new Dictionary<int[], int[]>();
- Dictionary<int[], int[]> jkk = new Dictionary<int[], int[]>();
- public byte[] hash(byte[] header, int nonce)
- {
- int i, j, k, l = 0;
- Array.Copy(header, 0, B, 0, 80);
- B[76] = (byte)(nonce >> 0);
- B[77] = (byte)(nonce >> 8);
- B[78] = (byte)(nonce >> 16);
- B[79] = (byte)(nonce >> 24);
- byte[] key = new byte[80];
- Array.Copy(B, 0, key, 0, 80);
- mac.init(key);
- B[80] = 0;
- B[81] = 0;
- B[82] = 0;
- for (i = 0; i < 4; i++)
- {
- B[83] = (byte)(i + 1);
- Array.Copy(B, 0, Z, 0, 84);
- mac.update(B, 0, 84);
- H = mac.doFinal();
- mac.init(key);
- //MessageBox.Show(HexToString(H));
- for (j = 0; j < 8; j++)
- {
- 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;
- }
- }
- int[] TX = new int[32];
- //else
- for (i = 0; i < 1024; i++)
- {
- Array.Copy(X, 0, V, i * 32, 32);
- //==============xor Salsa===================
- for (l = 0; l < 16; l++)
- w[l] = (X[l] ^= X[16 + l]);
- //Array.Copy(X, 0, TX, 0, 32);
- /*if (hashed.found.ContainsKey(TX))
- {
- hashed.found.TryGetValue(TX, out TZ);
- Array.Copy(TZ, 0, X, 0, 32);
- continue;
- }*/
- xorSalsa8();
- for (l = 0; l < 16; ++l)
- X[l] += w[l];
- for (l = 0; l < 16; l++)
- w[l] = (X[16 + l] ^= X[l]);
- xorSalsa8();
- for (l = 0; l < 16; ++l)
- X[16 + l] += w[l];
- //==========================================
- //Array.Copy(X, 0, TZ, 0, 32);
- //hashed.found.Add(TX, TZ);
- }
- for (i = 0; i < 1024; i++)
- {
- k = (X[16] & 1023) * 32;
- for (j = 0; j < 32; j++)
- {
- X[j] ^= V[k + j];
- }
- //==============xor Salsa===================
- for (l = 0; l < 16; l++)
- w[l] = (X[l] ^= X[16 + l]);
- xorSalsa8();
- for (l = 0; l < 16; ++l)
- X[l] += w[l];
- for (l = 0; l < 16; l++)
- w[l] = (X[16 + l] ^= X[l]);
- xorSalsa8();
- for (l = 0; l < 16; ++l)
- X[16 + l] += w[l];
- //==========================================
- }/**/
- for (i = 0; i < 32; i++)
- {
- B[i * 4 + 0] = (byte)(X[i] >> 0);
- B[i * 4 + 1] = (byte)(X[i] >> 8);
- B[i * 4 + 2] = (byte)(X[i] >> 16);
- B[i * 4 + 3] = (byte)(X[i] >> 24);
- }
- B[128 + 3] = 1;
- mac.update(B, 0, 128 + 4);
- H = mac.doFinal();
- return H;
- }
- public static int rotateLeft(int i, int distance)
- {
- uint val = (uint)i;
- return (int)((val << distance) | (val >> (32 - distance)));
- }
- public int rotr(int i, int distance)
- {
- uint val = (uint)i;
- return (int)((val >> distance) | (val << (32 - distance)));
- }
- public string HexToString(byte[] ba)
- {
- StringBuilder sb = new StringBuilder(ba.Length * 2);
- foreach (byte b in ba)
- {
- sb.AppendFormat("{0:x2}", b);
- }
- return sb.ToString();
- }
- public string intAtoS(int[] arr)
- {
- return (arr == null) ? null : arr.Skip(1).Aggregate(arr[0].ToString(), (s, i) => s + "," + i.ToString());
- }
- //=================================================================================================================
- public void xorSalsa8()
- {
- for (int i = 0; i < 8; i += 2)
- {
- /* Operate on columns. */
- 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);
- 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);
- 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);
- 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);
- /* Operate on rows. */
- 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);
- 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);
- 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);
- 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);
- }
- }
- public static int R(int i, int distance)
- {
- uint val = (uint)i;
- return (int)((val << distance) | (val >> (32 - distance)));
- }
- static unsafe void Copy(byte[] src, int srcIndex,
- byte[] dst, int dstIndex, int count)
- {
- // The following fixed statement pins the location of
- // the src and dst objects in memory so that they will
- // not be moved by garbage collection.
- fixed (byte* pSrc = src, pDst = dst)
- {
- byte* ps = pSrc;
- byte* pd = pDst;
- // Loop over the count in blocks of 4 bytes, copying an
- // integer (4 bytes) at a time:
- for (int n = 0; n < count / 4; n++)
- {
- *((int*)pd) = *((int*)ps);
- pd += 4;
- ps += 4;
- }
- // Complete the copy by moving any bytes that weren't
- // moved in blocks of 4:
- for (int n = 0; n < count % 4; n++)
- {
- *pd = *ps;
- pd++;
- ps++;
- }
- }
- }
- static unsafe void Copy(int[] src, int srcIndex,
- int[] dst, int dstIndex, int count)
- {
- // The following fixed statement pins the location of
- // the src and dst objects in memory so that they will
- // not be moved by garbage collection.
- fixed (int* pSrc = src, pDst = dst)
- {
- int* ps = pSrc;
- int* pd = pDst;
- // Loop over the count in blocks of 4 bytes, copying an
- // integer (4 bytes) at a time:
- for (int n = 0; n < count; n++)
- {
- *((int*)pd) = *((int*)ps);
- pd += 1;
- ps += 1;
- }
- }
- }
- //==================================================================================================================================
- }
Advertisement
Add Comment
Please, Sign In to add comment