Android Sha1哈希用户名和密码未正确散列

时间:2011-09-27 15:13:23

标签: java android mysql hash sha1

我有一个用户名和密码哈希的问题。我需要找到一种方法来使用java来散列数据,就像服务器使用mysql一样。这是一个mysql哈希:

我试图在Java中得到相同的结果:

        String userName_input = txtUserName.getText().toString();
        String password_input = txtPassword.getText().toString();

        try {
            String userName_hash;
            String password_hash;
            String username = SHA1.Sha1Hash(userName_input);
            String password = SHA1.Sha1Hash(password_input);
            String username1 = SHA1.Sha1Hash((username.concat(password)).substring(20, 35));
            String password1 = SHA1.Sha1Hash((password.concat(username)).substring(10, 35));    

            userName_hash = SHA1.Sha1Hash(username1.concat(username));   
            password_hash = SHA1.Sha1Hash(password1.concat(password));

    //THE RESULT WHICH SERVER RETURNS AND THE RESULT OF JAVA HASH       
UsernameHash (fcd86e8cc9fc7596f102de7b2b922e80c6e6fac9) : dd1ed59ebab6c420d750046539fcb60e7a1f9162
PasswordHash (b66936348bd0bd44fa44f5ca7dcceb909545e47f) : cde70a6416e061d00b4247b73c54c73fee7116ab

但是我得到的结果与mysql hash中的结果不一样。我正在使用这个SHA1代码:

public class SHA1 {
private static String convertToHex(byte[] data) { 
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < data.length; i++) { 
        int halfbyte = (data[i] >>> 4) & 0x0F;
        int two_halfs = 0;
        do { 
            if ((0 <= halfbyte) && (halfbyte <= 9)) 
                buf.append((char) ('0' + halfbyte));
            else 
                buf.append((char) ('a' + (halfbyte - 10)));
            halfbyte = data[i] & 0x0F;
        } while(two_halfs++ < 1);
    } 
    return buf.toString();
} 

public static String Sha1Hash(String text) 
throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
MessageDigest md;
md = MessageDigest.getInstance("SHA-1");
byte[] sha1hash = new byte[40];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
sha1hash = md.digest();
return convertToHex(sha1hash);
} 

}

任何想法/建议我的错误在哪里以及如何使哈希工作与mysql完全相同?

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

根据您提供的来源,这应该正常工作:

 String hashUser = SHA1.Sha1Hash(username);
            String hashPass = SHA1.Sha1Hash(password);

            /**
             * HASH USERNAME
             * sha1(concat(sha1(substr(concat(sha1('username'),sha1('password')),20,35)),sha1('username')))
             */
            String userPLUSpass = hashUser+hashPass;
            String userConcat = "";  
            String subStringUserHash = userConcat.concat(userPLUSpass);
            String userHashSubStr = SHA1.Sha1Hash(subStringUserHash.substring(19, 54));
            String luser = userHashSubStr+hashUser;
            String uConcat = "";            
            lastUser = SHA1.Sha1Hash(uConcat.concat(luser));

            /**
             * HASH PASSWORD
             * sha1(concat(sha1(substr(concat(sha1('password'),sha1('username')),10,35)),sha1('password')))
             */
            String passPLUSuser = hashPass+hashUser;
            String passConcat = "";  
            String subStringPassHash = passConcat.concat(passPLUSuser);
            String passHashSubStr = SHA1.Sha1Hash(subStringPassHash.substring(9, 44));
            String lpass = passHashSubStr+hashPass;
            String pConcat = "";            
            lastPass = SHA1.Sha1Hash(pConcat.concat(lpass));