Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def encode(self, password, salt):
- assert password
- assert salt and '$' not in salt
- hash = hashlib.sha512(salt + password).hexdigest()
- return "%s$%s$%s" % (self.algorithm, salt, hash)
- class DrupalPasswordHasher(BasePasswordHasher):
- algorithm = "S"
- iter_code = 'C'
- salt_length = 8
- def encode(self, password, salt, iter_code=None):
- """The Drupal 7 method of encoding passwords"""
- if iter_code == None:
- iterations = 2 ** _ITOA64.index(self.iter_code)
- else:
- iterations = 2 ** _ITOA64.index(iter_code)
- 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, iter_code,
- salt, output)
- return longhashed[:54]
- def verify(self, password, encoded):
- hash = encoded.split("$")[1]
- iter_code = hash[0]
- salt = hash[1:1 + self.salt_length]
- return encoded == self.encode(password, salt, iter_code)
- import xml.etree.ElementTree as ET
- from django.contrib.auth.models import User
- tree = ET.parse('/PATH/TO/Users.xml')
- root = tree.getroot()
- for row in root:
- user_dict = {}
- for field in row:
- user_dict[field.attrib['name']] = field.text
- user = User.objects.create_user(user_dict['name'], user_dict['mail'])
- if user_dict['pass'][0] == '$':
- user_dict['pass'] = user_dict['pass'][1:]
- user.password = user_dict['pass']
- user.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement