Convert between String/Hex/Byte - wrong format?

Convert between String/Hex/Byte - wrong format?

I am trying to convert a bitcoin address and have the following code from here (Calculate Segwit address from public address, 2nd answer):

Step1: $ printf 1L88S26C5oyjL1gkXsBeYwHHjvGvCcidr9 > adr.txt
Step2: $ printf $( cat adr.txt | sed 's/[[:xdigit:]]\{2\}/\\x&/g' ) >adr.hex
Step3: $ openssl dgst -sha256 -binary <adr.hex >tmp_sha256.hex
Step4: $ openssl dgst -ripemd160  <tmp_sha256.hex
## result should be: 56379c7bcd6b41188854e74169f844e8676cf8b8

Now I want to do this in Java. I currently have the following code. No matter what I try, I dont get the correct result. :(

    String address = "1L88S26C5oyjL1gkXsBeYwHHjvGvCcidr9"; // step 1
    System.out.println("address: " + address);
    byte[] addressHex = address.getBytes(StandardCharsets.UTF_8); // step 2
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    byte[] hash = digest.digest(addressHex); // step 3
    RIPEMD160Digest digest2 = new RIPEMD160Digest(); // steps 4
    digest2.update(hash, 0, hash.length);
    byte[] out = new byte[20];
    digest2.doFinal(out, 0);
    System.out.println(bytesToHex(out)); // = db151e871af66b1323893e3f527e22f7684718af
                                         // should be 56379c7bcd6b41188854e74169f844e8676cf8b8

Can someone help me? I think the problem is somewhere doing the conversion String/hex/byte ...? I tried really hard, but cannot find the correct way to do it.

I also tried to convert the address to hex and after that to bytes, but doesnt work neither. :/

http://ift.tt/2DxSGUm

Comments

Popular posts from this blog

Unable to send raw transaction: mandatory-script-verify-flag-failed

ETH To The Moon / Bank of England Cryptocurrency? / BTC Dominance / More (The Crypt0 Minute)

My blockchain.info wallet was hacked. How can I create a double send to foil the attacker?