Skip to content

Instantly share code, notes, and snippets.

@emidln
Created February 5, 2013 03:21
Show Gist options
  • Save emidln/4711912 to your computer and use it in GitHub Desktop.
Save emidln/4711912 to your computer and use it in GitHub Desktop.
import ctypes
import scrypt
# This only supports OSX right now:
libc = ctypes.CDLL('libc.dylib')
SCRYPT_MAXTIME = 0.5
SCRYPT_SEED_LENGTH = 64
def randstr(length):
""" return length-sized random string using arc4random_uniform(3) """
return ''.join(chr(libc.arc4random_uniform(0, 255)) for i in range(length))
def hash_password(password, maxtime=SCRYPT_MAXTIME, datalength=SCRYPT_SEED_LENGTH):
""" return a hashed password using scrypt """
return scrypt.encrypt(randstr(datalength), password, maxtime=maxtime)
def verify_password(hashed_password, guessed_password, maxtime=SCRYPT_MAXTIME):
""" verify a password hashed with scrypt, returns True if guessed_password is valid """
try:
scrypt.decrypt(hashed_password, guessed_password, maxtime)
return True
except scrypt.error:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment