Created
June 25, 2013 19:29
-
-
Save t2d/5861580 to your computer and use it in GitHub Desktop.
Implementation of a Hash Collision with SHA512/t
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.NoSuchProviderException; | |
import java.security.Security; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.HashMap; | |
import java.math.BigInteger; | |
import org.bouncycastle.crypto.Digest; | |
import org.bouncycastle.crypto.digests.SHA512tDigest; | |
import org.bouncycastle.jce.provider.BouncyCastleProvider; | |
public class Collision { | |
/** | |
* @param args digestSize in Bit | |
*/ | |
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException { | |
// init digest | |
int digestSize = Integer.parseInt(args[0]); | |
Security.addProvider(new BouncyCastleProvider()); | |
Digest messageDigest = new SHA512tDigest(digestSize); | |
// init variables | |
HashMap<BigInteger, String> HM = new HashMap<BigInteger, String>(); | |
String FirstPart = "529252-"; | |
int count = 0; | |
String ReturnString = null; | |
byte[] retValue = new byte[messageDigest.getDigestSize()]; | |
BigInteger Hash = null; | |
// add <hash, string> to hashmap until collision | |
while (ReturnString == null) { | |
String TestString = FirstPart + count; | |
messageDigest.update(TestString.getBytes(), 0, TestString.length()); | |
messageDigest.doFinal(retValue, 0); | |
Hash = new BigInteger(retValue); | |
// when collision ReturnString = old value | |
ReturnString = HM.put(Hash, TestString); | |
count++; | |
} | |
System.out.println("----------------"); | |
System.out.println("Collision found!"); | |
System.out.println("----------------"); | |
System.out.println("Hash: " + byteArrayToHexString(retValue)); | |
System.out.println("String1: " + ReturnString); | |
System.out.println("String2: " + HM.get(Hash)); | |
} | |
//Converting a bytes array to string of hex character | |
public static String byteArrayToHexString(byte[] b) { | |
int len = b.length; | |
String data = new String(); | |
for (int i = 0; i < len; i++){ | |
data += Integer.toHexString((b[i] >> 4) & 0xf); | |
data += Integer.toHexString(b[i] & 0xf); | |
} | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment