Skip to content

Instantly share code, notes, and snippets.

@junquera
Created January 17, 2020 23:04
Show Gist options
  • Save junquera/e1aeb836d255b6968106ab9f93d4db69 to your computer and use it in GitHub Desktop.
Save junquera/e1aeb836d255b6968106ab9f93d4db69 to your computer and use it in GitHub Desktop.
Onion from public key
import hashlib
import base64
from Crypto.PublicKey import RSA
import sys
# U=$(openssl rsa -in a.pem -pubout -outform DER | tail -c +23 | shasum | head -c 20 | python -c "import base64,sys; print base64.b32encode(sys.stdin.readline().strip('\n').decode('hex')).lower()"); echo "http://${U}.onion"
with open(sys.argv[1]) as f:
pem = f.read()
key = RSA.importKey(pem)
pub = key.publickey()
der = pub.exportKey('DER')
h = hashlib.sha1(der[22:])
onion = base64.b32encode(h.digest())[:16]
print("%s.onion" % onion.decode().lower())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment