Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for two way hashing in python?

Tags:

python

I want to allow users to validate their email address by clicking on a link. The link would look something like

http://www.example.com/verifyemail?id=some-random-string

When I am sending this email, I want to be able to easily generate this 'some-random-string' from row id of user, an integer. and when user clicks on this link, generate that integer back.

Only requirement is this 'some-random-string' should be as opaque and non-guessable to the user as possible.


Finally, this is what I settled on

def p3_encrypt_safe(plain, key):
    return base64.urlsafe_b64encode(p3_encrypt(plain, key))

used the nice crypto library from http://www.nightsong.com/phr/crypto/p3.py addition of base64 safe encoding is mine.

like image 672
Manish Avatar asked Sep 08 '26 14:09

Manish


1 Answers

Use encryption, that's exactly what it's designed for. Blowfish, AES, even DES3 if you don't need particularly high security.

Alternatively, you could compute an SHA-256 or SHA-512 (or whatever) hash of the email address and store it in a database along with the email address itself. That way you can just look up the email address using the hash as a key.

like image 165
David Z Avatar answered Sep 11 '26 05:09

David Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!