Created
June 4, 2013 03:44
-
-
Save vaskoz/5703423 to your computer and use it in GitHub Desktop.
Generates Java Strings with the same hashCode.
This file contains 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.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class StringHashCollisionGenerator { | |
public static void main(String[] args) { | |
if (args.length != 1) { | |
printUsage(); | |
System.exit(1); | |
} | |
int desiredRecords = Integer.valueOf(args[0]); | |
if (desiredRecords < 2) { | |
printUsage(); | |
System.exit(1); | |
} | |
List<String> strings = new ArrayList<String>(Arrays.asList("Aa", "BB")); | |
List<String> temp = new ArrayList<String>(); | |
complete: | |
for (int i = 0; i < 5; i++) { | |
int size = strings.size(); | |
temp = new ArrayList<String>(size*size); | |
int count = 0; | |
for (String s : strings) { | |
for (String t : strings) { | |
if (count == desiredRecords) break complete; | |
temp.add(s+t); | |
count++; | |
} | |
} | |
strings = temp; | |
} | |
strings = temp; | |
for (String s : strings) | |
System.out.println(s); | |
} | |
private static void printUsage() { | |
System.err.println("Usage: [number of Strings (2^1-2^32]"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment