Skip to content

Instantly share code, notes, and snippets.

@dgrtwo
Created April 9, 2012 15:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dgrtwo/2344345 to your computer and use it in GitHub Desktop.
Save dgrtwo/2344345 to your computer and use it in GitHub Desktop.
Django password hasher for migration from Drupal
"""
DrupalPasswordHasher
To use, put this in any app and add to your settings.py, something like this:
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'myproject.myapp.drupal_hasher.DrupalPasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
'django.contrib.auth.hashers.SHA1PasswordHasher',
'django.contrib.auth.hashers.MD5PasswordHasher',
'django.contrib.auth.hashers.CryptPasswordHasher',
)
Three notes:
-This requires Drupal 1.4 or higher (with the introduction of the flexible password
hasher)
-In Drupal, the number of algorithm iterations is a power of 2,
which is represented by the first character after the $. Specifically, it
performs 2^(index within _ITOA64) iterations, such that 'C' represents 2^14
and 'D' would represent 2^15.
-In Drupal the passwords are stored as:
$S$<hash>
while in Django the passwords are stored as
S$<hash>
So you must cut off the first character of each password when migrating.
"""
import hashlib
from django.contrib.auth.hashers import BasePasswordHasher
_ITOA64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
class DrupalPasswordHasher(BasePasswordHasher):
algorithm = "S"
iterations = 'C'
salt_length = 8
def encode(self, password, salt, iterations=None):
"""The Drupal 7 method of encoding passwords"""
if iterations == None:
iterations = 2 ** _ITOA64.index(self.iterations)
hash = hashlib.sha512(salt + password).digest()
for i in range(iterations):
hash = hashlib.sha512(hash + password).digest()
l = len(hash)
output = ''
i = 0
while i < l:
value = ord(hash[i])
i = i + 1
output += _ITOA64[value & 0x3f]
if i < l:
value |= ord(hash[i]) << 8
output += _ITOA64[(value >> 6) & 0x3f]
if i >= l:
break
i += 1
if i < l:
value |= ord(hash[i]) << 16
output += _ITOA64[(value >> 12) & 0x3f]
if i >= l:
break
i += 1
output += _ITOA64[(value >> 18) & 0x3f]
longhashed = "%s$%s%s%s" % (self.algorithm, self.iterations,
salt, output)
return longhashed[:54]
def verify(self, password, encoded):
hash = encoded.split("$")[1]
iterations = 2 ** _ITOA64.index(hash[0])
salt = hash[1:1 + self.salt_length]
return encoded == self.encode(password, salt, iterations)
@skargo
Copy link

skargo commented Jan 21, 2015

Hi! I really need to use this snippet but as a newbie to django have no clue how to implement it. I appreciate if you add a brief docs. Thanks.

@m-aciek
Copy link

m-aciek commented Sep 13, 2016

Provided as a djangosnippet here.

But currently calculations are killing my machine…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment