Index: jython/src/org/python/modules/_hashlib.java =================================================================== --- jython/src/org/python/modules/_hashlib.java (revision 7292) +++ jython/src/org/python/modules/_hashlib.java (working copy) @@ -21,19 +21,22 @@ /** * 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. + * + * 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 algorithmMap = new HashMap() {{ + private static final Map algorithmMap = new HashMap() { + { put("sha1", "sha-1"); put("sha256", "sha-256"); put("sha384", "sha-384"); put("sha512", "sha-512"); - }}; + put("sha224", "sha-224"); + } + }; public static void classDictInit(PyObject dict) { dict.__setitem__("__name__", Py.newString("_hashlib")); @@ -51,7 +54,6 @@ if (algorithmMap.containsKey(name)) { name = algorithmMap.get(name); } - Hash hash = new Hash(name); if (obj != null) { hash.update(obj); @@ -95,10 +97,18 @@ return openssl_sha512(null); } + public static PyObject openssl_sha224() { + return openssl_sha224(null); + } + public static PyObject openssl_sha512(PyObject obj) { return new$("sha512", obj); } + public static PyObject openssl_sha224(PyObject obj) { + return new$("sha224", obj); + } + /** * A generic wrapper around a MessageDigest. */ @@ -115,13 +125,16 @@ private MessageDigest digest; /** Supposed block sizes of algorithms for the block_size attribute. */ - private static final Map blockSizes = new HashMap() {{ + private static final Map blockSizes = new HashMap() { + { put("md5", 64); put("sha-1", 64); put("sha-256", 64); put("sha-384", 128); + put("sha-224", 64); put("sha-512", 128); - }}; + } + }; public Hash(String name) { this(name, getDigest(name)); @@ -135,6 +148,10 @@ private static final MessageDigest getDigest(String name) { try { + // since sha 224 is not present in java.security + if (name.equals("sha-224")) { + return new SHA224Digest(); + } return MessageDigest.getInstance(name); } catch (NoSuchAlgorithmException nsae) { throw Py.ValueError("unsupported hash type"); @@ -143,20 +160,21 @@ /** * Clone the underlying MessageDigest. - * + * * @return a copy of MessageDigest */ private MessageDigest cloneDigest() { try { - return (MessageDigest)digest.clone(); + return (MessageDigest) digest.clone(); } catch (CloneNotSupportedException cnse) { - throw Py.RuntimeError(String.format("_hashlib.HASH (%s) internal error", name)); + 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() { @@ -171,15 +189,14 @@ final void HASH_update(PyObject obj) { String string; if (obj instanceof PyUnicode) { - string = ((PyUnicode)obj).encode(); + string = ((PyUnicode) obj).encode(); } else if (obj instanceof PyString) { string = obj.toString(); } else if (obj instanceof PyArray) { - string = ((PyArray)obj).tostring(); - } - else { + string = ((PyArray) obj).tostring(); + } else { throw Py.TypeError("update() argument 1 must be string or read-only buffer, not " - + obj.getType().fastGetName()); + + obj.getType().fastGetName()); } digest.update(StringUtil.toBytes(string)); } @@ -205,10 +222,10 @@ 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; + hexDigest[j++] = (char) c; c = result[i] & 0xf; c = c > 9 ? c + 'a' - 10 : c + '0'; - hexDigest[j++] = (char)c; + hexDigest[j++] = (char) c; } return Py.newString(new String(hexDigest)); } @@ -243,7 +260,8 @@ @Override public String toString() { - return String.format("<%s HASH object @ %s>", name, Py.idstr(this)); + return String + .format("<%s HASH object @ %s>", name, Py.idstr(this)); } } } Index: jython/Lib/test/test_hashlib.py =================================================================== --- jython/Lib/test/test_hashlib.py (revision 7292) +++ jython/Lib/test/test_hashlib.py (working copy) @@ -75,7 +75,6 @@ self.check('md5', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 'd174ab98d277d9f5a5611c2c9f419d9f') - # use the three examples from Federal Information Processing Standards # Publication 180-1, Secure Hash Standard, 1995 April 17 # http://www.itl.nist.gov/div897/pubs/fip180-1.htm @@ -186,13 +185,12 @@ def test_main(): if test_support.is_jython: # Java has no builtin support for sha224 - hashes = [hash for hash in HashLibTestCase.supported_hash_names - if hash.lower() != 'sha224'] + hashes = [hash for hash in HashLibTestCase.supported_hash_names] HashLibTestCase.supported_hash_names = tuple(hashes) - del HashLibTestCase.test_case_sha224_0 - del HashLibTestCase.test_case_sha224_1 - del HashLibTestCase.test_case_sha224_2 - del HashLibTestCase.test_case_sha224_3 + # del HashLibTestCase.test_case_sha224_0 + # del HashLibTestCase.test_case_sha224_1 + # del HashLibTestCase.test_case_sha224_2 + # del HashLibTestCase.test_case_sha224_3 test_support.run_unittest(HashLibTestCase)