def pbkdf2(password, salt, iterations, dklen=0, digest=None):
 assert (iterations > 0)
 if (not digest):
  digest = hashlib.sha256
 password = force_bytes(password)
 salt = force_bytes(salt)
 hlen = digest().digest_size
 if (not dklen):
  dklen = hlen
 if (dklen > (((2 ** 32) - 1) * hlen)):
  raise OverflowError(u'dklen too big')
 l = (- ((- dklen) // hlen))
 r = (dklen - ((l - 1) * hlen))
 hex_format_string = (u'%%0%ix' % (hlen * 2))
 (inner, outer) = (digest(), digest())
 if (len(password) > inner.block_size):
  password = digest(password).digest()
 password += ('\x00' * (inner.block_size - len(password)))
 inner.update(password.translate(hmac.trans_36))
 outer.update(password.translate(hmac.trans_5C))
 def F(i):
  def U():
   u = (salt + struct.pack('>I', i))
   for j in xrange(int(iterations)):
    (dig1, dig2) = (inner.copy(), outer.copy())
    dig1.update(u)
    dig2.update(dig1.digest())
    u = dig2.digest()
    (yield _bin_to_long(u))
  return _long_to_bin(reduce(operator.xor, U()), hex_format_string)
 T = [F(x) for x in range(1, (l + 1))]
 return (''.join(T[:(-1)]) + T[(-1)][:r])

def pbkdf2 ( password , salt , iterations , dklen = 0 , digest = None ) : assert ( iterations > 0 ) if ( not digest ) : digest = hashlib . sha256 password = force_bytes ( password ) salt = force_bytes ( salt ) hlen = digest ( ) . digest_size if ( not dklen ) : dklen = hlen if ( dklen > ( ( ( 2 ** 32 ) - 1 ) * hlen ) ) : raise OverflowError ( u'dklen too big' ) l = ( - ( ( - dklen ) // hlen ) ) r = ( dklen - ( ( l - 1 ) * hlen ) ) hex_format_string = ( u'%%0%ix' % ( hlen * 2 ) ) ( inner , outer ) = ( digest ( ) , digest ( ) ) if ( len ( password ) > inner . block_size ) : password = digest ( password ) . digest ( ) password += ( '\x00' * ( inner . block_size - len ( password ) ) ) inner . update ( password . translate ( hmac . trans_36 ) ) outer . update ( password . translate ( hmac . trans_5C ) ) def F ( i ) : def U ( ) : u = ( salt + struct . pack ( '>I' , i ) ) for j in xrange ( int ( iterations ) ) : ( dig1 , dig2 ) = ( inner . copy ( ) , outer . copy ( ) ) dig1 . update ( u ) dig2 . update ( dig1 . digest ( ) ) u = dig2 . digest ( ) ( yield _bin_to_long ( u ) ) return _long_to_bin ( reduce ( operator . xor , U ( ) ) , hex_format_string ) T = [ F ( x ) for x in range ( 1 , ( l + 1 ) ) ] return ( '' . join ( T [ : ( - 1 ) ] ) + T [ ( - 1 ) ] [ : r ] )
module function to hash a password according to the pbkdf2 specification .