MD5.java
3.5 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.jcr.vault.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
private final long msb;
private final long lsb;
public MD5(long msb, long lsb) {
this.msb = msb;
this.lsb = lsb;
}
public MD5(String str) {
if (str.length() != 32) {
throw new IllegalArgumentException("invalid string length " + str.length());
}
this.msb = (Long.parseLong(str.substring(0, 8), 16) << 32) + Long.parseLong(str.substring(8, 16), 16);
this.lsb = (Long.parseLong(str.substring(16, 24), 16) << 32) + Long.parseLong(str.substring(24, 32), 16);
}
public MD5(byte[] bytes) {
if (bytes.length != 16) {
throw new IllegalArgumentException("invalid bytes length " + bytes.length);
}
this.msb = MD5.getLong(bytes, 0);
this.lsb = MD5.getLong(bytes, 8);
}
public long[] getLongs() {
return new long[]{this.msb, this.lsb};
}
public long getMsb() {
return this.msb;
}
public long getLsb() {
return this.lsb;
}
public byte[] getBytes() {
byte[] buf = new byte[16];
MD5.setLong(buf, 0, this.msb);
MD5.setLong(buf, 8, this.lsb);
return buf;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static MD5 digest(InputStream in) throws IOException {
try {
MessageDigest md;
int read;
try {
md = MessageDigest.getInstance("md5");
}
catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e.toString());
}
byte[] buffer = new byte[8192];
while ((read = in.read(buffer)) > 0) {
md.update(buffer, 0, read);
}
MD5 mD5 = new MD5(md.digest());
return mD5;
}
finally {
in.close();
}
}
public static MD5 digest(File file) throws IOException {
return MD5.digest(new FileInputStream(file));
}
public String toString() {
return String.format("%016x%016x", this.msb, this.lsb);
}
public int hashCode() {
return (int)(this.msb >> 32 ^ this.msb ^ this.lsb >> 32 ^ this.lsb);
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
MD5 md5 = (MD5)o;
return this.lsb == md5.lsb && this.msb == md5.msb;
}
private static long getLong(byte[] b, int offs) {
return ((long)(b[offs] & 255) << 56) + ((long)(b[1 + offs] & 255) << 48) + ((long)(b[2 + offs] & 255) << 40) + ((long)(b[3 + offs] & 255) << 32) + ((long)(b[4 + offs] & 255) << 24) + ((long)(b[5 + offs] & 255) << 16) + ((long)(b[6 + offs] & 255) << 8) + (long)(b[7 + offs] & 255);
}
private static void setLong(byte[] b, int offs, long v) {
b[offs] = (byte)(v >>> 56 & 255);
b[offs + 1] = (byte)(v >>> 48 & 255);
b[offs + 2] = (byte)(v >>> 40 & 255);
b[offs + 3] = (byte)(v >>> 32 & 255);
b[offs + 4] = (byte)(v >>> 24 & 255);
b[offs + 5] = (byte)(v >>> 16 & 255);
b[offs + 6] = (byte)(v >>> 8 & 255);
b[offs + 7] = (byte)(v >>> 0 & 255);
}
}