How to compare in python an input value with an hashed value in mysql table?
admin at formationgrowthhacking.com writes:
> I have a wordpress 5.3 websites which sell a software with license key.
> The license key is encrypted and stored in Mysql table. there are 2 columns "license" and "hash":
Why would you store *both* an encrypted key and a hash?
If you have the encrypted key in the database and the encryption key on your site (presumably in the Python code) and your site is hacked, all the license keys are potentially in the open.
And if your key is on the site you can as well encrypt the entered license key and compare it to the stored encrypted key.
> My python program get an input for user (the license key in string
> without any encrypton) and need to compare it with the official license
> key stored in Mysql database of our Wordpress website.
> I read a lot of hashlib python, functions and methods. But I didn't find
> anywhere how could I "hash" the string input typed by user with some
> hash values from the table, in order to compare both values (the input
> license and the license stored in mysql table).
This supposes Python 3:
Let's assume the entered key is in ASCII and stored in the variable 'key'.
from hashlib import sha256
key = bytes(key, 'ascii')
hash = sha256(key).hexdigest()
Now you can compare hash with the stored hash in the database. Of course this only works if that stored hash has been calculated in the same way from the same key.
On Python 2 (which you shouldn't use) you can leave out the "key = bytes(key, 'ascii')" part.
You can of course make it more sophisticated, for example by using a salt. Unless your keys are extremely valuable, I wouldn't bother with that.
Pieter van Oostrum
PGP key: [8DAE142BE17999C4]