[Fix] Хеширование

This commit is contained in:
Tenebrius 2021-06-14 14:22:08 +05:00
parent 781ab27127
commit 8c259a7702
2 changed files with 4 additions and 9 deletions

View file

@ -4,8 +4,6 @@
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import pro.gravit.utils.helper.SecurityHelper; import pro.gravit.utils.helper.SecurityHelper;
import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -19,11 +17,8 @@ public class DigestPasswordVerifier extends PasswordVerifier {
public boolean check(String encryptedPassword, String password) { public boolean check(String encryptedPassword, String password) {
try { try {
MessageDigest digest = MessageDigest.getInstance(algo); MessageDigest digest = MessageDigest.getInstance(algo);
digest.update(password.getBytes(StandardCharsets.UTF_8)); byte[] bytes = SecurityHelper.fromHex(encryptedPassword);
byte[] bytes = digest.digest(); return Arrays.equals(bytes, digest.digest(password.getBytes(StandardCharsets.UTF_8)));
String myHash = DatatypeConverter
.printHexBinary(bytes);
return myHash.equalsIgnoreCase(encryptedPassword);
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
logger.error("Digest algorithm {} not supported", algo); logger.error("Digest algorithm {} not supported", algo);
return false; return false;

View file

@ -18,9 +18,9 @@ public class DoubleDigestPasswordVerifier extends PasswordVerifier {
public boolean check(String encryptedPassword, String password) { public boolean check(String encryptedPassword, String password) {
try { try {
MessageDigest digest = MessageDigest.getInstance(algo); MessageDigest digest = MessageDigest.getInstance(algo);
byte[] bytes = SecurityHelper.fromHex(encryptedPassword); byte[] bytes = SecurityHelper.fromHex(password);
byte[] firstDigest = digest.digest(bytes); byte[] firstDigest = digest.digest(bytes);
return Arrays.equals(password.getBytes(StandardCharsets.UTF_8), toHexMode ? digest.digest(SecurityHelper.toHex(firstDigest).getBytes(StandardCharsets.UTF_8)) : digest.digest(firstDigest)); return Arrays.equals(encryptedPassword.getBytes(StandardCharsets.UTF_8), toHexMode ? digest.digest(SecurityHelper.toHex(firstDigest).getBytes(StandardCharsets.UTF_8)) : digest.digest(firstDigest));
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
logger.error("Digest algorithm {} not supported", algo); logger.error("Digest algorithm {} not supported", algo);
return false; return false;