Python Forum
How to compare in python an input value with an hashed value in mysql table?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to compare in python an input value with an hashed value in mysql table?
#1
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":

license_key
def50200352f5dc4bd8181a9daebbf4f9177fe725111a5a479d64636d01c2a10074e0c645abe898dea18210af563a5334288420551ab61c18ca4506cd03aa5d2bdd40933ddf7ca4d4b61b1c0f58a3830cbe0891cf4ff526311d5d637a55a574eca2c3a1b487b56

hash 9498cbf8bf00d6c55e31f98ba6d8294afa3127a84f31aa622c4158ac7377c6dd

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 topic To SHA512-hash a password in MySQL database by Python gave me a lot of information but didn't fix my issue.

Anyone has any odea how to proceed?

Thanks
Reply
#2
what hash function is used?
Although in the first place reasonable question is why store hash of the license key, not the license key itself?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks for trying to help me.

The license key is generated by the plugin "License manager for woocommerce":
https://wordpress.org/support/plugin/lic...ocommerce/

It looks like this in mysql table:
http://prntscr.com/qnejaj

I have no idea which hash function they use. I asked them on their support page.

I will have the same question for Wordpress user password. My python program will ask for login/password before to run the python code. I want to make this authentification process based on user's credentials stored in Wordpress database.
Reply
#4
If you have the license key in the DB why would you need to compare the hash, not the key itself?
Are you sure it's a hash of the key? It doesn't make sense to keep both the license key and it's hash in the DB.
Also hash may be hash of concatenation of several fields -e.g. license key + activation date + expiration date (just an example), so without knowing the hash function and what is hashed exactly it's difficult to calculate the hash

As to the passwords - it's not that simple question at all if it has to be done right
Maybe read https://crackstation.net/hashing-security.htm from gentle introduction
for example:
from hashlib import md5, sha256, sha512
key = 'def50200352f5dc4bd8181a9daebbf4f9177fe725111a5a479d64636d01c2a10074e0c645abe898dea18210af563a5334288420551ab61c18ca4506cd03aa5d2bdd40933ddf7ca4d4b61b1c0f58a3830cbe0891cf4ff526311d5d637a55a574eca2c3a1b487b56'

for hash_func in (md5, sha256, sha512):
    print(hash_func(key.encode()).hexdigest())
Output:
e39f98acdcd52c6a5804d4f58f6a5219 d44607349c1d7d99d827cb58930883f800c54b0aa3b631179cb455ef05df808e eed729e73dbcfc8ca05547a3e0c557b54d4051b823cb84a4a904505d6b57bbe5a3eae47f030f5b71342870d81ba1a5a18dd525b4b3a341d8bb7b38a1ce264768
that's the output of the md5, sha256 and sha512 hash functions applied on license key. Probably the hash function is sha256, but applied on something different from license key
Looking at the source code - there is file
license-manager-for-woocommerce.zip\license-manager-for-woocommerce\includes\Crypto.php
they use sha256, but if I get it right they apply it on some encrypted string.
Encryption is done using https://github.com/defuse/php-encryption which is in
license-manager-for-woocommerce.zip\license-manager-for-woocommerce\vendor\defuse\php-encryption\docs
There is extensive documentation for that library.
Given the above I don't want to dive further into this plugin. There is API for the plugin, whay don't you use it for your needs (e.g. validate a key)?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thank you so much for your help.
You gave me enough info for further investigation.
I may come back with an update. I will try to fix this issue myself without bothering you again.

Litle details,

the license key is goldQ3T8-1QRD-5QBI-9F22

and it is stored in database already encrypted.
License key is not saved in database with clear text. It is already encrypted. I am not sure what is this hash column for?

License key =>
def50200962018b6bbed50fc53abca6bb076eb2947fc379e69bd38dcea9f8fbe29eedd43f1148e721d5f6657d8d0152356f5a7ba566dde545a9a354c7b42af88fe4ea7775a4e2ee1a26d8b8f7e3272cf5a8bbe38197fdf19e1726d5e2d769bae408cd511706388abad5a75

hash =>
25138e045e9f50022331340a26d0eecbd0d7ca6bfefee0275749025c4f56c3a8
see screenshot:
http://prntscr.com/qnhz8h

I thought the "hash" column was the key to encrypt or decrypt the license stored in column "license_key".


So I run your code for testing with appropriate licese key:
from hashlib import md5, sha256, sha512

key = 'goldQ3T8-1QRD-5QBI-9F22'

for hash_func in (md5, sha256, sha512):
    print(hash_func(key.encode()).hexdigest())
and it give this outpu:
Output:
ecc58b55c33fe6dfe3b49d6d63aad65b f67e701240fbd964aa9a0eb81e2f549b8e3dd97e1aa3b1f5796fd12cd9b14005 8288f635fbab6d6511fc5aa63caf153fa434b3d351612cdf48dcf6abea4275cde5f0d6fffda2e7c6fd42350483603cf6959dd62c946eea2b75eca9f60a5cf5b7 Process finished with exit code 0
As you can see, here the code doesn't give same result than database.

=======================================

Regarding wordpress login, I didn't find any python library which can handle this authentification process.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql error message: Lost connection to MySQL server during query tomtom 6 1,166 Feb-09-2022, 09:55 AM
Last Post: ibreeden
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 2,904 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  Compare two Excel sheets with Python and list diffenrences dmkfon 1 3,268 Oct-09-2021, 03:30 PM
Last Post: Larz60+
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 1,121 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Python and MySql ogautier 8 1,604 May-20-2021, 11:10 PM
Last Post: Pedroski55
  Python and MySQL Pedroski55 5 1,694 Mar-23-2021, 06:40 AM
Last Post: ndc85430
  "'DataFrame' objects are mutable, thus they cannot be hashed" Mark17 1 4,312 Dec-25-2020, 02:31 AM
Last Post: tinh
  Creating table in MySQL db with decimal number issue dangermaus33 7 2,005 Nov-20-2020, 10:40 PM
Last Post: dangermaus33
  Fetch Oracle DB rows & print it in HTML file with table's col headers in table format tssr_2001 1 1,904 Sep-04-2020, 01:39 PM
Last Post: ibreeden
  Python MySQL ogautier 0 1,373 Sep-03-2020, 03:54 PM
Last Post: ogautier

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020