title: MD5 Hash of ISO-8859-1 string in Java
body: <p>I'm implementing an interface for digital payment service called <a href="http://www.verkkomaksut.fi/index.php?id=24" rel="nofollow noreferrer">Suomen Verkkomaksut</a>. The information about the payment is sent to them via HTML form. To ensure that no one messes with the information during the transfer a MD5 hash is calculated at both ends with a special key that is not sent to them.</p>
  
  <p>My problem is that for some reason they seem to decide that the incoming data is encoded with ISO-8859-1 and not UTF-8. The hash that I sent to them is calculated with UTF-8 strings so it differs from the hash that they calculate.</p>
  
  <p>I tried this with following code:</p>
  
  <pre><code>String prehash = "6pKF4jkv97zmqBJ3ZL8gUw5DfT2NMQ|13466|123456||Testitilaus|EUR|http://www.esimerkki.fi/success|http://www.esimerkki.fi/cancel|http://www.esimerkki.fi/notify|5.1|fi_FI|0412345678|0412345678|esimerkki@esimerkki.fi|Matti|Meikäläinen||Testikatu 1|40500|Jyväskylä|FI|1|2|Tuote #101|101|1|10.00|22.00|0|1|Tuote #202|202|2|8.50|22.00|0|1";
  String prehashIso = new String(prehash.getBytes("ISO-8859-1"), "ISO-8859-1");
  
  String hash = Crypt.md5sum(prehash).toUpperCase(); 
  String hashIso = Crypt.md5sum(prehashIso).toUpperCase();
  </code></pre>
  
  <p>Unfortunately both hashes are identical with value C83CF67455AF10913D54252737F30E21. The correct value for this example case is 975816A41B9EB79B18B3B4526569640E according to Suomen Verkkomaksut's documentation.</p>
  
  <p>Is there a way to calculate MD5 hash in Java with ISO-8859-1 strings?</p>
  
  <p><strong>UPDATE:</strong> While waiting answer from Suomen Verkkomaksut, I found an alternative way to make the hash. Michael Borgwardt corrected my understanding of String and encodings and I looked for a way to make the hash from byte[].</p>
  
  <p>Apache Commons is an excellent source of libraries and I found their DigestUtils class which has a md5hex function which takes byte[] input and returns a 32 character hex string.</p>
  
  <p>For some reason this still doesn't work. Both of these return the same value:</p>
  
  <pre><code>DigestUtils.md5Hex(prehash.getBytes());
  DigestUtils.md5Hex(prehash.getBytes("ISO-8859-1"));
  </code></pre>