/
Maksim_Shchelochok
/
Course_Work
Обзор
Документация
Войти
/
Maksim_Shchelochok
/
Course_Work
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/util/PasswordHasher.java
35 строк
1 KB
Maksim
Написан класс Authentication для авторизации и регистрации
20 июн 2026, 15:20
20 июн 2026, 15:20
e14473d
Код
Авторство
О чём код?
package util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.regex.Pattern; public class PasswordHasher { private static final Pattern STRONG_PASSWORD = Pattern.compile( "^(?=.*[0-9])(?=.*[A-Z])(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>/?]).{8,}$"); public static boolean isStrong(String password) { if (password == null) return false; return STRONG_PASSWORD.matcher(password).matches(); } public static String hashPassword(String password) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hashBytes = digest.digest(password.getBytes(java.nio.charset.StandardCharsets.UTF_8)); // Перевод байтов в шестнадцатеричную строку. StringBuilder hexString = new StringBuilder(); for (byte b : hashBytes) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { hexString.append('0'); } hexString.append(hex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Ошибка хеширования: " + e.getMessage()); } } }