crypto.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """
  2. Django's standard crypto functions and utilities.
  3. """
  4. import hashlib
  5. import hmac
  6. import secrets
  7. from django.conf import settings
  8. from django.utils.encoding import force_bytes
  9. def salted_hmac(key_salt, value, secret=None):
  10. """
  11. Return the HMAC-SHA1 of 'value', using a key generated from key_salt and a
  12. secret (which defaults to settings.SECRET_KEY).
  13. A different key_salt should be passed in for every application of HMAC.
  14. """
  15. if secret is None:
  16. secret = settings.SECRET_KEY
  17. key_salt = force_bytes(key_salt)
  18. secret = force_bytes(secret)
  19. # We need to generate a derived key from our base key. We can do this by
  20. # passing the key_salt and our base key through a pseudo-random function and
  21. # SHA1 works nicely.
  22. key = hashlib.sha1(key_salt + secret).digest()
  23. # If len(key_salt + secret) > sha_constructor().block_size, the above
  24. # line is redundant and could be replaced by key = key_salt + secret, since
  25. # the hmac module does the same thing for keys longer than the block size.
  26. # However, we need to ensure that we *always* do this.
  27. return hmac.new(key, msg=force_bytes(value), digestmod=hashlib.sha1)
  28. def get_random_string(length=12,
  29. allowed_chars='abcdefghijklmnopqrstuvwxyz'
  30. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
  31. """
  32. Return a securely generated random string.
  33. The default length of 12 with the a-z, A-Z, 0-9 character set returns
  34. a 71-bit value. log_2((26+26+10)^12) =~ 71 bits
  35. """
  36. return ''.join(secrets.choice(allowed_chars) for i in range(length))
  37. def constant_time_compare(val1, val2):
  38. """Return True if the two strings are equal, False otherwise."""
  39. return secrets.compare_digest(force_bytes(val1), force_bytes(val2))
  40. def pbkdf2(password, salt, iterations, dklen=0, digest=None):
  41. """Return the hash of password using pbkdf2."""
  42. if digest is None:
  43. digest = hashlib.sha256
  44. dklen = dklen or None
  45. password = force_bytes(password)
  46. salt = force_bytes(salt)
  47. return hashlib.pbkdf2_hmac(digest().name, password, salt, iterations, dklen)