简体   繁体   中英

generating you tor hostname from your tor private_key with python

I've been trying to generate my tor hostname from my private_key with python3. I've used this tutorial(java) and tried to change it to what i want to achieve, but i cant seem to generate my hostname. I suspect that the error lies somewhere in the slicing at the last line, but i cant find any documentation about it.

from Crypto.PublicKey import RSA
from base64 import b32encode as b32
import hashlib

privkey= open('privkey',"r")
#print(key.read())
key = RSA.importKey(privkey.read())
pubkey = key.publickey()
print(b32(hashlib.sha1(pubkey.exportKey()[22:).digest()[:10]))

also, my goal is to do it as easy understandable as possible, and this answer seems overly complicated to me.

aye I know this question is old but maybe my answer will benefit other people like me who were trying to do something similar and stumbled upon this question. I think I figured it out - I used some of your code and some of the code from PyShallot and it's working for me.

import hashlib
from hashlib import sha1
import rsa
from pyasn1.type import univ, namedtype
from pyasn1.codec.der import encoder
from base64 import b32encode
from Crypto.PublicKey import RSA

class RSAPublicKey(univ.Sequence):
    componentType = namedtype.NamedTypes(
            namedtype.NamedType('modulus', univ.Integer()),
            namedtype.NamedType('publicExponent', univ.Integer())
            )

pubkey, privkey = rsa.newkeys(1024)
privkey = privkey.save_pkcs1(format='PEM')
key = RSA.importKey(privkey)
n = key.n
e = key.e
public_key = RSAPublicKey()
public_key.setComponentByName('modulus', n)
public_key.setComponentByName('publicExponent', e)
pubkey = encoder.encode(public_key)
onion = b32encode(sha1(pubkey).digest())[:16].lower()+'.onion'
print str(onion)
print str(privkey)

The code above prints an onion address and the corresponding private key. PyShallot seems to work by calculating a public key and private key piece by piece instead of using an RSA library. I used the rsa module to generate a private key and the Crypto module to extract the individual RSA parameters, and then I used the code from PyShallot to calculate the public key. From there I could derive the onion address. It's really ugly and hacky, and there's definitely a better way to do it that's faster and doesn't use so many libraries. But it works.

You almost got it right, just needed to slice the digest at 16 bytes, then slice the hash at 16 characters:

key_hash = b32(hashlib.sha1(pubkey.exportKey()[22:]).digest()[:16])
print(key_hash[:16].lower() + ".onion")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM