title: Cannot output correct hash in Java. What is wrong?
body: <p>In my Android app I have a SHA256 hash which I must further hash with the RIPEMD160 message digest algorithm.</p>
  
  <p>I can output the correct sha256 and ripemd160 hash of any string, but when I try to hash the sha256 hash with ripemd160 I get a hash which is incorrect.</p>
  
  <p>According to online hash calculators, the SHA256 value of the string 'test'(all lowercase) is:</p>
  
  <pre><code>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
  </code></pre>
  
  <p>And the RIPEMD160 value of the string 'test' is:</p>
  
  <pre><code>5e52fee47e6b070565f74372468cdc699de89107
  </code></pre>
  
  <p>The value from hashing the resulting sha256 hash with ripemd160 according to online calcs is:</p>
  
  <pre><code>4efc1c36d3349189fb3486d2914f56e05d3e66f8
  </code></pre>
  
  <p>And the one my app gives me is:</p>
  
  <pre><code>cebaa98c19807134434d107b0d3e5692a516ea66
  </code></pre>
  
  <p>which is obviously wrong.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public static String toRIPEMD160(String in)
  {
      byte[] addr = in.getBytes();
      byte[] out = new byte[20];
      RIPEMD160Digest digest = new RIPEMD160Digest();
      byte[] sha256 = sha256(addr);
      digest.update(sha256,0,sha256.length);
      digest.doFinal(out,0);
      return getHexString(out);
  }
  
  public static byte[] sha256(byte[] data)
  {
      byte[] sha256 = new byte[32];
      try
      {
          sha256 = MessageDigest.getInstance("SHA-256").digest(data);
      }
      catch(NoSuchAlgorithmException e)
      {}
  
      return sha256;
  }
  </code></pre>
  
  <p>For the ripemd160 algorithm, you need bouncycastle and java.security.MessageDigest for sha256.</p>