title: Java's MessageDigest SHA1-algorithm returns different result than SHA1-function of php
body: <p>I have a SQL table with usernames and passwords. The passwords are encoded using MessageDigest's digest() method. If I encode a password - let's say "abcdef12" - with MessageDigest's digest() method and then convert it to hexadecimal values, the String is different than if I do the same using PHP's SHA1-method. I'd expect these values to be exactly the same though.</p>
  
  <p>Code that is used to encode the passwords:</p>
  
  <pre><code>MessageDigest md = MessageDigest.getInstance("SHA-1");
  byte[] passbyte;
  passbyte = "abcdef12".getBytes("UTF-8");
  passbyte = md.digest(passbyte);
  </code></pre>
  
  <p>The conversion of the String to hexadecimal is done using this method:</p>
  
  <pre><code>public static String convertStringToHex(String str) {
  
      char[] chars = str.toCharArray();
  
      StringBuffer hex = new StringBuffer();
      for (int i = 0; i &lt; chars.length; i++) {
          hex.append(Integer.toHexString((int) chars[i]));
      }
  
      return hex.toString();
  }
  </code></pre>
  
  <p>Password: <strong>abcdef12</strong></p>
  
  <p>Here's the password as returned by a lot of SHA1-hash online generators and PHP SHA1()-function: <strong>d253e3bd69ce1e7ce6074345fd5faa1a3c2e89ef</strong></p>
  
  <p>Here's the password as encoded by MessageDigest: <strong>d253e3bd69ce1e7ce674345fd5faa1a3c2e2030ef</strong></p>
  
  <p>Am I forgetting something?</p>
  
  <p>Igor.</p>
  
  <p>Edit: I've found someone with a similar problem: <a href="https://stackoverflow.com/questions/790232/c-sha-1-vs-php-sha-1-different-results">C# SHA-1 vs. PHP SHA-1...Different Results?</a> . The solution was to change encodings.. but I can't change encodings on the server-side since the passwords in that SQL-table are not created by my application.
  I use client-side SHA1-encoding using a JavaScript SHA1-class (more precisely: a Google Web Toolkit-class). It works and encodes the string as expected, but apparently using ASCII characters?..</p>