Password Encryption Using Python
2 min read
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:
- Install Python and pip in your system.
- Create a project directory.
- Create a file name "app.py" inside the directory.
- Open terminal at the directory and run-
pip install bcrypt
- 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.
Take a string that is a password of your choice. Ex -
password = "youareSmart123"
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:
We will be using the "bcrypt" package to generate salt. Use the below code to do it:
salt = bcrypt.gensalt()
Now, the final step is actually encrypting the password. To encrypt :
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!