_argon2.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright 2013 Donald Stufft and individual contributors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import nacl.bindings
  15. _argon2_strbytes_plus_one = nacl.bindings.crypto_pwhash_STRBYTES
  16. PWHASH_SIZE = _argon2_strbytes_plus_one - 1
  17. SALTBYTES = nacl.bindings.crypto_pwhash_SALTBYTES
  18. PASSWD_MIN = nacl.bindings.crypto_pwhash_PASSWD_MIN
  19. PASSWD_MAX = nacl.bindings.crypto_pwhash_PASSWD_MAX
  20. PWHASH_SIZE = _argon2_strbytes_plus_one - 1
  21. BYTES_MAX = nacl.bindings.crypto_pwhash_BYTES_MAX
  22. BYTES_MIN = nacl.bindings.crypto_pwhash_BYTES_MIN
  23. ALG_ARGON2I13 = nacl.bindings.crypto_pwhash_ALG_ARGON2I13
  24. ALG_ARGON2ID13 = nacl.bindings.crypto_pwhash_ALG_ARGON2ID13
  25. ALG_ARGON2_DEFAULT = nacl.bindings.crypto_pwhash_ALG_DEFAULT
  26. def verify(password_hash: bytes, password: bytes) -> bool:
  27. """
  28. Takes a modular crypt encoded argon2i or argon2id stored password hash
  29. and checks if the user provided password will hash to the same string
  30. when using the stored parameters
  31. :param password_hash: password hash serialized in modular crypt() format
  32. :type password_hash: bytes
  33. :param password: user provided password
  34. :type password: bytes
  35. :rtype: boolean
  36. .. versionadded:: 1.2
  37. """
  38. return nacl.bindings.crypto_pwhash_str_verify(password_hash, password)