Guest User

Untitled

a guest
Apr 3rd, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using System.Collections;
  6. using System.Linq;
  7. using System.Runtime.Remoting.Metadata.W3cXsd2001;
  8. namespace abloop
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Dictionary<byte[], string> collisionTable = new Dictionary<byte[], string>();
  15.             SoapHexBinary hexString = new SoapHexBinary();
  16.             Random pseudoRNG = new Random();
  17.             SHA256 hashSha = new SHA256Cng();
  18.             for (int i = 0; i < 39506988; i++)
  19.             //For a 50 bit hash, this number gives us a ~50% chance for collision.
  20.             {
  21.                 byte[] byteContainer = new byte[8];
  22.                 byte[] fiftyBitHash = new byte[7];
  23.                 pseudoRNG.NextBytes(byteContainer);
  24.                 //Generate prng number to hash
  25.                 byte[] hashedBYTES = hashSha.ComputeHash(byteContainer);
  26.                 //Use SHA256 to hash prng number
  27.                 fiftyBitHash[0] = (byte)(((int)hashedBYTES[25]) << 6);
  28.                 fiftyBitHash[0] = (byte)((int)fiftyBitHash[0] >> 6);
  29.                 fiftyBitHash[1] = hashedBYTES[26];
  30.                 fiftyBitHash[2] = hashedBYTES[27];
  31.                 fiftyBitHash[3] = hashedBYTES[28];
  32.                 fiftyBitHash[4] = hashedBYTES[29];
  33.                 fiftyBitHash[5] = hashedBYTES[30];
  34.                 fiftyBitHash[6] = hashedBYTES[31];
  35.                 //Get the right 50 bits of the hash
  36.                 hexString.Value = fiftyBitHash;
  37.                 collisionTable.Add(byteContainer, hexString.ToString());
  38.                 //Add both to the dictionary
  39.             }
  40.             var k = collisionTable.GroupBy(p => p.Value).Where(p => p.Count() > 1).Select(p => p).First();
  41.             //Find collisions using LINQ magic
  42.             Console.WriteLine(k.Count());
  43.             foreach (var item in k)
  44.             {
  45.                 Console.WriteLine(item.Value);
  46.                 var xud = new SoapHexBinary(item.Key);
  47.                 Console.WriteLine(xud.ToString());
  48.                 //Print the lsb(50) hash and the bytes used for the collision
  49.             }
  50.             Console.ReadLine();
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment