You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.8 KiB

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

Powered by TurnKey Linux.