import java.security.MessageDigest;
import java.util.UUID;
public class SaltSecurityTest {
private static String salt;
public static void main(String[] args) {
salt = salt();
String passwdMingW = "123456";
String passwdMiW = md5(passwdMingW+salt);
System.out.println(checkPW("5203",passwdMingW,passwdMiW));
}
private static String md5(String s){
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(s.getBytes("utf-8"));
return toHex(bytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String salt(){
UUID uuid = UUID.randomUUID();
String[] arr = uuid.toString().split("-");
return arr[0];
}
private static String toHex(byte[] bytes) {
final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
StringBuilder ret = new StringBuilder(bytes.length * 2);
for (int i=0; i<bytes.length; i++) {
ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
}
return ret.toString();
}
private static boolean checkPW(String userID,String passwdMingW,String passwdMiW){
String passwdMiWTemp = md5(passwdMingW+salt);
return passwdMiWTemp.equals(passwdMiW);
}
}
评论 (1 条评论)