01: // Copyright (c) Corporation for National Research Initiatives
02: package org.python.modules;
03:
04: import org.python.core.*;
05:
06: // Implementation of the MD5 object as returned from md5.new()
07:
08: public class MD5Object extends PyObject {
09: private String data;
10:
11: public int digest_size = 16;
12:
13: public MD5Object(String s) {
14: data = s;
15: }
16:
17: public MD5Object(PyObject arg) {
18: this ("");
19: update(arg);
20: }
21:
22: public PyObject update(PyObject arg) {
23: if (!(arg instanceof PyString))
24: // TBD: this should be able to call safeRepr() on the arg, but
25: // I can't currently do this because safeRepr is protected so
26: // that it's not accessible from Python. This is bogus;
27: // arbitrary Java code should be able to get safeRepr but we
28: // still want to hide it from Python. There should be another
29: // way to hide Java methods from Python.
30: throw Py.TypeError("argument 1 expected string");
31: data += arg.toString();
32: return Py.None;
33: }
34:
35: public PyObject digest() {
36: md md5obj = md.new_md5(data);
37: md5obj.calc();
38: // this is for compatibility with CPython's output
39: String s = md5obj.toString();
40: char[] x = new char[s.length() / 2];
41:
42: for (int i = 0, j = 0; i < s.length(); i += 2, j++) {
43: String chr = s.substring(i, i + 2);
44: x[j] = (char) java.lang.Integer.parseInt(chr, 16);
45: }
46: return new PyString(new String(x));
47: }
48:
49: public PyObject hexdigest() {
50: md md5obj = md.new_md5(data);
51: md5obj.calc();
52: // this is for compatibility with CPython's output
53: return new PyString(md5obj.toString());
54: }
55:
56: public PyObject copy() {
57: return new MD5Object(data);
58: }
59: }
|