-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastHash.java
More file actions
109 lines (85 loc) · 2.11 KB
/
FastHash.java
File metadata and controls
109 lines (85 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package MadTools;
/**
*
* @author Mad Scientist
*/
public class FastHash
{
public String binHash(String s, int l)
{
String h = "";
for(int i = 0; i < s.length(); i++)
h += Integer.toBinaryString(s.charAt(i));
while(h.length()!=l)
{
if(h.length()<l) h += Integer.toBinaryString(h.length());
else h = XOR(h.substring(0, l), h.substring(l+1, h.length()-1));
}
return XOR(h, reverse(h));
}
private String reverse(String s)
{
String h = "";
for(int i = 0; i < s.length(); i++)
h = s.charAt(i) + h;
return h;
}
private String XOR(String a, String b)
{
while(a.length()!=b.length())
if(a.length()>b.length()) b = "0" + b;
else a = "0" + a;
String c = "";
for(int i = 0; i < a.length(); i++)
c += a.charAt(i)=='0' ? b.charAt(i) : (b.charAt(i)=='0' ? "1" : "0");
return c;
}
public String hexHash(String s, int l)
{
return toHex(binHash(s,l));
}
String toHex(String s)
{
String h = "";
while(s.length()%4!=0) s = "0" + s;
char n[] = s.toCharArray();
for(int i = n.length-1; i>=0; i-=4)
h=h.concat(hexDigitLookup("" + n[i-3] + n[i-2] + n[i-1] + n[i]));
return h;
}
private String hexDigitLookup(String d)
{
if(d.length()!=4) return "";
String [][] table = {
{"0000","0"},
{"0001","1"},
{"0010","2"},
{"0011","3"},
{"0100","4"},
{"0101","5"},
{"0110","6"},
{"0111","7"},
{"1000","8"},
{"1001","9"},
{"1010","A"},
{"1011","B"},
{"1100","C"},
{"1101","D"},
{"1110","E"},
{"1111","F"},
};
for(String [] pair : table)
if(pair[0].equals(d))
return pair[1];
return "";
}
public static void main(String args[])
{
FastHash f = new FastHash();
int width = 128;
System.out.println(f.hexHash("Hello World", width));
System.out.println(f.hexHash("Hello Worlds", width));
System.out.println(f.hexHash("Hello_World", width));
System.out.println(f.hexHash("hello world", width));
}
}