-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy path_hashlib.java
More file actions
272 lines (229 loc) · 7.94 KB
/
_hashlib.java
File metadata and controls
272 lines (229 loc) · 7.94 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* Copyright (c) Jython Developers */
package org.python.modules;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import org.python.core.ClassDictInit;
import org.python.core.Py;
import org.python.core.PyArray;
import org.python.core.PyFrozenSet;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyTuple;
import org.python.core.PyType;
import org.python.core.PyUnicode;
import org.python.core.Untraversable;
import org.python.core.util.StringUtil;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedType;
/**
* The Python _hashlib module: provides hashing algorithms via
* java.security.MessageDigest.
*
* The 'openssl' method prefix is to match CPython and provide what the pure python
* hashlib.py module expects.
*/
public class _hashlib implements ClassDictInit {
/** A mapping of Python algorithm names to MessageDigest names. */
private static final Map<String, String> algorithmMap = new HashMap<String, String>() {{
put("sha1", "sha-1");
put("sha224", "sha-224");
put("sha256", "sha-256");
put("sha384", "sha-384");
put("sha512", "sha-512");
}};
public static final PyFrozenSet openssl_md_meth_names =
new PyFrozenSet(new PyTuple(Py.newString("md5"), Py.newString("sha1"),
Py.newString("sha224"), Py.newString("sha256"), Py.newString("sha384"),
Py.newString("sha512")));
public static void classDictInit(PyObject dict) {
dict.__setitem__("__name__", Py.newString("_hashlib"));
dict.__setitem__("algorithmMap", null);
dict.__setitem__("classDictInit", null);
}
public static PyObject new$(String name) {
return new$(name, null);
}
public static PyObject new$(String name, PyObject obj) {
name = name.toLowerCase();
// NOTE: we're not disallowing other MessageDigest algorithms
if (algorithmMap.containsKey(name)) {
name = algorithmMap.get(name);
}
Hash hash = new Hash(name);
if (obj != null) {
hash.update(obj);
}
return hash;
}
public static PyObject openssl_md5() {
return openssl_md5(null);
}
public static PyObject openssl_md5(PyObject obj) {
return new$("md5", obj);
}
public static PyObject openssl_sha1() {
return openssl_sha1(null);
}
public static PyObject openssl_sha1(PyObject obj) {
return new$("sha1", obj);
}
public static PyObject openssl_sha224() {
return openssl_sha224(null);
}
public static PyObject openssl_sha224(PyObject obj) {
return new$("sha224", obj);
}
public static PyObject openssl_sha256() {
return openssl_sha256(null);
}
public static PyObject openssl_sha256(PyObject obj) {
return new$("sha256", obj);
}
public static PyObject openssl_sha384() {
return openssl_sha384(null);
}
public static PyObject openssl_sha384(PyObject obj) {
return new$("sha384", obj);
}
public static PyObject openssl_sha512() {
return openssl_sha512(null);
}
public static PyObject openssl_sha512(PyObject obj) {
return new$("sha512", obj);
}
/**
* A generic wrapper around a MessageDigest.
*/
@Untraversable
@ExposedType(name = "_hashlib.HASH")
public static class Hash extends PyObject {
public static final PyType TYPE = PyType.fromClass(Hash.class);
/** The hash algorithm name. */
@ExposedGet
public String name;
/** The hashing engine. */
private MessageDigest digest;
/** Supposed block sizes of algorithms for the block_size attribute. */
private static final Map<String, Integer> blockSizes = new HashMap<String, Integer>() {{
put("md5", 64);
put("sha-1", 64);
put("sha-224", 64);
put("sha-256", 64);
put("sha-384", 128);
put("sha-512", 128);
}};
public Hash(String name) {
this(name, getDigest(name));
}
private Hash(String name, MessageDigest digest) {
super(TYPE);
this.name = name;
this.digest = digest;
}
private static MessageDigest getDigest(String name) {
try {
return MessageDigest.getInstance(name);
} catch (NoSuchAlgorithmException nsae) {
throw Py.ValueError("unsupported hash type");
}
}
/**
* Clone the underlying MessageDigest.
*
* @return a copy of MessageDigest
*/
private MessageDigest cloneDigest() {
try {
synchronized (this) {
return (MessageDigest)digest.clone();
}
} catch (CloneNotSupportedException cnse) {
throw Py.RuntimeError(String.format("_hashlib.HASH (%s) internal error", name));
}
}
/**
* Safely calculate the digest without resetting state.
*
* @return a byte[] calculated digest
*/
private byte[] calculateDigest() {
return cloneDigest().digest();
}
public void update(PyObject obj) {
HASH_update(obj);
}
@ExposedMethod
final void HASH_update(PyObject obj) {
String string;
if (obj instanceof PyUnicode) {
string = ((PyUnicode)obj).encode();
} else if (obj instanceof PyString) {
string = obj.toString();
} else if (obj instanceof PyArray) {
string = ((PyArray)obj).tostring();
} else {
throw Py.TypeError("update() argument 1 must be string or read-only buffer, not "
+ obj.getType().fastGetName());
}
byte[] input = StringUtil.toBytes(string);
synchronized (this) {
digest.update(input);
}
}
public PyObject digest() {
return HASH_digest();
}
@ExposedMethod
final PyObject HASH_digest() {
return Py.newString(StringUtil.fromBytes(calculateDigest()));
}
public PyObject hexdigest() {
return HASH_hexdigest();
}
@ExposedMethod
final PyObject HASH_hexdigest() {
byte[] result = calculateDigest();
// Make hex version of the digest
char[] hexDigest = new char[result.length * 2];
for (int i = 0, j = 0; i < result.length; i++) {
int c = ((result[i] >> 4) & 0xf);
c = c > 9 ? c + 'a' - 10 : c + '0';
hexDigest[j++] = (char)c;
c = result[i] & 0xf;
c = c > 9 ? c + 'a' - 10 : c + '0';
hexDigest[j++] = (char)c;
}
return Py.newString(new String(hexDigest));
}
public PyObject copy() {
return HASH_copy();
}
@ExposedMethod
final PyObject HASH_copy() {
return new Hash(name, cloneDigest());
}
@ExposedGet(name = "digestsize")
public synchronized int getDigestSize() {
return digest.getDigestLength();
}
@ExposedGet(name = "digest_size")
public int getDigest_size() {
return getDigestSize();
}
@ExposedGet(name = "block_size")
public PyObject getBlockSize() {
Integer size = blockSizes.get(name);
if (size == null) {
return Py.None;
}
return Py.newInteger(size);
}
@Override
public String toString() {
return String.format("<%s HASH object @ %s>", name, Py.idstr(this));
}
}
}