bcrypt Python Documentation

repository·main·Indexed 23 days ago

https://github.com/pyca/bcrypt

A Python implementation of the bcrypt password hashing function. Provides tools for password hashing and verification via hashpw() and checkpw(), salt generation with gensalt(), and a Key Derivation Function (KDF) via kdf() implementing bcrypt_pbkdf.

Tokens
1.1K
Snippets
6
Records
11
Agent score
31%

What's inside bcrypt

  1. Handle passwords longer than 72 bytes

    main

    Passing a password longer than 72 bytes to hashpw now raises a ValueError. To avoid this, a common pattern is to hash the password with a cryptographic hash (like sha256) and then base64 encode the digest before passing it to bcrypt.

    import bcrypt
    import hashlib
    import base64
    
    password = b"an incredibly long password" * 10
    hashed = bcrypt.hashpw(
        base64.b64encode(hashlib.sha256(password).digest()),
        bcrypt.gensalt()
    )
  2. Install system dependencies for bcrypt

    main

    On Linux, bcrypt requires a C compiler and a Rust compiler (minimum supported Rust version is 1.74.0). Use the following commands to install required dependencies based on your distribution:

    Debian and Ubuntu:

    $ sudo apt-get install build-essential cargo

    Fedora and RHEL-derivatives:

    $ sudo yum install gcc cargo

    Alpine:

    $ apk add --update musl-dev gcc cargo
  3. Use bcrypt as a KDF (bcrypt_pbkdf)

    main

    As of version 3.0.0, bcrypt provides a kdf function which implements bcrypt_pbkdf. This is used in OpenSSH's newer encrypted private key format.

    import bcrypt
    key = bcrypt.kdf(
        password=b'password',
        salt=b'salt',
        desired_key_bytes=32,
        rounds=100)
  4. Adjust the bcrypt prefix

    main
    To maintain compatibility with specific libraries, you can adjust the prefix by passing 2a or 2b (the default) to bcrypt.gensalt(prefix=b"...") as a bytes object. Note that the $2y$ prefix is still supported in hashpw but is deprecated.
  5. Hash and verify passwords with bcrypt

    main

    Use bcrypt.hashpw() to hash a password with a randomly-generated salt, and bcrypt.checkpw() to verify if an unhashed password matches a previously hashed one. Note that inputs must be bytes.

    import bcrypt
    password = b"super secret password"
    # Hash a password for the first time, with a randomly-generated salt
    hashed = bcrypt.hashpw(password, bcrypt.gensalt())
    # Check that an unhashed password matches one that has previously been
    # hashed
    if bcrypt.checkpw(password, hashed):
        print("It Matches!")
    else:
        print("It Does not Match :(")
  6. Adjust the bcrypt work factor (rounds)

    main

    You can adjust the logarithmic work factor by passing the desired number of rounds to bcrypt.gensalt(rounds=N). The default is 12.

    import bcrypt
    password = b"super secret password"
    # Hash a password for the first time, with a certain number of rounds
    hashed = bcrypt.hashpw(password, bcrypt.gensalt(14))
    # Check that an unhashed password matches one that has previously been
    # hashed
    if bcrypt.checkpw(password, hashed):
        print("It Matches!")
    else:
        print("It Does not Match :(")