DESCRIPTION.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. bcrypt
  2. ======
  3. .. image:: https://img.shields.io/pypi/v/bcrypt.svg
  4. :target: https://pypi.python.org/pypi/bcrypt/
  5. :alt: Latest Version
  6. .. image:: https://travis-ci.org/pyca/bcrypt.svg?branch=master
  7. :target: https://travis-ci.org/pyca/bcrypt
  8. Modern password hashing for your software and your servers
  9. Installation
  10. ============
  11. To install bcrypt, simply:
  12. .. code:: bash
  13. $ pip install bcrypt
  14. Note that bcrypt should build very easily on Linux provided you have a C compiler, headers for Python (if you're not using pypy), and headers for the libffi libraries available on your system.
  15. For Debian and Ubuntu, the following command will ensure that the required dependencies are installed:
  16. .. code:: bash
  17. $ sudo apt-get install build-essential libffi-dev python-dev
  18. For Fedora and RHEL-derivatives, the following command will ensure that the required dependencies are installed:
  19. .. code:: bash
  20. $ sudo yum install gcc libffi-devel python-devel
  21. Changelog
  22. =========
  23. 3.1.3
  24. -----
  25. * Fixed a compilation issue on Solaris.
  26. * Added a warning when using too few rounds with ``kdf``.
  27. 3.1.2
  28. -----
  29. * Fixed a compile issue affecting big endian platforms.
  30. * Fixed invalid escape sequence warnings on Python 3.6.
  31. * Fixed building in non-UTF8 environments on Python 2.
  32. 3.1.1
  33. -----
  34. * Resolved a ``UserWarning`` when used with ``cffi`` 1.8.3.
  35. 3.1.0
  36. -----
  37. * Added support for ``checkpw``, a convenience method for verifying a password.
  38. * Ensure that you get a ``$2y$`` hash when you input a ``$2y$`` salt.
  39. * Fixed a regression where ``$2a`` hashes were vulnerable to a wraparound bug.
  40. * Fixed compilation under Alpine Linux.
  41. 3.0.0
  42. -----
  43. * Switched the C backend to code obtained from the OpenBSD project rather than
  44. openwall.
  45. * Added support for ``bcrypt_pbkdf`` via the ``kdf`` function.
  46. 2.0.0
  47. -----
  48. * Added support for an adjustible prefix when calling ``gensalt``.
  49. * Switched to CFFI 1.0+
  50. Usage
  51. -----
  52. Password Hashing
  53. ~~~~~~~~~~~~~~~~
  54. Hashing and then later checking that a password matches the previous hashed
  55. password is very simple:
  56. .. code:: pycon
  57. >>> import bcrypt
  58. >>> password = b"super secret password"
  59. >>> # Hash a password for the first time, with a randomly-generated salt
  60. >>> hashed = bcrypt.hashpw(password, bcrypt.gensalt())
  61. >>> # Check that an unhashed password matches one that has previously been
  62. >>> # hashed
  63. >>> if bcrypt.checkpw(password, hashed):
  64. ... print("It Matches!")
  65. ... else:
  66. ... print("It Does not Match :(")
  67. KDF
  68. ~~~
  69. As of 3.0.0 ``bcrypt`` now offers a ``kdf`` function which does ``bcrypt_pbkdf``.
  70. This KDF is used in OpenSSH's newer encrypted private key format.
  71. .. code:: pycon
  72. >>> import bcrypt
  73. >>> key = bcrypt.kdf(
  74. ... password=b'password',
  75. ... salt=b'salt',
  76. ... desired_key_bytes=32,
  77. ... rounds=100)
  78. Adjustable Work Factor
  79. ~~~~~~~~~~~~~~~~~~~~~~
  80. One of bcrypt's features is an adjustable logarithmic work factor. To adjust
  81. the work factor merely pass the desired number of rounds to
  82. ``bcrypt.gensalt(rounds=12)`` which defaults to 12):
  83. .. code:: pycon
  84. >>> import bcrypt
  85. >>> password = b"super secret password"
  86. >>> # Hash a password for the first time, with a certain number of rounds
  87. >>> hashed = bcrypt.hashpw(password, bcrypt.gensalt(14))
  88. >>> # Check that a unhashed password matches one that has previously been
  89. >>> # hashed
  90. >>> if bcrypt.checkpw(password, hashed):
  91. ... print("It Matches!")
  92. ... else:
  93. ... print("It Does not Match :(")
  94. Adjustable Prefix
  95. ~~~~~~~~~~~~~~~~~
  96. Another one of bcrypt's features is an adjustable prefix to let you define what
  97. libraries you'll remain compatible with. To adjust this, pass either ``2a`` or
  98. ``2b`` (the default) to ``bcrypt.gensalt(prefix=b"2b")`` as a bytes object.
  99. As of 3.0.0 the ``$2y$`` prefix is still supported in ``hashpw`` but deprecated.
  100. Maximum Password Length
  101. ~~~~~~~~~~~~~~~~~~~~~~~
  102. The bcrypt algorithm only handles passwords up to 72 characters, any characters
  103. beyond that are ignored. To work around this, a common approach is to hash a
  104. password with a cryptographic hash (such as ``sha256``) and then base64
  105. encode it to prevent NULL byte problems before hashing the result with
  106. ``bcrypt``:
  107. .. code:: pycon
  108. >>> password = b"an incredibly long password" * 10
  109. >>> hashed = bcrypt.hashpw(
  110. ... base64.b64encode(hashlib.sha256(password).digest()),
  111. ... bcrypt.gensalt()
  112. ... )
  113. Compatibility
  114. -------------
  115. This library should be compatible with py-bcrypt and it will run on Python
  116. 2.6+, 3.3+, and PyPy 2.6+.
  117. C Code
  118. ------
  119. This library uses code from OpenBSD.
  120. Security
  121. --------
  122. ``bcrypt`` follows the `same security policy as cryptography`_, if you
  123. identify a vulnerability, we ask you to contact us privately.
  124. .. _`same security policy as cryptography`: https://cryptography.io/en/latest/security/