Commits

Sever Băneșiu  committed 4bd02d5

Added bcrypt to bcrypt migration based on rounds value.

  • Participants
  • Parent commits b01ca46

Comments (0)

Files changed (2)

File django_bcrypt/models.py

     ``User.check_password()`` if the hash is incorrect.
 
     If ``BCRYPT_MIGRATE`` is set, attempts to convert sha1 password to bcrypt
-    or converts between different bcrypt cost values.
+    or converts between different bcrypt rounds values.
 
     .. note::
 
     if self.password.startswith('bc$'):
         salt_and_hash = self.password[3:]
         result = bcrypt.hashpw(raw_password, salt_and_hash) == salt_and_hash
+        rounds = int(salt_and_hash.split('$')[2])
+        if not rounds == get_rounds():
+            self.set_password(raw_password)
+            self.save()
     elif _check_password(self, raw_password):
         result = True
         if is_enabled() and migrate_to_bcrypt():

File django_bcrypt/tests.py

     def assertSha1(self, hashed, password):
         self.assertEqual(hashed[:5], 'sha1$')
 
-    def test_migrate_password(self):
+    def test_migrate_sha1_to_bcrypt(self):
         user = User()
         with settings(BCRYPT_MIGRATE=True, BCRYPT_ENABLED_UNDER_TEST=True):
             _set_password(user, 'password')
             self.assertTrue(bcrypt_check_password(user, 'password'))
             self.assertBcrypt(user.password, 'password')
 
+    def test_migrate_bcrypt_to_bcrypt(self):
+        user = User()
+        with settings(BCRYPT_MIGRATE=True,
+                      BCRYPT_ROUNDS=10,
+                      BCRYPT_ENABLED_UNDER_TEST=True):
+            user.set_password('password')
+        with settings(BCRYPT_MIGRATE=True,
+                      BCRYPT_ROUNDS=12,
+                      BCRYPT_ENABLED_UNDER_TEST=True):
+            user.check_password('password')
+        salt_and_hash = user.password[3:]
+        self.assertEquals(salt_and_hash.split('$')[2], '12')
+
     def test_no_migrate_password(self):
         user = User()
         with settings(BCRYPT_MIGRATE=False, BCRYPT_ENABLED_UNDER_TEST=True):