Skip to content

Instantly share code, notes, and snippets.

@RichardEllicott
Last active April 1, 2018 15:13
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 RichardEllicott/bb0fc57a90cb949377fc0a211c1c0e87 to your computer and use it in GitHub Desktop.
Save RichardEllicott/bb0fc57a90cb949377fc0a211c1c0e87 to your computer and use it in GitHub Desktop.
unusual hashing scheme, not designed for passwords but for sending client ID's publicly in shorter strings, adds random bytes to a truncated sha256
from __future__ import absolute_import, division, print_function
import hashlib
from Crypto import Random
def myhash(data, iv_length=8, hash_length=16):
'''
my hash scheme made to behave somewhat like bcrypt by having an IV
this is randomly generated and attached to the front of the hash, it is also injected into the sha256 hash
this ensures the hash is generated different
the hash is designed to obscure some operating information of my covert software where bcrypt would be the best choice but is too long
the default setting is 8 bytes of IV randomness, 16 bytes of a sha256, making a total of 24 bytes or 48 hex chars in a string
this is possibly the shortest hash that has a random output, another alternative would be using AES, encrypt 0000s with password
'''
print('hash complexity:', 2**(hash_length * 8))
print('hash random permutations:', 2**(iv_length * 8))
m = hashlib.sha256()
iv = Random.new().read(iv_length) # generate radom bytes to pass through hash before the data
m.update(iv)
m.update(data)
m = m.digest()
m = iv + m[0:hash_length]
print('generated myhash:', m.encode('hex'))
print('length = {} bytes, {} hex chars'.format(len(m), len(m) * 2))
return m
def myhash_checkpw(password, hashed_password, iv_length=8, hash_length=16):
m = hashlib.sha256()
iv = hashed_password[0:iv_length]
hashed_password = hashed_password[iv_length:]
m.update(iv)
m.update(password)
m = m.digest()[0:hash_length]
return hashed_password == m
pass
s = 'hashme'
s_hash = myhash(s)
print(myhash_checkpw(s, s_hash)) #confirm match
print(myhash_checkpw(s, 'd85fafd83a089773d6ab450b4529a1e7a06b49afebcc375b'.decode('hex'))) #and also this key i made earlier
@RichardEllicott
Copy link
Author

has a dependancy on pycrypto, this can easily be changed to raw python for no library dependancies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment