Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.armd.backbase.otp.domain;
- import java.util.Collections;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class PasswordValidator {
- private static final int MIN_LENGTH = 6;
- private static String[] predefinedSequences = {
- /**
- * клавиатура ENG
- */
- "qwertyuiopasdfghjklzxcvbnm",
- /**
- * Алфавит ENG
- */
- "abcdefghijklmnopqrstuvwxyz",
- /**
- * Цифры
- */
- "1234567890123"
- };
- private final String password;
- private List<String> errors = new LinkedList<>();
- public PasswordValidator(String password) {
- if (password == null) {
- throw new IllegalArgumentException("Пароль не может быть null");
- }
- this.password = password;
- }
- public PasswordValidator checkLength() {
- if (password.length() < MIN_LENGTH) {
- errors.add("Пароль должен быть не короче " + MIN_LENGTH + " символов");
- }
- return this;
- }
- public PasswordValidator checkGlobalPattern() {
- Pattern regex = Pattern.compile("[\\w_\\$#!@%~\\^&\\*\\(\\)_\\+\\-\\{\\}=`\\[\\]:;<>\\./\\\\]+");
- if (!regex.matcher(password).matches()) {
- errors.add("Пароль должен состоять из цифр и букв латинского алфавита, допускается использование специальных символов ~ ! @ # $ % ^ & * ( ) _ + ` - = { } [ ] : ; < > . / \\");
- }
- return this;
- }
- private PasswordValidator checkPredefinedSequences() {
- int coincidences;
- String passwordLower = password.toLowerCase();
- final int MAX_STRING_LENGHT_SEQUENCES = 4;
- final String REPEATS_PATTERN = "(%s)|(%s)";
- for (int i = 0; i < predefinedSequences.length; ++i) {
- String currentKeyboard = predefinedSequences[i];
- for (int j = 0; j < currentKeyboard.length() - MAX_STRING_LENGHT_SEQUENCES + 1; ++j) {
- coincidences = 0;
- String matchWord = currentKeyboard.substring(j, j + MAX_STRING_LENGHT_SEQUENCES);
- String matchWorldReverse = new StringBuffer(matchWord).reverse().toString();
- Pattern pattern = Pattern.compile(String.format(REPEATS_PATTERN, matchWord, matchWorldReverse));
- Matcher matcher = pattern.matcher(passwordLower);
- while (matcher.find()) {
- if (++coincidences > 0) {
- errors.add("Пароль содержит недопустимую последовательность символов");
- return this;
- }
- }
- }
- }
- return this;
- }
- public PasswordValidator checkAtleastOneLetter() {
- Pattern regex = Pattern.compile(".*[a-zA-Z]+.*");
- if (!regex.matcher(password).matches()) {
- errors.add("Пароль должен содержать минимум одну букву латинского алфавита");
- }
- return this;
- }
- public PasswordValidator checkAtleastOneDigit() {
- Pattern regex = Pattern.compile(".*[0-9]+.*");
- if (!regex.matcher(password).matches()) {
- errors.add("Пароль должен содержать минимум одну цифру");
- }
- return this;
- }
- public PasswordValidator checkTriplets() {
- Pattern regex = Pattern.compile(".*(.)\\1\\1.*");
- if (regex.matcher(password).matches()) {
- errors.add("Пароль не должен содержать три одинаковых символа подряд");
- }
- return this;
- }
- public void validate() {
- checkLength().checkPredefinedSequences().checkGlobalPattern().checkAtleastOneLetter().checkAtleastOneDigit().checkTriplets();
- }
- public boolean hasErrors() {
- return !errors.isEmpty();
- }
- public String getErrorString() {
- StringBuilder sb = new StringBuilder();
- for (String error : errors) {
- sb.append(error);
- sb.append("\n");
- }
- return sb.toString();
- }
- public List<String> getErrors() {
- return Collections.unmodifiableList(errors);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement