The thinking caused by the same or sequential characters in password verification. md

Keywords: Java ascii Junit

Thinking about password verification caused by the same or sequential characters

[TOC]

demand

Although users hate this kind of complex password verification, sometimes for the sake of security, our system has to force users to set a higher strength password.
For example: it is not allowed to have more than one character with the same or consecutive number in succession.

Reflection

For general verification, we usually use regular expressions, so I started from this aspect at first, but I still don't have an idea of implementation.
Later, I saw that someone used character array to judge whether it was continuous by subscript, and I saw someone talking about ASCII code by chance. It's a good implementation to combine the two.

Realization

The simple implementation is as follows:

import static org.junit.Assert.*;
import org.junit.Test;
public class MainTest {
    @Test
    public void test() {
        String password = "abcdd";
        boolean isRepeat = isRepeatChar(password, 1);
        boolean isOrder = isOrderChar(password, 3);
        assertEquals(true, isRepeat);
        assertEquals(true, isOrder);
    }
    /**
     * Whether there are consecutive i-bit sequential characters
     * @param str
     * @param i There are i-bit sequential characters, i should be greater than or equal to 1
     * @return Returns true if i-bit-ordered characters, false otherwise
     */
    private boolean isOrderChar(String str, int i) {
        char[] charArr = str.toCharArray();
        int len = charArr.length;
        int count = 0;
        int t = charArr[0];
        for (int j = 1; j < len; j++) {
            if ((t + 1) == charArr[j]) {
                count ++;
                if (count == i) {
                    return true;
                }
            } else {
                count = 0;
            }
            t = charArr[j];
        }
        return false;
    }
    /**
     * Does the string contain at least i characters that repeat the same
     * @param str
     * @param i i Digit repetition number, i should be greater than or equal to 1
     * @return Returns true if it contains i-bits that repeat the same characters, false otherwise
     */
    private boolean isRepeatChar(String str, int i) {
        char[] charArr = str.toCharArray();
        int len = charArr.length;
        int count = 0;
        int t = charArr[0];
        for (int j = 1; j < len; j++) {
            if (t == charArr[j]) {
                count ++;
                if (count == i) {
                    return true;
                }
            } else {
                t = charArr[j];
                count = 0;
            }
        }
        return false;
    }
}

summary

Implementation is very simple, but if you stick to regular expressions, you may not be able to do it for a long time, and obviously increase the difficulty, which is not a good way to achieve.
For 1-9, A-Z and A-Z, although they are limited enumerations, it's annoying to write them.
In the last sentence of the composition, we should shout slogans: think more, try more and summarize more.

Looking forward to your better sharing

Reference resources

Posted by lalabored on Wed, 25 Mar 2020 09:05:54 -0700