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

The following examples show how to use org.bouncycastle.crypto.digests.RIPEMD160Digest#update() . 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: SerializeUtils.java    From nuls-v2 with MIT License 5 votes vote down vote up
/**
 * Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.
 *
 * @param input 字节数组
 */
public static byte[] sha256hash160(byte[] input) {
    byte[] sha256 = Sha256Hash.hash(input);
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(sha256, 0, sha256.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);
    return out;
}
 
Example 5
Source File: Utils.java    From evt4j with MIT License 5 votes vote down vote up
private static byte[] ripemd160(byte[] data) {
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(data, 0, data.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);
    return out;
}
 
Example 6
Source File: Hash.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static byte[] sha256hash160(byte[] input) {
    byte[] sha256 = sha256(input);
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(sha256, 0, sha256.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);
    return out;
}
 
Example 7
Source File: Utils.java    From nuls with MIT License 5 votes vote down vote up
/**
 * Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.
 */
public static byte[] sha256hash160(byte[] input) {
    byte[] sha256 = Sha256Hash.hash(input);
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(sha256, 0, sha256.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);
    return out;
}
 
Example 8
Source File: Hash.java    From balzac with Apache License 2.0 5 votes vote down vote up
public static Hash ripemd160(byte[] bytes) {
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(bytes, 0, bytes.length);
    byte[] ripmemdHash = new byte[20];
    digest.doFinal(ripmemdHash, 0);
    return new Hash(ripmemdHash);
}
 
Example 9
Source File: Hash.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static byte[] sha256hash160(byte[] input) {
    byte[] sha256 = sha256(input);
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(sha256, 0, sha256.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);
    return out;
}
 
Example 10
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);
}