<commit_before>

from __future__ import print_function

__all__ = ['DrupalPasswordHasher']

import hashlib

# Copied from: https://djangosnippets.org/snippets/2729/#c4520


"""
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',
)

Two notes: First, in Drupal the number of iterations is a multiple of 2,
which is represented by the first character after the $, so set the iterations
class member appropriately.

Secondly, 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, sys

import six

_ITOA64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

IS_Pv3 = sys.version_info[0] > 2


class DrupalPasswordHasher(object):
    algorithm = u'S'
    iter_code = u'C'
    salt_length = 8

    def encode(self, password, salt, iter_code=None):
        """The Drupal 7 method of encoding passwords"""
        
        if isinstance(password, six.string_types):
            b_password = password.encode("utf8")
        else:
            b_password = password

        if isinstance(salt, six.string_types): 
            b_salt = salt.encode("utf8")
        else:
            b_salt = salt

        if iter_code == None:
            iterations = 2 ** _ITOA64.index(self.iter_code)
        else:
            iterations = 2 ** _ITOA64.index(iter_code)
        hash = hashlib.sha512(b_salt + b_password).digest()

        for i in range(iterations):
            hash = hashlib.sha512(hash + b_password).digest()

        l = len(hash)<commit_msg>Changed isinstance check to try/except<commit_after>

from __future__ import print_function

__all__ = ['DrupalPasswordHasher']

import hashlib

# Copied from: https://djangosnippets.org/snippets/2729/#c4520


"""
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',
)

Two notes: First, in Drupal the number of iterations is a multiple of 2,
which is represented by the first character after the $, so set the iterations
class member appropriately.

Secondly, 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, sys

_ITOA64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

IS_Pv3 = sys.version_info[0] > 2


class DrupalPasswordHasher(object):
    algorithm = u'S'
    iter_code = u'C'
    salt_length = 8

    def encode(self, password, salt, iter_code=None):
        """The Drupal 7 method of encoding passwords"""
        
        try:
            b_password = password.encode("utf8")
        except Exception as e:
            b_password = password

        try:
            b_salt = salt.encode("utf8")
        except Exception as e:
            b_salt = salt


        if iter_code == None:
            iterations = 2 ** _ITOA64.index(self.iter_code)
        else:
            iterations = 2 ** _ITOA64.index(iter_code)
        hash = hashlib.sha512(b_salt + b_password).digest()

        for i in range(iterations):
            hash = hashlib.sha512(hash + b_password).digest()

        l = len(hash)
{'fraction_non_alphanumeric': 0.09285888374360224, 'fraction_numerical': 0.021935169388252497, 'mean_word_length': 3.2178828365878727, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5619265', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1291, 'n_words': 333}