KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > modules > MD5Object


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.modules;
3
4 import org.python.core.*;
5
6 // Implementation of the MD5 object as returned from md5.new()
7

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