How to encrypt password with Python

0

I need to encrypt password using Python, from what I saw in PHP examples, they use a "module" that encrypts the password and that in theory does not to decrypt, to validate the password they encrypt the password that the user typed and compares with which it is encrypted in the database.

How to do this in Python? From what I managed to find on / tested, every time I put to encrypt the result and always different, so how do I compare? What do I need to learn?

Fictional example of what's happening to me:

First try:

msg = "1234"
encript(msg) >> adsadasfafsa

Second try:

msg = "1234"
encript(msg) >> weqwrewerwer

Concluding every time I encrypt the result is different, so I can not compare.

    
asked by anonymous 04.05.2018 / 20:20

1 answer

1

First, we need to install the bcrypt module, as it does not come by default. To do this, simply type the following command in the terminal:

sudo pip install bcrypt

Now we just use the module. I'll post an example usage.

import bcrypt

senha = '12345'
salt = bcrypt.gensalt(8)

print salt
$2a$08$K02Yy9Sn2mDReCeHwu3zse

hash = bcrypt.hashpw(senha, salt)

print hash
$2a$08$K02Yy9Sn2mDReCeHwu3zseMpikne058OpGqfMhKHhuDLIYrnvNT9G

Source: python blog - How to use bcrypt in python?

    
04.05.2018 / 20:31