Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Created June 4, 2013 03:44
Show Gist options
  • Save vaskoz/5703423 to your computer and use it in GitHub Desktop.
Save vaskoz/5703423 to your computer and use it in GitHub Desktop.
Generates Java Strings with the same hashCode.
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