Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Python bcrypt – Hash a Password with bcrypt

Learn to use Python bcrypt module for hashing a plain text password into encrypted String. Also learn to match the supplied password with already stored encrypted password with bcrypt module. 1. Python bcrypt module Bcrypt algorithm was designed by Niels Provos and David Mazières, based on the Blowfish …

Lokesh Gupta

July 6, 2021

Python Modules
Java Encryption Decryption, Java Hashing, Python Basics
Python

Learn to use Python bcrypt module for hashing a plain text password into encrypted String. Also learn to match the supplied password with already stored encrypted password with bcrypt module.

1. Python bcrypt module

Bcrypt algorithm was designed by Niels Provos and David Mazières, based on the Blowfish cipher.

Bcrypt helps in preventing the brute-force search attacks by increasing the iteration count (rounds). The computation cost of the algorithm depends on parameterized rounds, so it can be increased as computers get faster. It uses a salt to protect against rainbow table attacks, as well.

1.1. Installing bcrypt module

Use pip install command to install bcrypt module.

# Latest version

pip install bcrypt

# Any specific version

pip install python-bcrypt==0.3.2

1.2. bcrypt Methods

Method Description
gensalt(rounds) Returns a randomly-generated salt.
Optional rounds parameter adjusts the work factor. Default value is 12.
hashpw(passwd, salt) Hash a password. With randomly generated salt and optionally given number of rounds.
checkpw(passwd, hashedPasswd) Check that a unhashed password matches the hashed password.

2. Python bcrypt Examples

Example 1: Python bcrypt example to hash a password

import bcrypt

passwd = b'user_password'

# Hash a password for the first time
hashed = bcrypt.hashpw(passwd, bcrypt.gensalt())

print ("Password hash is : " , hashed)

Program output.

Password hash is :  b'$2b$12$rt0asWjvT0IkAOfqlhKSau.f2UTMhMpGtlIYArco7MSKERkBhwioC'

Example 2: Python bcrypt example to match a password

import bcrypt

passwd = b'user_password'

# Hash a password for the first time
hashed = bcrypt.hashpw(passwd, bcrypt.gensalt(10))

# Match with already stored hashed password
matched = bcrypt.checkpw(passwd, hashed)

print ("Password match is : " , matched)

Program output.

Password match is :  True

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Python Flow Control

  • Python if…else
  • Python for Loop
  • Python while Loop
  • Python break
  • Python continue
  • Python pass

Python Datatypes

  • Python Integer
  • Python String
  • Python List
  • Python Tuple
  • Python Set
  • Python Dictionary
  • Python OrderedDict
  • Python Priority Queue

Python Modules

  • Python Bcrypt
  • Python Hashlib
  • Python Httplib2
  • Python JSON

Python Advanced Topics

  • Python CSV Files
  • Building a Recommendation System

Python Reference

  • Built-in Functions
  • String Functions

Table of Contents

  • 1. Python bcrypt module
      • 1.1. Installing bcrypt module
      • 1.2. bcrypt Methods
  • 2. Python bcrypt Examples
      • Example 1: Python bcrypt example to hash a password
      • Example 2: Python bcrypt example to match a password
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Python hashlib

Next

Python JSON – Read a JSON file

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap