title: Java SHA1 output not the same as Linux's sha1sum command
body: <p>I've tried the following code to produce a SHA1 digest of a String:</p>
  
  <pre><code>import java.io.UnsupportedEncodingException;
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  import java.util.Formatter;
  
  public class SHA1 {
      private static String encryptPassword(String password)
      {
          String sha1 = "";
          try
          {
              MessageDigest crypt = MessageDigest.getInstance("SHA-1");
              crypt.reset();
              crypt.update(password.getBytes("UTF-8"));
              sha1 = byteToHex(crypt.digest());
          }
          catch(NoSuchAlgorithmException e)
          {
              e.printStackTrace();
          }
          catch(UnsupportedEncodingException e)
          {
              e.printStackTrace();
          }
          return sha1;
      }
  
      private static String byteToHex(final byte[] hash)
      {
          Formatter formatter = new Formatter();
          for (byte b : hash)
          {
              formatter.format("%02x", b);
          }
          String result = formatter.toString();
          formatter.close();
          return result;
      }
  
      public static void main(String args[]){
          System.out.println(SHA1.encryptPassword("test"));
  
      }
  }
  </code></pre>
  
  <p>This code was based on <a href="https://stackoverflow.com/questions/4895523/java-string-to-sha1">this question</a> and <a href="https://stackoverflow.com/questions/4400774/java-calculate-a-sha1-of-a-string">this other question</a>. Note that this is not a duplicate of those questions since they are about formatting the output.</p>
  
  <p>The problem is that it produces a different result than running the same input string through <code>sha1sum</code> command in Linux -> <code>echo test|sha1sum</code>.</p>
  
  <p>Java code output for "test" -> <code>a94a8fe5ccb19ba61c4c0873d391e987982fbbd3</code>
  sha1sum in linux terminal for "test" -> <code>4e1243bd22c66e76c2ba9eddc1f91394e57f9f83</code></p>
  
  <ul>
  <li>Why aren't they the same ? </li>
  <li>Don't Java's <code>MessageDigest</code> class and Linux's <code>sha1sum</code> utility implement the same algorithm ?</li>
  </ul>