forked from sektorcap/sha-painless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha1.painless
93 lines (79 loc) · 2.28 KB
/
sha1.painless
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
byte[] digest(byte[] x) {
int[] blks = new int[(((x.length + 8) >> 6) + 1) * 16];
int i;
for(i = 0; i < x.length; i++) {
blks[i >> 2] |= x[i] << (24 - (i % 4) * 8);
}
blks[i >> 2] |= 0x80 << (24 - (i % 4) * 8);
blks[blks.length - 1] = x.length * 8;
int[] w = new int[80];
int a = 1732584193;
int b = -271733879;
int c = -1732584194;
int d = 271733878;
int e = -1009589776;
for(i = 0; i < blks.length; i += 16) {
int olda = a;
int oldb = b;
int oldc = c;
int oldd = d;
int olde = e;
for(int j = 0; j < 80; j++) {
w[j] = (j < 16) ? blks[i + j] :
( rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1) );
int t = rol(a, 5) + e + w[j] +
( (j < 20) ? 1518500249 + ((b & c) | ((~b) & d))
: (j < 40) ? 1859775393 + (b ^ c ^ d)
: (j < 60) ? -1894007588 + ((b & c) | (b & d) | (c & d))
: -899497514 + (b ^ c ^ d) );
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
a = a + olda;
b = b + oldb;
c = c + oldc;
d = d + oldd;
e = e + olde;
}
byte[] hash = new byte[20];
System.arraycopy(intToBytes(a), 0, hash, 0, 4);
System.arraycopy(intToBytes(b), 0, hash, 4, 4);
System.arraycopy(intToBytes(c), 0, hash, 8, 4);
System.arraycopy(intToBytes(d), 0, hash, 12, 4);
System.arraycopy(intToBytes(e), 0, hash, 16, 4);
return hash;
}
int rol(int num, int cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
byte[] intToBytes(int i) {
byte[] b = new byte[4];
for (int c = 0; c < 4; c++) {
b[c] = (byte) ((i >>> (56 - 8 * c)) & 255);
}
return b;
}
String bytesToHex(byte[] bytes) {
char[] hexArray = "0123456789abcdef".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 255;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 15];
}
return String.copyValueOf(hexChars);
}
byte[] charArrayToByteArray(char[] charArray) {
byte[] byteArray = new byte[charArray.length];
for(int i= 0; i < charArray.length; i++) {
byteArray[i] = (byte)(255 & (int)charArray[i]);
}
return byteArray;
}
// Main
byte[] data = charArrayToByteArray(doc['field.value.keyword'].value.toCharArray());
byte[] res = digest(data);
return bytesToHex(res);