All same hash code string in Java

All same hash code string in Java

final String p1 = "AaBB";
final String p2 = "BBAa";
final String p3 = "BBBB";
final String[] pp = new String[] { p1, p2, p3 };
final int ppLength = pp.length;
Random random = new Random();
random.setSeed(System.currentTimeMillis());
final int size = 10;
final int length = size * 4;
final int times = 2_000_000; //n million loops
Map<String, String> map = new HashMap<>(10000);
File file = new File("r:/1.txt");
file.delete();
BufferedWriter bw = new BufferedWriter( //
    new OutputStreamWriter( //
            new FileOutputStream(file, true) // append
            , Charset.forName("UTF-8") // charset
    ) //
    , 1024 // buffer size
);
StringBuffer sb = new StringBuffer(length);
for (int ii = 0; ii < size; ii++) {
    sb.append(pp[random.nextInt(ppLength)]);
}
final int hashCode = sb.toString().hashCode();
sb.setLength(0);
for (int count = 0; count < times; count++) {
    for (int ii = 0; ii < size; ii++) {
        sb.append(pp[random.nextInt(ppLength)]);
    }
    String ss = sb.toString();
    if (!map.containsKey(ss)) {
        if (ss.hashCode() != hashCode) {
            bw.flush();
            bw.close();
            throw new RuntimeException("hashCode not equal");
        }
        map.put(ss, ss);
        bw.append(ss);
        bw.newLine();
    }
    sb.setLength(0);
}
System.out.println(map.size());
bw.flush();
bw.close();

Reference site: https://stackoverflow.com/questions/8669946/application-vulnerability-due-to-non-random-hash-functions

Recommended Posts

All same hash code string in Java
Code to escape a JSON string in Java
Sample code to convert List to List <String> in Java Stream
Write Java8-like code in Java8
Java Spring environment in vs Code
Arbitrary string creation code by Java
[Java] Reflash all items in BeanClass
Java string
Regarding String type equivalence comparison in Java
Split a string with ". (Dot)" in Java
[Mac] Install Java in Visual Studio Code
The story of low-level string comparison in Java
Script Java code
Add --enable-preview option in Java in Visual Studio Code
Static code analysis with Checkstyle in Java + Gradle
[Java] String padding
Java sample code 02
Java sample code 03
Changes in Java 11
Try using Sourcetrail (win version) in Java code
Rock-paper-scissors in Java
Java sample code 04
Try using Sourcetrail (macOS version) in Java code
Java string processing
[AtCoder Problem-ABC001] C-Do wind observation in Java [Code]
Code that deletes all files of the specified prefix in AWS S3 (Java)
Java sample code 01
Java character code
Pi in Java
Split string (Java)
How to write Java String # getBytes in Kotlin?
Import files of the same hierarchy in Java
[Java beginner's anguish] Hard-to-test code implemented in Junit
FizzBuzz in Java
[Mac] Install Java in Visual Studio Code (VS Code)
Delete All from Java SDK in Azure Cosmos DB
Do not write if (isAdmin == true) code in Java
Get Locale objects for all locales available in Java
Java11: Run Java code in a single file as is
Differences in code when using the length system in Java
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
[Java] String comparison and && and ||
Azure functions in java
Java string multiple replacement
Format XML in Java
Simple htmlspecialchars in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
[Note] Java: String survey
Ping commands in Java