Convert between String/Hex/Byte in Java for sha256 - 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 &gt; adr.txt
Step2: $ printf $( cat adr.txt | sed 's/[[:xdigit:]]\{2\}/\\x&amp;/g' ) &gt;adr.hex
Step3: $ openssl dgst -sha256 -binary &lt;adr.hex &gt;tmp_sha256.hex
Step4: $ openssl dgst -ripemd160  &lt;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);
String addressHex = toHex(address);
System.out.println("address hex: " + addressHex);
byte[] addressBytes = addressHex.getBytes(StandardCharsets.UTF_8); // step 2
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(addressBytes); // 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("result: " + bytesToHex(out)); // = 62ab42cba5d2632d1350fafb2587f5d2ece445d3
                                                  // should be 56379c7bcd6b41188854e74169f844e8676cf8b8


Output:

address: 1L88S26C5oyjL1gkXsBeYwHHjvGvCcidr9
address hex: 314c383853323643356f796a4c31676b58734265597748486a764776436369647239
result: 62ab42cba5d2632d1350fafb2587f5d2ece445d3


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. :/

// updated post
... still doesnt show the correct result :/

// update2:

byte[] address = ("1L88S26C5oyjL1gkXsBeYwHHjvGvCcidr9").getBytes();
System.out.println("address byte array: " + address);
String addressHex = bytesToHex(address);
System.out.println("address hex: " + addressHex);
byte[] addressBytes = addressHex.getBytes();
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(addressBytes);
RIPEMD160Digest digest2 = new RIPEMD160Digest();
digest2.update(hash, 0, hash.length);
byte[] out = new byte[20];
digest2.doFinal(out, 0);
System.out.println("result: " + bytesToHex(out));


Output

address byte array: [B@108c4c35
address hex: 314c383853323643356f796a4c31676b58734265597748486a764776436369647239
result: 62ab42cba5d2632d1350fafb2587f5d2ece445d3
I have not the Java solution, but I can indicate the error. The conversion to hex is apparently missing.  When I do the hash on the text file itself, I also get the wrong result:

$ printf 1L88S26C5oyjL1gkXsBeYwHHjvGvCcidr9 &gt; adr.txt
$ openssl dgst -sha256 -binary &lt;adr.txt &gt;tmp_sha256.hex
$ openssl dgst -ripemd160  &lt;tmp_sha256.hex
(stdin)= db151e871af66b1323893e3f527e22f7684718af


so the transformation into a hex byte array "addressHex" is for sure missing something.

Update: 

I can see the "bytesToHex()" function, was this the code from the link I provided in the other thread? If so, try to dump the output of your java into a file, and use hexdump to verify. So you can concentrate on these two lines (I made the sed command shorter, without "cat"):

$ printf 1L88S26C5oyjL1gkXsBeYwHHjvGvCcidr9 &gt; adr.txt
$ printf $( sed 's/[[:xdigit:]]\{2\}/\\x&amp;/g' adr.txt ) &gt;adr.hex
$ hexdump -C adr.hex 
00000000  31 4c 88 53 26 c5 6f 79  6a 4c 31 67 6b 58 73 be  |1L.S&amp;.oyjL1gkXs.|
00000010  59 77 48 48 6a 76 47 76  cc 69 64 72 39           |YwHHjvGv.idr9|
0000001d


Here you should receive the same result by your java code, then you'd be good to go :-)