Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Difference in PHP an Java SHA1 Hash

843811Nov 19 2008 — edited Jun 11 2009
Hello,

i wrote a Java app that uses SHA1 to compute hashes of users passwords witch are stored in a MySQL database. When i try to check PHP hashed user input against the stored value, they dont match.

Java class:
public class Hasher {
    
    private String type;
    
    public Hasher(String type) {
        this.type = type;
    }
    
    public String encode(String pw) {
        String hash = new String("");
        try {
            MessageDigest md = MessageDigest.getInstance(type);           
            byte[] textBytes = pw.getBytes("8859_1");
            md.update(textBytes);        
            for (byte b : md.digest()) {
                hash += Integer.toHexString(b & 0xff);
            }
        }
        catch (NoSuchAlgorithmException e) {            
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        return hash;
    }
}
Example:
String to hash: 1234
Result of class shown above: 7110eda4d09e62aa5e4a390b0a572acd2c220
Result of PHP sha1 function: 7110eda4d09e062aa5e4a390b0a572ac0d2c0220

obviously they dont match :) but why??
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 9 2009
Added on Nov 19 2008
3 comments
857 views