title: Convert SHA1 Hash in PHP to JAVA Code
body: <p>Hey guys i have a Problem, i must convert PHP to Java...</p>
  
  <p>The Php Function creates a sha1 hash in raw format and encode it.</p>
  
  <pre><code>strtolower(urlencode(sha1("asfasfasdf", true)));
  </code></pre>
  
  <p>OUT: <code>%bep%c3%9cc%dc%e4%89%f6n%0cw%fb%a3%95%ba%d8%c9r%82</code></p>
  
  <p>In java i tried:</p>
  
  <pre><code>public static String buildIdentity(String identity) {
          try {
              return URLEncoder.encode(toSHA1(identity.getBytes())).toLowerCase();
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          }
          return null;
      }
  
      public static String toSHA1(byte[] convertme) throws UnsupportedEncodingException{
          MessageDigest md = null;
          try {
              md = MessageDigest.getInstance("SHA-1");
              md.update(convertme);
              byte[] res = md.digest();
              return new String(res);
          }
          catch(NoSuchAlgorithmException e) {
              e.printStackTrace();
          }
          return null;
      }
  
  
  System.out.println(Utils.buildIdentity("asfasfasdf"));
  </code></pre>
  
  <p>but the Output of this is: <code>%ef%bf%bdp%c3%9cc%ef%bf%bd%ef%bf%bd%ef%bf%bdn%0cw%ef%bf%bd%ef%bf%bd%ef%bf%bdr%ef%bf%bd</code></p>
  
  <p>please help me :(</p>
  
  <p><strong>Solution Found!</strong></p>
  
  <pre><code>public static String buildIdentity(String identity) {
      try {
          return URLEncoder.encode( new String(toSHA1(identity.getBytes("ISO-8859-1")), "ISO-8859-1"), "ISO-8859-1").toLowerCase();
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      }
      return null;
  }
  
  public static byte[] toSHA1(byte[] convertme){
      try {
          MessageDigest md = MessageDigest.getInstance("SHA-1");
          md.update(convertme);
          return md.digest();
      }
      catch(NoSuchAlgorithmException e) {
          e.printStackTrace();
      }
      return null;
  }
  </code></pre>