Advertisement
Guest User

Untitled

a guest
May 29th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import hmac
  2. import hashlib
  3. import struct
  4.  
  5. is_python2 = True
  6.  
  7. def pbkdf_two(passwd, salt, iters=2048, keylen=64, digestmod=hashlib.sha512):
  8. import hmac
  9. import struct
  10. import hashlib
  11. dgsz = digestmod().digest_size if callable(digestmod) else digestmod.digest_size
  12. pwd, salt = [x if isinstance(x, bytes) else bytes(x, 'utf-8') for x in (passwd, salt)]
  13. if keylen is None: keylen = dgsz
  14. # Helper function which copies each iteration for h, where h is an hmac seeded with password
  15. def pbhelper(h, salt, itercount, blocksize):
  16. import struct
  17. def prf(h, data):
  18. hm = h.copy()
  19. hm.update(data)
  20. return hm.digest()
  21. U = prf(h, salt + struct.pack('>i', blocksize))
  22. T = U
  23. for j in range(2, itercount+1):
  24. U = prf(h, U)
  25. T = "".join([chr( ord(x) ^ ord(y) ) for (x, y) in zip( T, U )]) \
  26. if is_python2 else bytes([x ^ y for (x, y) in zip(T, U)]) # XORing
  27. return T
  28. L = int(keylen/dgsz) if (keylen%dgsz) else int(keylen/dgsz)+1 # L - number of output blocks to produce
  29. #if keylen % dgsz != 0: L += 1
  30. h = hmac.new(key=passwd, msg=None, digestmod=digestmod )
  31. T = b""
  32. for i in range(1, L+1):
  33. T += pbhelper(h, salt, iters, i)
  34. return T[:keylen]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement