__init__.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright 2017 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. from nacl.exceptions import CryptPrefixError
  15. from . import _argon2, argon2i, argon2id, scrypt
  16. STRPREFIX = argon2id.STRPREFIX
  17. PWHASH_SIZE = argon2id.PWHASH_SIZE
  18. assert _argon2.ALG_ARGON2_DEFAULT == _argon2.ALG_ARGON2ID13
  19. # since version 1.0.15 of libsodium
  20. PASSWD_MIN = argon2id.PASSWD_MIN
  21. PASSWD_MAX = argon2id.PASSWD_MAX
  22. MEMLIMIT_MAX = argon2id.MEMLIMIT_MAX
  23. MEMLIMIT_MIN = argon2id.MEMLIMIT_MIN
  24. OPSLIMIT_MAX = argon2id.OPSLIMIT_MAX
  25. OPSLIMIT_MIN = argon2id.OPSLIMIT_MIN
  26. OPSLIMIT_INTERACTIVE = argon2id.OPSLIMIT_INTERACTIVE
  27. MEMLIMIT_INTERACTIVE = argon2id.MEMLIMIT_INTERACTIVE
  28. OPSLIMIT_MODERATE = argon2id.OPSLIMIT_MODERATE
  29. MEMLIMIT_MODERATE = argon2id.MEMLIMIT_MODERATE
  30. OPSLIMIT_SENSITIVE = argon2id.OPSLIMIT_SENSITIVE
  31. MEMLIMIT_SENSITIVE = argon2id.MEMLIMIT_SENSITIVE
  32. str = argon2id.str
  33. assert argon2i.ALG != argon2id.ALG
  34. SCRYPT_SALTBYTES = scrypt.SALTBYTES
  35. SCRYPT_PWHASH_SIZE = scrypt.PWHASH_SIZE
  36. SCRYPT_OPSLIMIT_INTERACTIVE = scrypt.OPSLIMIT_INTERACTIVE
  37. SCRYPT_MEMLIMIT_INTERACTIVE = scrypt.MEMLIMIT_INTERACTIVE
  38. SCRYPT_OPSLIMIT_SENSITIVE = scrypt.OPSLIMIT_SENSITIVE
  39. SCRYPT_MEMLIMIT_SENSITIVE = scrypt.MEMLIMIT_SENSITIVE
  40. kdf_scryptsalsa208sha256 = scrypt.kdf
  41. scryptsalsa208sha256_str = scrypt.str
  42. verify_scryptsalsa208sha256 = scrypt.verify
  43. def verify(password_hash: bytes, password: bytes) -> bool:
  44. """
  45. Takes a modular crypt encoded stored password hash derived using one
  46. of the algorithms supported by `libsodium` and checks if the user provided
  47. password will hash to the same string when using the parameters saved
  48. in the stored hash
  49. """
  50. if password_hash.startswith(argon2id.STRPREFIX):
  51. return argon2id.verify(password_hash, password)
  52. elif password_hash.startswith(argon2i.STRPREFIX):
  53. return argon2id.verify(password_hash, password)
  54. elif scrypt.AVAILABLE and password_hash.startswith(scrypt.STRPREFIX):
  55. return scrypt.verify(password_hash, password)
  56. else:
  57. raise (
  58. CryptPrefixError(
  59. "given password_hash is not in a supported format"
  60. )
  61. )