MySQL MD5 e Java MD5 non uguali
-
06-07-2019 - |
Domanda
La prossima funzione in MySQL
MD5 ('secret')
genera 5ebe2294ecd0e0f08eab7690d2a6ee69
Vorrei avere una funzione Java per generare lo stesso output. Ma
public static String md5( String source ) {
try {
MessageDigest md = MessageDigest.getInstance( "MD5" );
byte[] bytes = md.digest( source.getBytes("UTF-8") );
return getString( bytes );
} catch( Exception e ) {
e.printStackTrace();
return null;
}
}
private static String getString( byte[] bytes ) {
StringBuffer sb = new StringBuffer();
for( int i=0; i<bytes.length; i++ ) {
byte b = bytes[ i ];
sb.append( ( int )( 0x00FF & b ) );
if( i+1 <bytes.length ) {
sb.append( "-" );
}
}
return sb.toString();
}
genera
94-190-34-148-236-208-224-240-142-171-118-144-210-166-238-105
Soluzione
Prova a codificare in base 16. Solo per iniziare ... 94 in base 16 è 5E.
** Modifica: ** Prova a cambiare il metodo getString:
private static String getString( byte[] bytes )
{
StringBuffer sb = new StringBuffer();
for( int i=0; i<bytes.length; i++ )
{
byte b = bytes[ i ];
String hex = Integer.toHexString((int) 0x00FF & b);
if (hex.length() == 1)
{
sb.append("0");
}
sb.append( hex );
}
return sb.toString();
}
Altri suggerimenti
sostituire
sb.append( ( int )( 0x00FF & b ) );
if( i+1 <bytes.length ) {
sb.append( "-" );
}
da
String hex = Integer.toHexString((int) 0x00FF & b);
if (hex.length == 1) sb.append("0");
sb.append( hex );
Questo può essere abbreviato in un one-liner usando le classi di utilità della libreria Apec Commons Codec ( http: / /commons.apache.org/codec )
String md = org.apache.commons.codec.digest.DigestUtils.md5hex("whatever");
Quei due sono uguali. Quello Java sembra essere in decimale. Converti in esadecimale.
Valuta di convertire i byte decimali in esadecimali. Ad esempio 94 base 10 è 5e base 16.
Questo perché la base è diversa. Il risultato MySQL MD5 è in base-16, mentre Java MD5 è in base-10.
Vorrei poterti aiutare ulteriormente, ma la mia matematica puzza. Un mio amico mi ha aiutato a generare un checksum in base 10 da un checksum in base 16 in PHP, ma ho perso la sceneggiatura. Spero che tu possa trovare la tua risposta in base a questo.
String password = org.springframework.util.DigestUtils.md5DigestAsHex("password".getBytes())
System.out.println(password)
Guarda come lo faccio, il codice è autoesplicabile!
Codice Java:
public static void main(String a[]) throws NoSuchAlgorithmException {
String passClear = "cleartext";
MessageDigest md5 = MessageDigest.getInstance("MD5"); // you can change it to SHA1 if needed!
md5.update(passClear.getBytes(), 0, passClear.length());
System.out.printf("MD5: %s: %s ", passClear, new BigInteger(1, md5.digest()).toString(16));
}
Output:
MD5: testo in chiaro: 5ab677ec767735cebd67407005786016
Query Mysql che produce lo stesso hash:
SELECT md5( 'cleartext' );
Output:
md5 ( 'cleartext')
5ab677ec767735cebd67407005786016
Invece di reinventare la ruota, prova il codec di Apache commons ( http://commons.apache.org/codec / ) che gestirà la codifica esadecimale con Hex.encodeHex (byte [])
private String encodeAsMD5(String password) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(password.getBytes());
return new String(Hex.encodeHex(bytes));
}
catch(Exception e) {
e.printStackTrace();
return null;
}
}
Utilizza le classi di utilità dalla libreria Codec di Apache Commons: http://commons.apache.org/codec/
String password = org.apache.commons.codec.digest.DigestUtils.md5Hex("password");
System.out.println(password);