Created
October 27, 2012 02:40
-
-
Save antitree/3962751 to your computer and use it in GitHub Desktop.
Python implementation of Tor's password hashing OpenPGP S2K algorithm
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
import os, binascii, hashlib | |
#supply password | |
secret = 'foo' | |
#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