Created
February 18, 2019 20:17
-
-
Save fnkr/8347eefb0009ec312e5865d4d9b09f37 to your computer and use it in GitHub Desktop.
Python implementation of HeidiSQL password hash functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original implementation is written in Pascal: | |
# https://github.com/HeidiSQL/HeidiSQL/blob/10.1/source/apphelpers.pas#L456-L506 | |
from itertools import zip_longest | |
from random import randint | |
def decode(hash): | |
def grouper(n, iterable, padvalue=None): | |
return zip_longest(*[iter(iterable)] * n, fillvalue=padvalue) | |
def bytehandler(x): | |
nr = int(''.join(x), 16) - int(hash[-1], 16) | |
if nr < 0: | |
nr += 255 | |
return chr(nr) | |
return ''.join(map(bytehandler, grouper(2, hash[:-1]))) | |
def encode(password): | |
hash = '' | |
salt = randint(1, 9) | |
for char in password: | |
nr = ord(char) + salt | |
if nr > 255: | |
nr = nr - 255 | |
hex = '%0.2X' % nr | |
if len(hex) == 1: | |
h = '0' + hex | |
hash = hash + hex | |
hash = hash + str(salt) | |
return hash | |
print(decode(encode('password'))) # 'password' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment