SHA1.java
3.76 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.io.FileUtils
*/
package com.day.jcr.vault.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.io.FileUtils;
public class SHA1 {
public static final SHA1 NULL = new SHA1(0, 0, 0, 0, 0);
private final int w0;
private final int w1;
private final int w2;
private final int w3;
private final int w4;
public SHA1(int w0, int w1, int w2, int w3, int w4) {
this.w0 = w0;
this.w1 = w1;
this.w2 = w2;
this.w3 = w3;
this.w4 = w4;
}
public SHA1(String str) {
if (str.length() != 40) {
throw new IllegalArgumentException("invalid string length " + str.length());
}
this.w0 = (int)Long.parseLong(str.substring(0, 8), 16);
this.w1 = (int)Long.parseLong(str.substring(8, 16), 16);
this.w2 = (int)Long.parseLong(str.substring(16, 24), 16);
this.w3 = (int)Long.parseLong(str.substring(24, 32), 16);
this.w4 = (int)Long.parseLong(str.substring(32, 40), 16);
}
public SHA1(byte[] bytes) {
if (bytes.length != 20) {
throw new IllegalArgumentException("invalid bytes length " + bytes.length);
}
this.w0 = SHA1.getInt(bytes, 0);
this.w1 = SHA1.getInt(bytes, 4);
this.w2 = SHA1.getInt(bytes, 8);
this.w3 = SHA1.getInt(bytes, 12);
this.w4 = SHA1.getInt(bytes, 16);
}
public int[] getInts() {
return new int[]{this.w0, this.w1, this.w2, this.w3, this.w4};
}
public byte[] getBytes() {
byte[] buf = new byte[20];
SHA1.setInt(buf, 0, this.w0);
SHA1.setInt(buf, 4, this.w1);
SHA1.setInt(buf, 8, this.w2);
SHA1.setInt(buf, 12, this.w3);
SHA1.setInt(buf, 16, this.w4);
return buf;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static SHA1 digest(InputStream in) throws IOException {
try {
MessageDigest md;
int read;
try {
md = MessageDigest.getInstance("SHA-1");
}
catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e.toString());
}
byte[] buffer = new byte[8192];
while ((read = in.read(buffer)) > 0) {
md.update(buffer, 0, read);
}
SHA1 sHA1 = new SHA1(md.digest());
return sHA1;
}
finally {
in.close();
}
}
public static SHA1 digest(File file) throws IOException {
return SHA1.digest(FileUtils.openInputStream((File)file));
}
public String toString() {
return String.format("%08x%08x%08x%08x%08x", this.w0, this.w1, this.w2, this.w3, this.w4);
}
public int hashCode() {
return this.w2;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
SHA1 sha1 = (SHA1)o;
return this.w0 == sha1.w0 && this.w1 == sha1.w1 && this.w2 == sha1.w2 && this.w3 == sha1.w3 && this.w4 == sha1.w4;
}
private static int getInt(byte[] b, int offs) {
return ((b[offs] & 255) << 24) + ((b[1 + offs] & 255) << 16) + ((b[2 + offs] & 255) << 8) + (b[3 + offs] & 255);
}
private static void setInt(byte[] b, int offs, int v) {
b[offs] = (byte)(v >>> 24 & 255);
b[offs + 1] = (byte)(v >>> 16 & 255);
b[offs + 2] = (byte)(v >>> 8 & 255);
b[offs + 3] = (byte)(v & 255);
}
}