1 package org.python.modules; 3 4 import org.python.core.*; 5 6 8 public class MD5Object extends PyObject 9 { 10 private String data; 11 12 public MD5Object(String 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 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 String 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 chr = s.substring(i, i+2); 43 x[j] = (char)java.lang.Integer.parseInt(chr, 16); 44 } 45 return new PyString(new String (x)); 46 } 47 48 public PyObject hexdigest() { 49 md md5obj = md.new_md5(data); 50 md5obj.calc(); 51 return new PyString(md5obj.toString()); 53 } 54 55 public PyObject copy() { 56 return new MD5Object(data); 57 } 58 } 59 | Popular Tags |