Java Code Examples for org.bouncycastle.crypto.digests.RIPEMD160Digest#getDigestSize()

The following examples show how to use org.bouncycastle.crypto.digests.RIPEMD160Digest#getDigestSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BtcAddress.java    From blockchain with Apache License 2.0 6 votes vote down vote up
/**
 * 根据公钥生成比特币钱包地址
 * @param publicKey
 * @return
 */
public static String getAddress(byte[] publicKey) {

	//1. 计算公钥的 SHA-256 哈希值
	byte[] sha256Bytes = Hash.sha3(publicKey);
	//2. 取上一步结果,计算 RIPEMD-160 哈希值
	RIPEMD160Digest digest = new RIPEMD160Digest();
	digest.update(sha256Bytes, 0, sha256Bytes.length);
	byte[] ripemd160Bytes = new byte[digest.getDigestSize()];
	digest.doFinal(ripemd160Bytes, 0);
	//3. 取上一步结果,前面加入地址版本号(主网版本号“0x00”)
	byte[] networkID = new BigInteger("00", 16).toByteArray();
	byte[] extendedRipemd160Bytes = ByteUtils.add(networkID, ripemd160Bytes);
	//4. 取上一步结果,计算 SHA-256 哈希值
	byte[] oneceSha256Bytes = Hash.sha3(extendedRipemd160Bytes);
	//5. 取上一步结果,再计算一下 SHA-256 哈希值
	byte[] twiceSha256Bytes = Hash.sha3(oneceSha256Bytes);
	//6. 取上一步结果的前4个字节(8位十六进制)
	byte[] checksum = new byte[4];
	System.arraycopy(twiceSha256Bytes, 0, checksum, 0, 4);
	//7. 把这4个字节加在第5步的结果后面,作为校验
	byte[] binaryAddressBytes = ByteUtils.add(extendedRipemd160Bytes, checksum);
	//8. 把结果用 Base58 编码算法进行一次编码
	return Base58.encode(binaryAddressBytes);
}
 
Example 2
Source File: EOSFormatter.java    From eosio-java with MIT License 5 votes vote down vote up
/**
 * Digesting input byte[] to RIPEMD160 format
 *
 * @param input - input byte[]
 * @return RIPEMD160 format
 */
@NotNull
private static byte[] digestRIPEMD160(@NotNull byte[] input) {
    RIPEMD160Digest digest = new RIPEMD160Digest();
    byte[] output = new byte[digest.getDigestSize()];
    digest.update(input, 0, input.length);
    digest.doFinal(output, 0);

    return output;
}
 
Example 3
Source File: BtcAddressUtils.java    From blockchain-java with Apache License 2.0 5 votes vote down vote up
/**
 * 计算公钥的 RIPEMD160 Hash值
 *
 * @param pubKey 公钥
 * @return ipeMD160Hash(sha256 ( pubkey))
 */
public static byte[] ripeMD160Hash(byte[] pubKey) {
    //1. 先对公钥做 sha256 处理
    byte[] shaHashedKey = DigestUtils.sha256(pubKey);
    RIPEMD160Digest ripemd160 = new RIPEMD160Digest();
    ripemd160.update(shaHashedKey, 0, shaHashedKey.length);
    byte[] output = new byte[ripemd160.getDigestSize()];
    ripemd160.doFinal(output, 0);
    return output;
}
 
Example 4
Source File: Main.java    From BitcoinWallet with MIT License 4 votes vote down vote up
/**
 * generate bitcoin privatekey, publickey and address.
 *
 * @param childPrivateKey
 */
private static void bitcoinAddress(ExtendedPrivateKey childPrivateKey) {
    // 获取比特币私钥
    String privateKey = childPrivateKey.getPrivateKey();
    // 加80前缀和01后缀
    String rk = "80" + privateKey + "01";
    // 生成校验和
    byte[] checksum = Sha256.sha256(hexStringToByteArray(rk));
    checksum = Sha256.sha256(checksum);
    // 取校验和前4位(32bits)
    String end = String.valueOf(Hex.encodeHex(checksum)).substring(0, 8);
    rk = rk + end;
    // 进行base58编码
    String privateK = Base58.base58Encode(hexStringToByteArray(rk));


    // 获取比特币公钥
    String publicKey = childPrivateKey.neuter().getPublicKey();
    // 对公钥进行一次sha256
    byte[] pk256 = hexStringToByteArray(publicKey);
    pk256 = Sha256.sha256(pk256);
    // 进行ripe160加密(20位)
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(pk256, 0, pk256.length);
    byte[] ripemd160Bytes = new byte[digest.getDigestSize()];
    digest.doFinal(ripemd160Bytes, 0);
    // 加00前缀(比特币主网)变成21位
    byte[] extendedRipemd160Bytes = hexStringToByteArray("00" + String.valueOf(Hex.encodeHex(ripemd160Bytes)));
    // 计算校验和
    checksum = Sha256.sha256(extendedRipemd160Bytes);
    checksum = Sha256.sha256(checksum);
    // 加校验和前4位,变成25位
    String pk = String.valueOf(Hex.encodeHex(extendedRipemd160Bytes)) + String.valueOf(Hex.encodeHex(checksum)).substring(0, 8);
    // base58加密
    String address = Base58.base58Encode(hexStringToByteArray(pk));

    System.out.println("bitcoin privateKey:" + privateK);
    System.out.println("bitcoin publicKey:" + publicKey);
    System.out.println("bitcoin address:" + address);

    generateSegwitAddress(address);
}