import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class CheckPassword {
static String SALT = "this is a salt String";
public static final String KEY_MD5 = "MD5";
boolean checkPW(String username, String password, String passwordEncrypt) throws NoSuchAlgorithmException {
String passwordAddSalt = getPasswordEncrypt(username, password);
return passwordEncrypt.equals(passwordAddSalt);
}
private String getPasswordEncrypt(String username, String pwd) throws NoSuchAlgorithmException {
MessageDigest md = getEncryptMethod(username);
String salt = getSalt(username);
md.update((pwd + salt).getBytes());
BigInteger PasswordEncrypt = new BigInteger(md.digest());
return PasswordEncrypt.toString(16);
}
private String getSalt(String username) {
return username + SALT;
}
private MessageDigest getEncryptMethod(String username) throws NoSuchAlgorithmException {
return MessageDigest.getInstance(KEY_MD5);
}
}
评论