【发布时间】:2018-04-14 15:47:40
【问题描述】:
我正在尝试生成从公钥生成的洋葱地址。
如果在previous post 中的代码中添加以下行,就在 privateKeyEncoded
之后String publicKeyEncoded = encoder.encodeToString(publicKey.getEncoded());
当我将 privateKeyEncoded 放入 /var/lib/tor/hidden_service/private_key 时,保存 publicKeyEncoded 并启动 tor 服务,创建一个新的洋葱地址。我正在尝试获取与 tor 服务相同的洋葱地址,以及从 publicKeyEncoded 创建的地址。使用此代码
import org.apache.commons.codec.binary.Base32;
import org.apache.commons.codec.binary.Base64;
//base64 string from the public key created
String publicKeyTest = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCMnFkJTMZ2ZxnqLwCiB/EWHjsHbnC+sKEIrGbyOTYiTl3LygsekAX6LhgcllscLUFqSKlMRB3jRB0GAPrIc73E/hTnmWBtF8NT8DhZzl06LZ1BtNjfON1pHm87STMAayiSaXPmSOwIqOA89aJPcA9m4v4IhtjYSFXmCAsE4RqoAwIDAQAB";
//the onion address the tor service gives when the private key is used
String onionAddressTest = "qqkhrc4men3fiqyl";
byte[] publicKeyDecoded = Base64.decodeBase64(publicKeyTest);
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
byte[] sha1hash = messageDigest.digest(publicKeyDecoded);
int numberOfCharacters = 10;
byte[] reducedHash = new byte[numberOfCharacters];
for(int i = 0; i < numberOfCharacters; i++) {
reducedHash[i] = sha1hash[i];
}
Base32 base32encoder = new Base32();
String onionAddress = base32encoder.encodeAsString(reducedHash).toLowerCase();
System.out.println(onionAddress); // but this gives "7j3iz4of464hje2e"
我尝试使用spongycastle 复制我的转换,但得到了相同的答案。这让我觉得我生成公钥的方式有问题,或者我从 base64 进行的初始转换有问题。
给定公钥 (publicKeyTest) 如何使用 java 获取洋葱地址 (onionAddressTest)?
【问题讨论】: