下一個函數在MySQLMySQL的MD5和Java MD5不等於
MD5( 'secret')
產生5ebe2294ecd0e0f08eab7690d2a6ee69
我想有一個Java函數生成相同的輸出。但
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();
}
生成基地16
94-190-34-148-236-208-224-240-142-171-118-144-210-166-238-105
@Randolpho:若B <0x10的,則需要墊0 – laalto 2009-06-23 17:33:21
優秀點;編輯爲包含@ mihi的檢查(這比IMO字節值檢查更好)。 – Randolpho 2009-06-23 17:37:08
你忘了()在hex.length() – 2009-06-23 17:45:03