Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. def encode(self, password, salt):
  2. assert password
  3. assert salt and '$' not in salt
  4. hash = hashlib.sha512(salt + password).hexdigest()
  5. return "%s$%s$%s" % (self.algorithm, salt, hash)
  6.  
  7. class DrupalPasswordHasher(BasePasswordHasher):
  8. algorithm = "S"
  9. iter_code = 'C'
  10. salt_length = 8
  11.  
  12. def encode(self, password, salt, iter_code=None):
  13. """The Drupal 7 method of encoding passwords"""
  14. if iter_code == None:
  15. iterations = 2 ** _ITOA64.index(self.iter_code)
  16. else:
  17. iterations = 2 ** _ITOA64.index(iter_code)
  18. hash = hashlib.sha512(salt + password).digest()
  19.  
  20. for i in range(iterations):
  21. hash = hashlib.sha512(hash + password).digest()
  22.  
  23. l = len(hash)
  24.  
  25. output = ''
  26. i = 0
  27.  
  28. while i < l:
  29. value = ord(hash[i])
  30. i = i + 1
  31.  
  32. output += _ITOA64[value & 0x3f]
  33. if i < l:
  34. value |= ord(hash[i]) << 8
  35.  
  36. output += _ITOA64[(value >> 6) & 0x3f]
  37. if i >= l:
  38. break
  39. i += 1
  40.  
  41. if i < l:
  42. value |= ord(hash[i]) << 16
  43.  
  44. output += _ITOA64[(value >> 12) & 0x3f]
  45. if i >= l:
  46. break
  47. i += 1
  48.  
  49. output += _ITOA64[(value >> 18) & 0x3f]
  50.  
  51. longhashed = "%s$%s%s%s" % (self.algorithm, iter_code,
  52. salt, output)
  53. return longhashed[:54]
  54.  
  55. def verify(self, password, encoded):
  56. hash = encoded.split("$")[1]
  57. iter_code = hash[0]
  58. salt = hash[1:1 + self.salt_length]
  59. return encoded == self.encode(password, salt, iter_code)
  60.  
  61. import xml.etree.ElementTree as ET
  62. from django.contrib.auth.models import User
  63.  
  64. tree = ET.parse('/PATH/TO/Users.xml')
  65. root = tree.getroot()
  66.  
  67. for row in root:
  68. user_dict = {}
  69. for field in row:
  70. user_dict[field.attrib['name']] = field.text
  71. user = User.objects.create_user(user_dict['name'], user_dict['mail'])
  72. if user_dict['pass'][0] == '$':
  73. user_dict['pass'] = user_dict['pass'][1:]
  74. user.password = user_dict['pass']
  75. user.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement