Skip to content

Instantly share code, notes, and snippets.

@jishanshaikh4
Forked from james-see/torhasher.py
Created May 17, 2022 07:20
Show Gist options
  • Save jishanshaikh4/9c2c4f16924d42f86c77c0a6373639b3 to your computer and use it in GitHub Desktop.
Save jishanshaikh4/9c2c4f16924d42f86c77c0a6373639b3 to your computer and use it in GitHub Desktop.
create a hashed password for control port setting in torrc HashedControlPassword line easily with this (python2)
import os, binascii, hashlib
#supply password
secret = 'your password here you want to use' # all you need to change!
#static 'count' value later referenced as "c"
indicator = chr(96)
#used to generate salt
rng = os.urandom
#generate salt and append indicator value so that it
salt = "%s%s" % (rng(8), indicator)
#That's just the way it is. It's always prefixed with 16
prefix = '16:'
# swap variables just so I can make it look exactly like the RFC example
c = ord(salt[8])
# generate an even number that can be divided in subsequent sections. (Thanks Roman)
EXPBIAS = 6
count = (16+(c&15)) << ((c>>4) + EXPBIAS) #
d = hashlib.sha1()
#take the salt and append the password
tmp = salt[:8]+secret
#hash the salty password as many times as the length of
# the password divides into the count value
slen = len(tmp)
while count:
if count > slen:
d.update(tmp)
count -= slen
else:
d.update(tmp[:count])
count = 0
hashed = d.digest()
#Convert to hex
salt = binascii.b2a_hex(salt[:8]).upper()
indicator = binascii.b2a_hex(indicator)
torhash = binascii.b2a_hex(hashed).upper()
#Put it all together into the proprietary Tor format.
print(prefix + salt + indicator + torhash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment