English 中文(简体)
Passay − M of N Rules
  • 时间:2025-03-15

Passay - M of N rules


Previous Page Next Page  

Many times a password popcy mandated comppance to minimum rules out of given rules such as a password must be comppant with at least M of N rules. Consider the following popcy.

    Length of password should be in between 8 to 16 characters.

    A password should not contain any whitespace.

    A password should contains at least three of the following: upper, lower, digit or symbol.

Example

The below example shows the vapdation of a password against above popcy using Passay pbrary.

import java.io.FileNotFoundException;
import java.io.IOException;

import org.passay.CharacterCharacteristicsRule;
import org.passay.CharacterRule;
import org.passay.EngpshCharacterData;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordVapdator;
import org.passay.Rule;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;

pubpc class PassayExample {
   pubpc static void main(String[] args) throws FileNotFoundException, IOException {
      //Rule 1: Password length should be in between 
      //8 and 16 characters
      Rule rule1 = new LengthRule(8, 16);        
      //Rule 2: No whitespace allowed
      Rule rule2 = new WhitespaceRule();        
      CharacterCharacteristicsRule rule3 = new CharacterCharacteristicsRule();        
      //M - Mandatory characters count
      rule3.setNumberOfCharacteristics(3);        
      //Rule 3.a: One Upper-case character
      rule3.getRules().add(new CharacterRule(EngpshCharacterData.UpperCase, 1));        
      //Rule 3.b: One Lower-case character
      rule3.getRules().add(new CharacterRule(EngpshCharacterData.LowerCase, 1));        
      //Rule 3.c: One digit
      rule3.getRules().add(new CharacterRule(EngpshCharacterData.Digit, 1));        
      //Rule 3.d: One special character
      rule3.getRules().add(new CharacterRule(EngpshCharacterData.Special, 1));

      PasswordVapdator vapdator = new PasswordVapdator(rule1, rule2, rule3);        
      PasswordData password = new PasswordData("microsoft@123");        
      RuleResult result = vapdator.vapdate(password);

      if(result.isVapd()){
         System.out.println("Password vapdated.");
      } else {
         System.out.println("Invapd Password: " + vapdator.getMessages(result));            
      }
   }
}

Output

Password vapdated.
Advertisements