PBKDF2: added doctest
[python-cryptoplus.git] / src / CryptoPlus / Hash / python_PBKDF2.py
blobeb2a2eb565f549ea5c9a88e051f6216876d6c5f0
1 import pypbkdf2
2 from CryptoPlus.Hash import SHA as SHA1, HMAC
4 __all__ = ['new']
6 def new(passphrase, salt, iterations=1000, digestmodule=SHA1, macmodule=HMAC):
7 """PKCS#5 v2.0 Password-Based Key Derivation
9 passphrase = the passphrase, supplied as a raw string, to make a key from
10 salt = salt as raw string
11 iterations = amount of iterations (default = 1000)
12 digestmodule = digest function to use, supply as module
13 example: python_SHA from CryptoPlus.Hash
14 default: SHA1
15 macmodule = mac function to use, supply as module
16 example: HMAC from CryptoPlus.Hash
17 default: HMAC
19 => macmodule & digestmodule construct the pseudorandom function
20 => by default: HMAC-SHA1
22 Examples: (from: http://www.ietf.org/rfc/rfc3962.txt)
23 ==========
25 >>> from CryptoPlus.Hash import python_PBKDF2
27 >>> passphrase = "password"
28 >>> salt = "ATHENA.MIT.EDUraeburn"
29 >>> iterations = 1
30 >>> hasher = python_PBKDF2.new(passphrase,salt,iterations)
31 >>> hasher.hexread(16)
32 'cdedb5281bb2f801565a1122b2563515'
34 >>> passphrase = "password"
35 >>> salt = "ATHENA.MIT.EDUraeburn"
36 >>> iterations = 1200
37 >>> hasher = python_PBKDF2.new(passphrase,salt,iterations)
38 >>> hasher.hexread(32)
39 '5c08eb61fdf71e4e4ec3cf6ba1f5512ba7e52ddbc5e5142f708a31e2e62b1e13'
41 >>> passphrase = "X"*64
42 >>> salt = "pass phrase equals block size"
43 >>> iterations = 1200
44 >>> hasher = python_PBKDF2.new(passphrase,salt,iterations)
45 >>> hasher.hexread(32)
46 '139c30c0966bc32ba55fdbf212530ac9c5ec59f1a452f5cc9ad940fea0598ed1'
48 >>> passphrase = "X"*65
49 >>> salt = "pass phrase exceeds block size"
50 >>> iterations = 1200
51 >>> hasher = python_PBKDF2.new(passphrase,salt,iterations)
52 >>> hasher.hexread(32)
53 '9ccad6d468770cd51b10e6a68721be611a8b4d282601db3b36be9246915ec82a'
54 """
55 return pypbkdf2.PBKDF2(passphrase, salt, iterations, digestmodule, macmodule)