Skip to content

Instantly share code, notes, and snippets.

@james-see
Created January 23, 2017 21:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save james-see/2f170fc17a328a638322078f42e04cbc to your computer and use it in GitHub Desktop.
Save james-see/2f170fc17a328a638322078f42e04cbc to your computer and use it in GitHub Desktop.
create a hashed password for control port setting in torrc HashedControlPassword line easily with this
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