From: <pj...@us...> - 2009-07-07 03:41:55
|
Revision: 6517 http://jython.svn.sourceforge.net/jython/?rev=6517&view=rev Author: pjenvey Date: 2009-07-07 03:41:42 +0000 (Tue, 07 Jul 2009) Log Message: ----------- convert unicode hashlib input to str via defaultencoding fixes #1189 Modified Paths: -------------- trunk/jython/NEWS trunk/jython/src/org/python/modules/_hashlib.java Added Paths: ----------- trunk/jython/Lib/test/test_hashlib_jy.py Added: trunk/jython/Lib/test/test_hashlib_jy.py =================================================================== --- trunk/jython/Lib/test/test_hashlib_jy.py (rev 0) +++ trunk/jython/Lib/test/test_hashlib_jy.py 2009-07-07 03:41:42 UTC (rev 6517) @@ -0,0 +1,23 @@ +# encoding: utf-8 +"""Misc hashlib tests + +Made for Jython. +""" +import hashlib +import unittest +from test import test_support + +class HashlibTestCase(unittest.TestCase): + + def test_unicode(self): + self.assertEqual(hashlib.md5(u'foo').hexdigest(), + 'acbd18db4cc2f85cedef654fccc4a4d8') + self.assertRaises(UnicodeEncodeError, hashlib.md5, u'Gráin amháiñ') + + +def test_main(): + test_support.run_unittest(HashlibTestCase) + + +if __name__ == '__main__': + test_main() Modified: trunk/jython/NEWS =================================================================== --- trunk/jython/NEWS 2009-07-07 02:24:24 UTC (rev 6516) +++ trunk/jython/NEWS 2009-07-07 03:41:42 UTC (rev 6517) @@ -9,6 +9,7 @@ - [ 1365 ] continuation lines fail in interactive interpreter - [ 1377 ] Event names shadowed by a field name on Java types leads to a NPE - [ 1381 ] Redundant declarations of interface implementation hides overriden methods + - [ 1189 ] MD5 hash is incorrectly calculated when string contains non-latin chars and using python md5 lib Jython 2.5.0 The same as rc4. Modified: trunk/jython/src/org/python/modules/_hashlib.java =================================================================== --- trunk/jython/src/org/python/modules/_hashlib.java 2009-07-07 02:24:24 UTC (rev 6516) +++ trunk/jython/src/org/python/modules/_hashlib.java 2009-07-07 03:41:42 UTC (rev 6517) @@ -7,10 +7,11 @@ import java.util.Map; import org.python.core.ClassDictInit; +import org.python.core.Py; import org.python.core.PyObject; import org.python.core.PyString; import org.python.core.PyType; -import org.python.core.Py; +import org.python.core.PyUnicode; import org.python.core.util.StringUtil; import org.python.expose.ExposedGet; import org.python.expose.ExposedMethod; @@ -105,7 +106,7 @@ public static final PyType TYPE = PyType.fromClass(Hash.class); - /** The hash algorithm name */ + /** The hash algorithm name. */ @ExposedGet public String name; @@ -121,14 +122,6 @@ put("sha-512", 128); }}; - private static final MessageDigest getDigest(String name) { - try { - return MessageDigest.getInstance(name); - } catch (NoSuchAlgorithmException nsae) { - throw Py.ValueError("unsupported hash type"); - } - } - public Hash(String name) { this(name, getDigest(name)); } @@ -139,6 +132,14 @@ this.digest = digest; } + private static final MessageDigest getDigest(String name) { + try { + return MessageDigest.getInstance(name); + } catch (NoSuchAlgorithmException nsae) { + throw Py.ValueError("unsupported hash type"); + } + } + /** * Clone the underlying MessageDigest. * @@ -171,6 +172,9 @@ throw Py.TypeError("update() argument 1 must be string or read-only buffer, not " + obj.getType().fastGetName()); } + if (obj instanceof PyUnicode) { + obj = obj.__str__(); + } byte[] bytes = ((PyString)obj).toBytes(); digest.update(bytes); } @@ -232,6 +236,7 @@ return Py.newInteger(size); } + @Override public String toString() { return String.format("<%s HASH object @ %s>", name, Py.idstr(this)); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |