Skip to content

Instantly share code, notes, and snippets.

@danriti
Created June 4, 2012 16:27
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save danriti/2869387 to your computer and use it in GitHub Desktop.
Generate user activation key for a Django user
"""
The activation key for the ``UserProfile`` will be a
SHA1 hash, generated from a combination of the ``User``'s
email and a random salt.
"""
salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
email = user.email
if isinstance(email, unicode):
email = email.encode('utf-8')
activation_key = hashlib.sha1(salt+email).hexdigest()
@mlissner
Copy link

For anybody landing here, note that sha1 is no longer considered secure. SHA256 should be used instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment