在python中为Tor隐藏服务生成private_key和主机名 [英] Generating private_key and hostname for Tor hidden service, in python

查看:114
本文介绍了在python中为Tor隐藏服务生成private_key和主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!/usr/bin/env python
import OpenSSL.crypto as crypto
import sha
import base64

KEY_BIT_LENGTH = 1024
ONION_LENGTH = 16
keys = crypto.PKey()
keys.generate_key(crypto.TYPE_RSA, KEY_BIT_LENGTH)

privkey_as_bytes = crypto.dump_privatekey(crypto.FILETYPE_ASN1, keys)
privkey_hash = sha.sha(privkey_as_bytes).digest()
onion = base64.b32encode(privkey_hash)[:ONION_LENGTH].lower() + '.onion'

print onion
print
print crypto.dump_privatekey(crypto.FILETYPE_PEM, keys)

我在这里做错了吗?我认为我的错误可能出在用于base32编码步骤的字母中. Shallot使用"abcdefghijklmnopqrstuvwxyz234567",但我完全不确定python使用什么,哈哈.

Am I doing something incorrect here? I think my error might be in the alphabet used for the base32 encoding step. Shallot uses "abcdefghijklmnopqrstuvwxyz234567", but I am totally unsure of what python uses, lol.

推荐答案

您的错误是使用私钥计算哈希值的,因此您应该使用公钥.

Your error was using the private key to calculate the hash, you should have used the public key.

crypto.dump_publickey(crypto.FILETYPE_ASN1, keys)的输出结构如下:

    SEQUENCE (2 elem)
        SEQUENCE (2 elem)
            OBJECT IDENTIFIER 1.2.840.113549.1.1.1 rsaEncryption (PKCS #1)
            NULL
        BIT STRING (1 elem)
            SEQUENCE(2 elem)
                INTEGER (1024 bit) 1669234523452435234253452...
                INTEGER 65537

但是Tor只使用最后一个序列,因此您必须从以下22个字节中分割:

but Tor only uses the last sequence, so you must slice from 22 bytes on:

pub_key = crypto.dump_publickey(crypto.FILETYPE_ASN1, keys)[22:]
key_hash = b32(hashlib.sha1(pub_key).digest()[:16])
print(key_hash[:16].lower() + ".onion")

这篇关于在python中为Tor隐藏服务生成private_key和主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆