| 1 |
nacl.hashlib |
| 2 |
============ |
| 3 |
|
| 4 |
.. currentmodule:: nacl.hashlib |
| 5 |
|
| 6 |
The :py:mod:`nacl.hashlib` module exposes directly usable implementations |
| 7 |
of raw constructs which libsodium exposes with simplified APIs, like the |
| 8 |
ones in :py:mod:`nacl.hash` and in :py:mod:`nacl.pwhash`. |
| 9 |
|
| 10 |
The :py:class:`~.blake2b` and :py:func:`~.scrypt` implementations |
| 11 |
are as API compatible as possible with the corresponding ones added |
| 12 |
to cpython standard library's hashlib module in cpython's version 3.6. |
| 13 |
|
| 14 |
|
| 15 |
.. class:: blake2b(data=b'', digest_size=BYTES, key=b'', salt=b'', person=b'') |
| 16 |
|
| 17 |
Returns an hash object which exposes an API mostly compatible |
| 18 |
to python3.6's hashlib.blake2b (the only difference being missing |
| 19 |
support for tree hashing parameters in the constructor) |
| 20 |
|
| 21 |
The methods :py:func:`update`, :py:func:`copy`, |
| 22 |
:func:`digest` and :func:`hexdigest` have the same semantics |
| 23 |
as described in hashlib documentation. |
| 24 |
|
| 25 |
Each instance exposes the :py:attr:`digest_size`, :py:attr:`block_size` |
| 26 |
:py:attr:`name` properties as required by hashlib API. |
| 27 |
|
| 28 |
.. attribute:: MAX_DIGEST_SIZE |
| 29 |
|
| 30 |
the maximum allowed value of the requested digest_size |
| 31 |
|
| 32 |
.. attribute:: MAX_KEY_SIZE |
| 33 |
|
| 34 |
the maximum allowed size of the password parameter |
| 35 |
|
| 36 |
.. attribute:: PERSON_SIZE |
| 37 |
|
| 38 |
the maximum size of the personalization |
| 39 |
|
| 40 |
.. attribute:: SALT_SIZE |
| 41 |
|
| 42 |
the maximum size of the salt |
| 43 |
|
| 44 |
|
| 45 |
.. function:: scrypt(password, salt='', n=2**20, r=8, p=1,\ |
| 46 |
maxmem=2**25, dklen=64) |
| 47 |
|
| 48 |
Derive a raw cryptographic key using the scrypt KDF. |
| 49 |
|
| 50 |
:param password: the input password |
| 51 |
:type password: bytes |
| 52 |
:param salt: a crypographically-strong random salt |
| 53 |
:type salt: bytes |
| 54 |
:param n: CPU/Memory cost factor |
| 55 |
:type n: int |
| 56 |
:param r: block size multiplier: the used block size will be 128 * r |
| 57 |
:type r: int |
| 58 |
:param p: requested parallelism: the number of independently running |
| 59 |
scrypt constructs which will contribute to the final key |
| 60 |
generation |
| 61 |
:type p: int |
| 62 |
:param maxmem: maximum memory the whole scrypt construct will be |
| 63 |
entitled to use |
| 64 |
:type maxmem: int |
| 65 |
:param dklen: length of the derived key |
| 66 |
:type dklen: int |
| 67 |
:return: a buffer dklen bytes long containing the derived key |
| 68 |
:raises nacl.exceptions.UnavailableError: If called when using a |
| 69 |
minimal build of libsodium. |
| 70 |
|
| 71 |
Implements the same signature as the ``hashlib.scrypt`` implemented |
| 72 |
in cpython version 3.6 |
| 73 |
|
| 74 |
The recommended values for n, r, p in 2012 were n = 2**14, r = 8, p = 1; |
| 75 |
as of 2016, libsodium suggests using n = 2**14, r = 8, p = 1 |
| 76 |
in a "interactive" setting and n = 2**20, r = 8, p = 1 |
| 77 |
in a "sensitive" setting. |
| 78 |
|
| 79 |
The total memory usage will respectively be a little greater than 16MB |
| 80 |
in the "interactive" setting, and a little greater than 1GB in the |
| 81 |
"sensitive" setting. |