Skip to content

Commit afdefb3

Browse files
author
Christophe Oosterlynck
committed
PBKDF2: added doctest
1 parent ffac5d7 commit afdefb3

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed
Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
11
import pypbkdf2
2-
from CryptoPlus.Hash import SHA1, HMAC
2+
from CryptoPlus.Hash import SHA as SHA1, HMAC
33

44
__all__ = ['new']
55

66
def new(passphrase, salt, iterations=1000, digestmodule=SHA1, macmodule=HMAC):
7+
"""PKCS#5 v2.0 Password-Based Key Derivation
8+
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
18+
19+
=> macmodule & digestmodule construct the pseudorandom function
20+
=> by default: HMAC-SHA1
21+
22+
Examples: (from: http://www.ietf.org/rfc/rfc3962.txt)
23+
==========
24+
25+
>>> from CryptoPlus.Hash import python_PBKDF2
26+
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'
33+
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'
40+
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'
47+
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+
"""
755
return pypbkdf2.PBKDF2(passphrase, salt, iterations, digestmodule, macmodule)

test/test_doctests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
DES3, python_Blowfish, Blowfish, python_Twofish, python_Serpent,\
1010
python_Rijndael, CAST, ARC2, python_PRESENT
1111
from CryptoPlus.Util import padding
12-
from CryptoPlus.Hash import python_RadioGatun
12+
from CryptoPlus.Hash import python_RadioGatun, python_PBKDF2
1313
try:
1414
from CryptoPlus.Cipher import IDEA
1515
from CryptoPlus.Cipher import RC5
@@ -21,7 +21,7 @@
2121
#for mod in (CryptoPlus.Cipher.python_AES,CryptoPlus.Cipher.python_AES):
2222
for mod in python_AES, AES, python_DES, DES, python_DES3, DES3, python_Blowfish,\
2323
Blowfish, python_Twofish, python_Serpent, python_Rijndael, CAST, ARC2,\
24-
python_PRESENT, padding, python_RadioGatun:
24+
python_PRESENT, padding, python_RadioGatun, python_PBKDF2:
2525
suite.addTest(doctest.DocTestSuite(mod))
2626
if not import_error:
2727
suite.addTest(doctest.DocTestSuite(IDEA))

0 commit comments

Comments
 (0)