Skip to content

Instantly share code, notes, and snippets.

@fnkr
Created February 18, 2019 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fnkr/8347eefb0009ec312e5865d4d9b09f37 to your computer and use it in GitHub Desktop.
Save fnkr/8347eefb0009ec312e5865d4d9b09f37 to your computer and use it in GitHub Desktop.
Python implementation of HeidiSQL password hash functions.
# 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