File: hashlib.rst

package info (click to toggle)
python-nacl 1.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,776 kB
  • sloc: ansic: 45,889; python: 7,249; sh: 6,752; asm: 2,974; makefile: 1,011; cs: 35; xml: 30; pascal: 11
file content (81 lines) | stat: -rw-r--r-- 2,896 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
nacl.hashlib
============

.. currentmodule:: nacl.hashlib

The :py:mod:`nacl.hashlib` module exposes directly usable implementations
of raw constructs which libsodium exposes with simplified APIs, like the
ones in :py:mod:`nacl.hash` and in :py:mod:`nacl.pwhash`.

The :py:class:`~.blake2b` and :py:func:`~.scrypt` implementations
are as API compatible as possible with the corresponding ones added
to cpython standard library's hashlib module in cpython's version 3.6.


.. class:: blake2b(data=b'', digest_size=BYTES, key=b'', salt=b'', person=b'')

    Returns an hash object which exposes an API mostly compatible
    to python3.6's hashlib.blake2b (the only difference being missing
    support for tree hashing parameters in the contructor)

    The methods :py:func:`update`, :py:func:`copy`,
    :func:`digest` and :func:`hexdigest` have the same semantics
    as described in hashlib documentation.

    Each instance exposes the :py:attr:`digest_size`, :py:attr:`block_size`
    :py:attr:`name` properties as required by hashlib API.

    .. attribute:: MAX_DIGEST_SIZE

        the maximum allowed value of the requested digest_size

    .. attribute:: MAX_KEY_SIZE

        the maximum allowed size of the password parameter

    .. attribute:: PERSON_SIZE

        the maximimum size of the personalization

    .. attribute:: SALT_SIZE

        the maximimum size of the salt


.. function:: scrypt(password, salt='', n=2**20, r=8, p=1,\
                     maxmem=2**25, dklen=64)

    Derive a raw cryptographic key using the scrypt KDF.

    :param password: the input password
    :type password: bytes
    :param salt: a crypographically-strong random salt
    :type salt: bytes
    :param n: CPU/Memory cost factor
    :type n: int
    :param r: block size multiplier: the used block size will be 128 * r
    :type r: int
    :param p: requested parallelism: the number of indipendently running
              scrypt constructs which will contribute to the final key
              generation
    :type p: int
    :param maxmem: maximum memory the whole scrypt construct will be
                   entitled to use
    :type maxmem: int
    :param dklen: length of the derived key
    :type dklen: int
    :return: a buffer dklen bytes long containing the derived key
    :raises nacl.exceptions.UnavailableError: If called when using a
        minimal build of libsodium.

    Implements the same signature as the ``hashlib.scrypt`` implemented
    in cpython version 3.6

    The recommended values for n, r, p in 2012 were n = 2**14, r = 8, p = 1;
    as of 2016, libsodium suggests using n = 2**14, r = 8, p = 1
    in a "interactive" setting and n = 2**20, r = 8, p = 1
    in a "sensitive" setting.

    The total memory usage will respectively be a little greater than 16MB
    in the "interactive" setting, and a little greater than 1GB in the
    "sensitive" setting.