password entity

Signed-off-by: Chenx221 <chenx221@yandex.com>
This commit is contained in:
Chenx221 2023-06-08 12:03:24 +08:00
parent d233fdedbe
commit f0b471db37
2 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package cyou.chenx221.pojo;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Password {
private String password;
private String encryptedPassword;
public Password() {
}
public Password(String password) throws NoSuchAlgorithmException {
encryptedPassword = encryptPassword(password);
}
private String encryptPassword(String password) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(password.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
public String getEncryptedPassword() {
return encryptedPassword;
}
}