开发者

Android Sha1 Hash username and password not hashing correctly

I have a little problem with hashing a username and password.I need to find a way to hash the data with java as the server did with mysql.Here is a mysql hashing :

and I'm trying to get the same result in Java like this :

        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       
Username开发者_如何学GoHash (fcd86e8cc9fc7596f102de7b2b922e80c6e6fac9) : dd1ed59ebab6c420d750046539fcb60e7a1f9162
PasswordHash (b66936348bd0bd44fa44f5ca7dcceb909545e47f) : cde70a6416e061d00b4247b73c54c73fee7116ab

But the result that I get is not the same as in mysql hash.I'm using this SHA1 code :

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);
} 

}

Any ideas/suggestions where is my mistake and how to get the hashing work exactly as in mysql?

Thanks in advance!!!


Depending on the source that you give this should work properly :

 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));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜