Skip to content

Instantly share code, notes, and snippets.

@jayd3e
Created July 11, 2012 05:42
Show Gist options
  • Save jayd3e/3088204 to your computer and use it in GitHub Desktop.
Save jayd3e/3088204 to your computer and use it in GitHub Desktop.
Signing a value
from hashlib import sha1
# This is what we sign our values with
secret = '2q3j4q23ruas9fu28qu3rjsfia9uq23'
# This is the hash that we are going to pass to our user's e-mail
sent_hash = sha1()
sent_hash.update(os.urandom(60))
signature = sha1()
signature.update(secret + sent_hash.hexdigest())
# This is what we are going to store in our db or server-side session
stored_hash = signature.hexdigest() + sent_hash.hexdigest()
# Sent this to the user's e-mail
return sent_hash.hexdigest()
# Then once they click the link with the sent_hash, we'll get a request
# like www.example.com/verify?sent_hash=2352j3iofsd9u2q93risdfja90u309qr23rwjf.
# So we grab that sent_hash value, and make sure it matches our signature from before.
stored_hash = # Get from db
signature = sha1()
signature.update(secret + sent_hash.hexdigest())
if signature.hexdigest() == stored_hash[:40]:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment