Skip to content

Instantly share code, notes, and snippets.

@shimakaze-git
Created October 20, 2019 07:02
Show Gist options
  • Save shimakaze-git/e4ea834921e09164d5a22159a8c4ace5 to your computer and use it in GitHub Desktop.
Save shimakaze-git/e4ea834921e09164d5a22159a8c4ace5 to your computer and use it in GitHub Desktop.
def make_password(password, salt=None, hasher='default'):
"""
Turn a plain-text password into a hash for database storage
Same as encode() but generate a new random salt. If password is None then
return a concatenation of UNUSABLE_PASSWORD_PREFIX and a random string,
which disallows logins. Additional random string reduces chances of gaining
access to staff or superuser accounts. See ticket #20079 for more info.
"""
if password is None:
return UNUSABLE_PASSWORD_PREFIX + get_random_string(UNUSABLE_PASSWORD_SUFFIX_LENGTH)
hasher = get_hasher(hasher)
salt = salt or hasher.salt()
return hasher.encode(password, salt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment