/usr/lib/python3/dist-packages/nacl/pwhash.py is in python3-nacl 1.1.2-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | # Copyright 2013 Donald Stufft and individual contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
import nacl.bindings
import nacl.encoding
import nacl.exceptions as exc
from nacl.exceptions import ensure
_strbytes_plus_one = nacl.bindings.crypto_pwhash_scryptsalsa208sha256_STRBYTES
SCRYPT_SALTBYTES = nacl.bindings.crypto_pwhash_scryptsalsa208sha256_SALTBYTES
SCRYPT_PWHASH_SIZE = _strbytes_plus_one - 1
SCRYPT_OPSLIMIT_INTERACTIVE = \
nacl.bindings.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE
SCRYPT_MEMLIMIT_INTERACTIVE = \
nacl.bindings.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE
SCRYPT_OPSLIMIT_SENSITIVE = \
nacl.bindings.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE
SCRYPT_MEMLIMIT_SENSITIVE = \
nacl.bindings.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE
def kdf_scryptsalsa208sha256(size, password, salt,
opslimit=SCRYPT_OPSLIMIT_SENSITIVE,
memlimit=SCRYPT_MEMLIMIT_SENSITIVE,
encoder=nacl.encoding.RawEncoder):
"""
Makes a key defined from ``password`` and ``salt`` that is
``size`` bytes long
the enclosing module provides the constants
- :py:const:`.SCRYPT_OPSLIMIT_INTERACTIVE`
- :py:const:`.SCRYPT_MEMLIMIT_INTERACTIVE`
- :py:const:`.SCRYPT_OPSLIMIT_SENSITIVE`
- :py:const:`.SCRYPT_MEMLIMIT_SENSITIVE`
as a guidance for correct settings respectively for the
interactive login and the long term key protecting sensitive data
use cases.
:param int size: int
:param bytes password: bytes
:param bytes salt: bytes
:param int opslimit:
:param int memlimit:
:rtype: bytes
"""
ensure(len(salt) == SCRYPT_SALTBYTES,
"The salt must be exactly %s, not %s bytes long" % (
SCRYPT_SALTBYTES,
len(salt)
),
raising=exc.ValueError
)
n_log2, r, p = nacl.bindings.nacl_bindings_pick_scrypt_params(opslimit,
memlimit)
maxmem = memlimit + (2 ** 16)
return encoder.encode(
nacl.bindings.crypto_pwhash_scryptsalsa208sha256_ll(
password, salt, 2 ** n_log2, r, p, maxmem=maxmem, dklen=size)
)
def scryptsalsa208sha256_str(password,
opslimit=SCRYPT_OPSLIMIT_INTERACTIVE,
memlimit=SCRYPT_MEMLIMIT_INTERACTIVE):
"""
Hashes a password with a random salt, returning an ascii string
that has all the needed info to check against a future password
The default settings for opslimit and memlimit are those deemed
correct for the interactive user login case.
:param bytes password:
:param int opslimit:
:param int memlimit:
:rtype: bytes
"""
return nacl.bindings.crypto_pwhash_scryptsalsa208sha256_str(password,
opslimit,
memlimit)
def verify_scryptsalsa208sha256(password_hash, password):
"""
Takes the output of scryptsalsa208sha256 and compares it against
a user provided password to see if they are the same
:param password_hash: bytes
:param password: bytes
:rtype: boolean
"""
ensure(len(password_hash) == SCRYPT_PWHASH_SIZE,
"The password hash must be exactly %s bytes long" %
nacl.bindings.crypto_pwhash_scryptsalsa208sha256_STRBYTES,
raising=exc.ValueError)
return nacl.bindings.crypto_pwhash_scryptsalsa208sha256_str_verify(
password_hash, password
)
|