Advertisement
Guest User

Untitled

a guest
Feb 20th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. package com.armd.backbase.otp.domain;
  2.  
  3. import java.util.Collections;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class PasswordValidator {
  10.  
  11.     private static final int MIN_LENGTH = 6;
  12.  
  13.     private static String[] predefinedSequences = {
  14.             /**
  15.              * клавиатура ENG
  16.              */
  17.             "qwertyuiopasdfghjklzxcvbnm",
  18.             /**
  19.              * Алфавит ENG
  20.              */
  21.             "abcdefghijklmnopqrstuvwxyz",
  22.             /**
  23.              * Цифры
  24.              */
  25.             "1234567890123"
  26.     };
  27.     private final String password;
  28.     private List<String> errors = new LinkedList<>();
  29.  
  30.     public PasswordValidator(String password) {
  31.         if (password == null) {
  32.             throw new IllegalArgumentException("Пароль не может быть null");
  33.         }
  34.         this.password = password;
  35.     }
  36.  
  37.     public PasswordValidator checkLength() {
  38.         if (password.length() < MIN_LENGTH) {
  39.             errors.add("Пароль должен быть не короче " + MIN_LENGTH + " символов");
  40.         }
  41.         return this;
  42.     }
  43.  
  44.     public PasswordValidator checkGlobalPattern() {
  45.         Pattern regex = Pattern.compile("[\\w_\\$#!@%~\\^&\\*\\(\\)_\\+\\-\\{\\}=`\\[\\]:;<>\\./\\\\]+");
  46.  
  47.         if (!regex.matcher(password).matches()) {
  48.             errors.add("Пароль должен состоять из цифр и букв латинского алфавита, допускается использование специальных символов ~ ! @ # $ % ^ & * ( ) _ + ` - = { } [ ] : ; < > . / \\");
  49.         }
  50.         return this;
  51.     }
  52.  
  53.     private PasswordValidator checkPredefinedSequences() {
  54.         int coincidences;
  55.         String passwordLower = password.toLowerCase();
  56.         final int MAX_STRING_LENGHT_SEQUENCES = 4;
  57.         final String REPEATS_PATTERN = "(%s)|(%s)";
  58.  
  59.         for (int i = 0; i < predefinedSequences.length; ++i) {
  60.             String currentKeyboard = predefinedSequences[i];
  61.             for (int j = 0; j < currentKeyboard.length() - MAX_STRING_LENGHT_SEQUENCES + 1; ++j) {
  62.                 coincidences = 0;
  63.                 String matchWord = currentKeyboard.substring(j, j + MAX_STRING_LENGHT_SEQUENCES);
  64.                 String matchWorldReverse = new StringBuffer(matchWord).reverse().toString();
  65.                 Pattern pattern = Pattern.compile(String.format(REPEATS_PATTERN, matchWord, matchWorldReverse));
  66.                 Matcher matcher = pattern.matcher(passwordLower);
  67.                 while (matcher.find()) {
  68.                     if (++coincidences > 0) {
  69.                         errors.add("Пароль содержит недопустимую последовательность символов");
  70.                         return this;
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.         return this;
  76.     }
  77.  
  78.     public PasswordValidator checkAtleastOneLetter() {
  79.         Pattern regex = Pattern.compile(".*[a-zA-Z]+.*");
  80.         if (!regex.matcher(password).matches()) {
  81.             errors.add("Пароль должен содержать минимум одну букву латинского алфавита");
  82.         }
  83.         return this;
  84.     }
  85.  
  86.     public PasswordValidator checkAtleastOneDigit() {
  87.         Pattern regex = Pattern.compile(".*[0-9]+.*");
  88.         if (!regex.matcher(password).matches()) {
  89.             errors.add("Пароль должен содержать минимум одну цифру");
  90.         }
  91.         return this;
  92.     }
  93.  
  94.     public PasswordValidator checkTriplets() {
  95.         Pattern regex = Pattern.compile(".*(.)\\1\\1.*");
  96.         if (regex.matcher(password).matches()) {
  97.             errors.add("Пароль не должен содержать три одинаковых символа подряд");
  98.         }
  99.         return this;
  100.     }
  101.  
  102.     public void validate() {
  103.         checkLength().checkPredefinedSequences().checkGlobalPattern().checkAtleastOneLetter().checkAtleastOneDigit().checkTriplets();
  104.     }
  105.  
  106.     public boolean hasErrors() {
  107.         return !errors.isEmpty();
  108.     }
  109.  
  110.     public String getErrorString() {
  111.         StringBuilder sb = new StringBuilder();
  112.         for (String error : errors) {
  113.             sb.append(error);
  114.             sb.append("\n");
  115.         }
  116.         return sb.toString();
  117.     }
  118.  
  119.     public List<String> getErrors() {
  120.         return Collections.unmodifiableList(errors);
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement