2

Hello I am looking for a way to produce the same result as with MySQL's PASSWORD function in Java. There are a Implementation of MySQL’s PASSWORD() Function in Java code?

For example:

Password: 123 in MySQL PASSWORD ('123 ') -> *23AE809DDACAF96AF0FD78ED04B6A265E05AA257

I hope you can help me

0

4 Answers 4

4

According to the answer pointed to by @ypercube, MySQL PASSWORD() is just sha1, applied twice.

Using Apache Commons Codec:

public static String MySQLPassword(String plainText) throws UnsupportedEncodingException {
    byte[] utf8 = plainText.getBytes("UTF-8");
    return "*" + DigestUtils.shaHex(DigestUtils.sha(utf8)).toUpperCase();
}

EDIT: tested, added throws clause, uppercased and a prefix "*".


Working code (to replace shaHex() that was not working):

public static String MySQLPassword(String plainText)
                  throws UnsupportedEncodingException
{
    byte[] utf8 = plainText.getBytes("UTF-8");
    byte[] test = DigestUtils.sha(DigestUtils.sha(utf8));
    return "*" + convertToHex(test).toUpperCase();
}
Sign up to request clarification or add additional context in comments.

11 Comments

You are missing the leading *.
Sorry it doesn't work I get the following Exeption: 05-05 10:43:25.392: ERROR/AndroidRuntime(29427): java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Hex.encodeHexString
@user I don't know much about Android, but it seems like you might have to do the conversion to hex yourself. Just use sha instead of shaHex, and wrap it with your own function the converts byte[] to a hex String.
If I you use only "sha" I get: [B@4637d2e0 And I use then my own convert I get: PWHash: 40bd001563085fc35165329ea1ff5c5ecbdbbeef But it should be this: 23AE809DDACAF96AF0FD78ED04B6A265E05AA257 it's not the same
@user: Did you apply sha() twice?
|
2

Updated itsadok's answer a bit.

public static String MySQLPassword(String plainText) throws UnsupportedEncodingException {
        byte[] utf8 = plainText.getBytes("UTF-8");
        return "*" + DigestUtils.sha1Hex(DigestUtils.sha1(utf8)).toUpperCase();
    }

1 Comment

This one both works and doesn't use the depricated sha and/or shaHex functions
1

Here is an example where MessageDigest used, plus converting to HEX string. So, just use it.

http://www.anyexample.com/programming/java/java_simple_class_to_compute_md5_hash.xml

1 Comment

The methode private static String convertToHex(byte[] data) { ... } is perfecktly, when shaHex() doesn't work Thanks
1

This question is almost the same - replace "Java" with ".NET or MS-SQL-Server" :

simulating-mysqls-password-encryption-using-net-or-ms-sql

It will give you an idea of how to convert MySQL PASSWORD() function into Java.


Please note that PASSWORD() purpose is for MySQL internal use and other, more secure methods should be used for hashing and authentication in application code. Unless you are limited somehow to use this one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.