Commits

Daniel Holth  committed 1ea4b85

All tests pass on Python 2.7 and Python 3.3

  • Participants
  • Parent commits c216917

Comments (0)

Files changed (3)

File cryptacular/core/__init__.py

 ]
 
 
-if 'unicode' in __builtins__:
+if 'unicode' in __builtins__: # pragma NO COVERAGE
     def check_unicode(text):
         if isinstance(text, unicode):
             text = text.encode('utf-8')
+        if not isinstance(text, str):
+            raise TypeError()
         return text
 else: # pragma NO COVERAGE
     def check_unicode(text):
         # In Python3, PyArg_ParseTuple("ss") in the builtin crypt module
         # and our _bcrypt.c encodes unicode as utf-8, which falls short
         # of dealing with bytes but is nearly what we want.
+        if not isinstance(text, str):
+            raise TypeError('expected str')
         return text
 
 

File cryptacular/crypt/__init__.py

 import crypt as system_crypt
 import base64
 
+from cryptacular.core import check_unicode
 import cryptacular.core
 
 OLDCRYPT = ""
 
     def encode(self, password):
         """Hash a password using the builtin crypt module."""
-        salt = self.PREFIX + base64.b64encode(os.urandom(12), altchars='./')
-        if isinstance(password, unicode):
-            password = password.encode('utf-8')
-        if not isinstance(password, str):
-            raise TypeError("password must be a str")
+        salt = self.PREFIX
+        salt += base64.b64encode(os.urandom(12), altchars=b'./').decode('utf-8')
+        password = check_unicode(password)
         rc = self._crypt(password, salt)
         return rc
 
     def check(self, encoded, password):
         """Check a bcrypt password hash against a password."""
-        if isinstance(password, unicode):
-            password = password.encode('utf-8')
-        if isinstance(encoded, unicode):
-            encoded = encoded.encode('utf-8')
-        if not isinstance(password, str):
-            raise TypeError("password must be a str")
-        if not isinstance(encoded, str):
-            raise TypeError("encoded must be a str")
+        password = check_unicode(password)
+        encoded = check_unicode(encoded)
         if not self.match(encoded):
             return False
         rc = self._crypt(password, encoded)

File cryptacular/crypt/test_crypt.py

+from __future__ import unicode_literals
 from nose.tools import eq_, raises, assert_false, assert_true, assert_not_equal
 from cryptacular.crypt import *
 
 class TestCRYPTPasswordManager(object):
-    snowpass = u"hashy the \N{SNOWMAN}"
+    snowpass = "hashy the \N{SNOWMAN}"
 
     PREFIX = OLDCRYPT
 
         password = "xyzzy"
         hash = manager.encode(password)
         assert_true(manager.check(hash, password))
-        assert_true(manager.check(unicode(hash), password))
         assert_false(manager.check(password, password))
         assert_not_equal(manager.encode(password), manager.encode(password))