Password Encryption Using Python

Password Encryption Using Python

Privacy? A significant term widely used in today's world. There has been an exponential increase in internet platforms, applications, and their popularity.

With this, lots of developers are building new projects and platforms. Since all of these require an authentication system, there is a strict requirement to have good password protection in place.

After all, you cannot just store the password provided by the user in raw form in the database. There has to be some sort of encryption before you store the password in the DB.

Does this sound complicated?

Don't worry. To simplify this process, I'll help you with a simple method to encrypt a password using Python.

Firstly let's start with the project setup:

  1. Install Python and pip in your system.
  2. Create a project directory.
  3. Create a file name "app.py" inside the directory.
  4. Open terminal at the directory and run-pip install bcrypt
  5. Import bcrypt in the "app.py" file. Bcrypt is the python package that will help us achieve encryption. You can read more about bcrypt at Bcrypt

Ok, so with the project setup done, let's start encrypting a password.

  1. Take a string that is a password of your choice. Ex - password = "youareSmart123"

  2. Next, we need to generate a string called "Salt." Salt is a fixed-length cryptographically-strong random value that is added to the input of hash functions to create unique hashes for every information. Salt is added to make a password hash output unique even for users adopting common passwords. An example of salt: b'$2b$12$Kh7S5S9FHT.WhzBa8tLZvO'

  3. We will be using the "bcrypt" package to generate salt. Use the below code to do it: salt = bcrypt.gensalt()

  4. Now, the final step is actually encrypting the password. To encrypt : passwordhashed=bcrypt.hashpw(password, salt)

  5. That's it. If we print "password hashed," we will get the encrypted output.

So, this was it for this blog. Hope you got to learn something reading this. Try out implementing the above method, comment down if you have any questions, and give feedback to the blog.

Did you find this article valuable?

Support Ayush Agarwal by becoming a sponsor. Any amount is appreciated!