blob: 51d73bc28106270f59cd56d8935a819b510e6d1b [file] [log] [blame]
Paul Kehrerb6d764c2014-01-27 22:32:11 -06001.. hazmat::
2
3Key Derivation Functions
4========================
5
6.. currentmodule:: cryptography.hazmat.primitives.kdf
7
8Key derivation functions derive key material from information such as passwords
9using a pseudo-random function (PRF).
10
11.. class:: PBKDF2(algorithm, length, salt, iterations, backend):
12
Paul Kehrer5d1af212014-01-28 12:19:32 -060013 .. versionadded:: 0.2
14
15 This class conforms to the
16 :class:`~cryptography.hazmat.primitives.interfaces.KeyDerivationFunction`
17 interface.
18
Paul Kehrerb6d764c2014-01-27 22:32:11 -060019 .. doctest::
20
Paul Kehrer5d1af212014-01-28 12:19:32 -060021 >>> import os
22 >>> from cryptography.hazmat.primitives import hashes
Paul Kehrerb6d764c2014-01-27 22:32:11 -060023 >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2
24 >>> from cryptography.hazmat.backends import default_backend
25 >>> backend = default_backend()
26 >>> salt = os.urandom(16)
27 >>> # derive
28 >>> kdf = PBKDF2(hashes.SHA1(), 20, salt, 10000, backend)
29 >>> key = kdf.derive(b"my great password")
30 >>> # verify
31 >>> kdf = PBKDF2(hashes.SHA1(), 20, salt, 10000, backend)
32 >>> kdf.verify(b"my great password", key)
Paul Kehrerb6d764c2014-01-27 22:32:11 -060033
Paul Kehrer5d1af212014-01-28 12:19:32 -060034 :param algorithm: An instance of a
35 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
36 provider.
37 :param int length: The desired length of the derived key. Maximum is
Paul Kehrerb6d764c2014-01-27 22:32:11 -060038 2\ :sup:`31` - 1.
Paul Kehrer5d1af212014-01-28 12:19:32 -060039 :param bytes salt: A salt. `NIST SP 800-132`_ recommends 128-bits or
40 longer.
41 :param int iterations: The number of iterations to perform of the hash
42 function.
43 :param backend: A
44 :class:`~cryptography.hazmat.backends.interfaces.CipherBackend`
45 provider.
Paul Kehrerb6d764c2014-01-27 22:32:11 -060046
47.. _`NIST SP 800-132`: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf