| 1 |
nacl.pwhash |
| 2 |
=========== |
| 3 |
|
| 4 |
.. module:: nacl.pwhash |
| 5 |
|
| 6 |
The package pwhash provides implementations of modern *memory-hard* |
| 7 |
password hashing construction exposing modules with a uniform API. |
| 8 |
|
| 9 |
Functions exposed at top level |
| 10 |
------------------------------ |
| 11 |
|
| 12 |
The top level module only provides the functions implementing |
| 13 |
ascii encoded hashing and verification. |
| 14 |
|
| 15 |
.. function:: str(password, \ |
| 16 |
opslimit=OPSLIMIT_INTERACTIVE, \ |
| 17 |
memlimit=MEMLIMIT_INTERACTIVE) |
| 18 |
|
| 19 |
Returns a password verifier hash, generated with the password hasher |
| 20 |
chosen as a default by libsodium. |
| 21 |
|
| 22 |
:param password: password used to seed the key derivation procedure; |
| 23 |
it length must be between |
| 24 |
:py:const:`PASSWD_MIN` and |
| 25 |
:py:const:`PASSWD_MAX` |
| 26 |
:type password: bytes |
| 27 |
:param opslimit: the time component (operation count) |
| 28 |
of the key derivation procedure's computational cost; |
| 29 |
it must be between |
| 30 |
:py:const:`OPSLIMIT_MIN` and |
| 31 |
:py:const:`OPSLIMIT_MAX` |
| 32 |
:type opslimit: int |
| 33 |
:param memlimit: the memory occupation component |
| 34 |
of the key derivation procedure's computational cost; |
| 35 |
it must be between |
| 36 |
:py:const:`MEMLIMIT_MIN` and |
| 37 |
:py:const:`MEMLIMIT_MAX` |
| 38 |
:type memlimit: int |
| 39 |
:returns: the ascii encoded password hash along with a prefix encoding |
| 40 |
the used hashing construct, the random generated salt and |
| 41 |
the operation and memory limits used to generate the password hash |
| 42 |
:rtype: bytes |
| 43 |
|
| 44 |
|
| 45 |
As of PyNaCl version 1.2 this is :py:func:`nacl.pwhash.argon2id.str`. |
| 46 |
|
| 47 |
.. versionadded:: 1.2 |
| 48 |
|
| 49 |
|
| 50 |
.. function:: verify(password_hash, password) |
| 51 |
|
| 52 |
This function checks if hashing the proposed password, with |
| 53 |
the same construction and parameters encoded in the password hash |
| 54 |
would generate the same encoded string, thus verifying the |
| 55 |
correct password has been proposed in an authentication attempt. |
| 56 |
|
| 57 |
.. versionadded:: 1.2 |
| 58 |
|
| 59 |
.. rubric:: Module level constants |
| 60 |
|
| 61 |
The top level module defines the constants related to the :py:func:`str` |
| 62 |
hashing construct and its corresponding :py:func:`verify` password |
| 63 |
verifier. |
| 64 |
|
| 65 |
.. py:data:: PASSWD_MIN |
| 66 |
.. py:data:: PASSWD_MAX |
| 67 |
|
| 68 |
minimum and maximum length of the password to hash |
| 69 |
|
| 70 |
.. py:data:: PWHASH_SIZE |
| 71 |
|
| 72 |
maximum size of the encoded hash |
| 73 |
|
| 74 |
.. py:data:: OPSLIMIT_MIN |
| 75 |
.. py:data:: OPSLIMIT_MAX |
| 76 |
|
| 77 |
minimum and maximum operation count for the hashing construct |
| 78 |
|
| 79 |
.. py:data:: MEMLIMIT_MIN |
| 80 |
.. py:data:: MEMLIMIT_MAX |
| 81 |
|
| 82 |
minimum and maximum memory occupation for the hashing construct |
| 83 |
|
| 84 |
and the recommended values for the opslimit and memlimit parameters |
| 85 |
|
| 86 |
.. py:data:: MEMLIMIT_INTERACTIVE |
| 87 |
.. py:data:: OPSLIMIT_INTERACTIVE |
| 88 |
|
| 89 |
recommended values for the interactive user authentication password |
| 90 |
check case, leading to a sub-second hashing time |
| 91 |
|
| 92 |
.. py:data:: MEMLIMIT_SENSITIVE |
| 93 |
.. py:data:: OPSLIMIT_SENSITIVE |
| 94 |
|
| 95 |
recommended values for generating a password hash/derived key meant to protect |
| 96 |
sensitive data, leading to a multi-second hashing time |
| 97 |
|
| 98 |
.. py:data:: MEMLIMIT_MODERATE |
| 99 |
.. py:data:: OPSLIMIT_MODERATE |
| 100 |
|
| 101 |
values leading to a hashing time and memory cost intermediate between the |
| 102 |
interactive and the sensitive cases |
| 103 |
|
| 104 |
Per-mechanism password hashing implementation modules |
| 105 |
----------------------------------------------------- |
| 106 |
|
| 107 |
Along with the respective :py:func:`str` and :py:func:`verify` functions, |
| 108 |
the modules implementing named password hashing constructs expose also |
| 109 |
a :py:func:`kdf` function returning a raw pseudo-random bytes sequence |
| 110 |
derived from the input parameters |
| 111 |
|
| 112 |
nacl.pwhash.argon2id |
| 113 |
"""""""""""""""""""" |
| 114 |
|
| 115 |
.. module:: nacl.pwhash.argon2id |
| 116 |
|
| 117 |
.. function:: kdf(size, password, salt, \ |
| 118 |
opslimit=OPSLIMIT_SENSITIVE, \ |
| 119 |
memlimit=MEMLIMIT_SENSITIVE, \ |
| 120 |
encoder=nacl.encoding.RawEncoder) |
| 121 |
|
| 122 |
Derive a ``size`` bytes long key from a caller-supplied |
| 123 |
``password`` and ``salt`` pair using the ``argon2id`` partially |
| 124 |
data dependent memory-hard construct. |
| 125 |
|
| 126 |
:param size: derived key size, must be between |
| 127 |
:py:const:`BYTES_MIN` and |
| 128 |
:py:const:`BYTES_MAX` |
| 129 |
:type size: int |
| 130 |
:param password: password used to seed the key derivation procedure; |
| 131 |
it length must be between |
| 132 |
:py:const:`PASSWD_MIN` and |
| 133 |
:py:const:`PASSWD_MAX` |
| 134 |
:type password: bytes |
| 135 |
:param salt: **RANDOM** salt used in the key derivation procedure; |
| 136 |
its length must be exactly :py:const:`.SALTBYTES` |
| 137 |
:type salt: bytes |
| 138 |
:param opslimit: the time component (operation count) |
| 139 |
of the key derivation procedure's computational cost; |
| 140 |
it must be between |
| 141 |
:py:const:`OPSLIMIT_MIN` and |
| 142 |
:py:const:`OPSLIMIT_MAX` |
| 143 |
:type opslimit: int |
| 144 |
:param memlimit: the memory occupation component |
| 145 |
of the key derivation procedure's computational cost; |
| 146 |
it must be between |
| 147 |
:py:const:`MEMLIMIT_MIN` and |
| 148 |
:py:const:`MEMLIMIT_MAX` |
| 149 |
:type memlimit: int |
| 150 |
:rtype: bytes |
| 151 |
|
| 152 |
The default settings for opslimit and memlimit are those deemed |
| 153 |
correct for generating a key, which can be used to protect |
| 154 |
sensitive data for a long time, leading to a multi-second |
| 155 |
hashing time. |
| 156 |
|
| 157 |
.. versionadded:: 1.2 |
| 158 |
|
| 159 |
.. function:: str(password, \ |
| 160 |
opslimit=OPSLIMIT_INTERACTIVE, \ |
| 161 |
memlimit=MEMLIMIT_INTERACTIVE) |
| 162 |
|
| 163 |
Returns a password verifier hash, generated with the ``argon2id`` |
| 164 |
password hasher. |
| 165 |
|
| 166 |
See: :py:func:`nacl.pwhash.str` for the general API. |
| 167 |
|
| 168 |
.. versionadded:: 1.2 |
| 169 |
|
| 170 |
.. function:: verify(password_hash, password) |
| 171 |
|
| 172 |
This function verifies the proposed ``password``, using |
| 173 |
``password_hash`` as a password verifier. |
| 174 |
|
| 175 |
See: :py:func:`nacl.pwhash.verify` for the general API. |
| 176 |
|
| 177 |
.. versionadded:: 1.2 |
| 178 |
|
| 179 |
|
| 180 |
.. rubric:: Module level constants |
| 181 |
|
| 182 |
The module defines the constants related to the :py:func:`kdf` raw hashing |
| 183 |
construct |
| 184 |
|
| 185 |
.. py:data:: SALTBYTES |
| 186 |
|
| 187 |
the length of the random bytes sequence passed in as a salt to the |
| 188 |
:py:func:`kdf` |
| 189 |
|
| 190 |
.. py:data:: BYTES_MIN |
| 191 |
.. py:data:: BYTES_MAX |
| 192 |
|
| 193 |
the minimum and maximum allowed values for the ``size`` parameter |
| 194 |
of the :py:func:`kdf` |
| 195 |
|
| 196 |
|
| 197 |
The meaning of each of the constants |
| 198 |
|
| 199 |
.. py:data:: PASSWD_MIN |
| 200 |
.. py:data:: PASSWD_MAX |
| 201 |
.. py:data:: PWHASH_SIZE |
| 202 |
.. py:data:: OPSLIMIT_MIN |
| 203 |
.. py:data:: OPSLIMIT_MAX |
| 204 |
.. py:data:: MEMLIMIT_MIN |
| 205 |
.. py:data:: MEMLIMIT_MAX |
| 206 |
.. py:data:: MEMLIMIT_INTERACTIVE |
| 207 |
.. py:data:: OPSLIMIT_INTERACTIVE |
| 208 |
.. py:data:: MEMLIMIT_SENSITIVE |
| 209 |
.. py:data:: OPSLIMIT_SENSITIVE |
| 210 |
.. py:data:: MEMLIMIT_MODERATE |
| 211 |
.. py:data:: OPSLIMIT_MODERATE |
| 212 |
|
| 213 |
is the same as in :py:mod:`nacl.hash`. |
| 214 |
|
| 215 |
|
| 216 |
nacl.pwhash.argon2i |
| 217 |
""""""""""""""""""" |
| 218 |
|
| 219 |
.. module:: nacl.pwhash.argon2i |
| 220 |
|
| 221 |
.. function:: kdf(size, password, salt, \ |
| 222 |
opslimit=OPSLIMIT_SENSITIVE, \ |
| 223 |
memlimit=MEMLIMIT_SENSITIVE, \ |
| 224 |
encoder=nacl.encoding.RawEncoder) |
| 225 |
|
| 226 |
Derive a ``size`` bytes long key from a caller-supplied |
| 227 |
``password`` and ``salt`` pair using the ``argon2i`` |
| 228 |
data independent memory-hard construct. |
| 229 |
|
| 230 |
See: py:func:`nacl.pwhash.argon2id.kdf` for the general API. |
| 231 |
|
| 232 |
.. versionadded:: 1.2 |
| 233 |
|
| 234 |
.. function:: str(password, \ |
| 235 |
opslimit=OPSLIMIT_INTERACTIVE, \ |
| 236 |
memlimit=MEMLIMIT_INTERACTIVE) |
| 237 |
|
| 238 |
Returns a password verifier hash, generated with the ``argon2i`` |
| 239 |
password hasher. |
| 240 |
|
| 241 |
See: :py:func:`nacl.pwhash.str` for the general API. |
| 242 |
|
| 243 |
.. versionadded:: 1.2 |
| 244 |
|
| 245 |
.. function:: verify(password_hash, password) |
| 246 |
|
| 247 |
This function verifies the proposed ``password``, using |
| 248 |
``password_hash`` as a password verifier. |
| 249 |
|
| 250 |
See: :py:func:`nacl.pwhash.verify` for the general API. |
| 251 |
|
| 252 |
.. versionadded:: 1.2 |
| 253 |
|
| 254 |
.. rubric:: Module level constants |
| 255 |
|
| 256 |
The meaning of each of the constants |
| 257 |
|
| 258 |
.. py:data:: PASSWD_MIN |
| 259 |
.. py:data:: PASSWD_MAX |
| 260 |
.. py:data:: PWHASH_SIZE |
| 261 |
.. py:data:: SALTBYTES |
| 262 |
.. py:data:: BYTES_MIN |
| 263 |
.. py:data:: BYTES_MAX |
| 264 |
.. py:data:: OPSLIMIT_MIN |
| 265 |
.. py:data:: OPSLIMIT_MAX |
| 266 |
.. py:data:: MEMLIMIT_MIN |
| 267 |
.. py:data:: MEMLIMIT_MAX |
| 268 |
.. py:data:: MEMLIMIT_INTERACTIVE |
| 269 |
.. py:data:: OPSLIMIT_INTERACTIVE |
| 270 |
.. py:data:: MEMLIMIT_SENSITIVE |
| 271 |
.. py:data:: OPSLIMIT_SENSITIVE |
| 272 |
.. py:data:: MEMLIMIT_MODERATE |
| 273 |
.. py:data:: OPSLIMIT_MODERATE |
| 274 |
|
| 275 |
is the same as in :py:mod:`nacl.pwhash` |
| 276 |
and :py:mod:`nacl.pwhash.argon2id` |
| 277 |
|
| 278 |
nacl.pwhash.scrypt |
| 279 |
"""""""""""""""""" |
| 280 |
|
| 281 |
.. module:: nacl.pwhash.scrypt |
| 282 |
|
| 283 |
.. function:: kdf(size, password, salt, \ |
| 284 |
opslimit=OPSLIMIT_SENSITIVE, \ |
| 285 |
memlimit=MEMLIMIT_SENSITIVE, \ |
| 286 |
encoder=nacl.encoding.RawEncoder) |
| 287 |
|
| 288 |
Derive a ``size`` bytes long key from a caller-supplied |
| 289 |
``password`` and ``salt`` pair using the ``scrypt`` |
| 290 |
data dependent memory-hard construct. |
| 291 |
|
| 292 |
See: :py:func:`nacl.pwhash.argon2id.kdf` for the general API. |
| 293 |
|
| 294 |
:raises nacl.exceptions.UnavailableError: If called when using a |
| 295 |
minimal build of libsodium. |
| 296 |
|
| 297 |
.. versionadded:: 1.2 |
| 298 |
|
| 299 |
.. function:: str(password, \ |
| 300 |
opslimit=OPSLIMIT_INTERACTIVE, \ |
| 301 |
memlimit=MEMLIMIT_INTERACTIVE) |
| 302 |
|
| 303 |
Returns a password verifier hash, generated with the ``scrypt`` |
| 304 |
password hasher. |
| 305 |
|
| 306 |
See: :py:func:`nacl.pwhash.str` for the general API. |
| 307 |
|
| 308 |
:raises nacl.exceptions.UnavailableError: If called when using a |
| 309 |
minimal build of libsodium. |
| 310 |
|
| 311 |
.. versionadded:: 1.2 |
| 312 |
|
| 313 |
.. function:: verify(password_hash, password) |
| 314 |
|
| 315 |
This function verifies the proposed ``password``, using |
| 316 |
``password_hash`` as a password verifier. |
| 317 |
|
| 318 |
See: py:func:`nacl.pwhash.verify` for the general API. |
| 319 |
|
| 320 |
:raises nacl.exceptions.UnavailableError: If called when using a |
| 321 |
minimal build of libsodium. |
| 322 |
|
| 323 |
.. versionadded:: 1.2 |
| 324 |
|
| 325 |
.. rubric:: Module level constants |
| 326 |
|
| 327 |
The meaning of each of the constants |
| 328 |
|
| 329 |
.. py:data:: PASSWD_MIN |
| 330 |
.. py:data:: PASSWD_MAX |
| 331 |
.. py:data:: PWHASH_SIZE |
| 332 |
.. py:data:: SALTBYTES |
| 333 |
.. py:data:: BYTES_MIN |
| 334 |
.. py:data:: BYTES_MAX |
| 335 |
.. py:data:: OPSLIMIT_MIN |
| 336 |
.. py:data:: OPSLIMIT_MAX |
| 337 |
.. py:data:: MEMLIMIT_MIN |
| 338 |
.. py:data:: MEMLIMIT_MAX |
| 339 |
.. py:data:: MEMLIMIT_INTERACTIVE |
| 340 |
.. py:data:: OPSLIMIT_INTERACTIVE |
| 341 |
.. py:data:: MEMLIMIT_SENSITIVE |
| 342 |
.. py:data:: OPSLIMIT_SENSITIVE |
| 343 |
.. py:data:: MEMLIMIT_MODERATE |
| 344 |
.. py:data:: OPSLIMIT_MODERATE |
| 345 |
|
| 346 |
is the same as in :py:mod:`nacl.pwhash` |
| 347 |
and :py:mod:`nacl.pwhash.argon2id` |