1package pwgen
2
3import (
4	"strings"
5)
6
7// containsAllClasses validates that the password contains at least one
8// character from each given character class. Can also contain other classes.
9func containsAllClasses(pw string, classes ...string) bool {
10CLASSES:
11	for _, class := range classes {
12		for _, ch := range class {
13			if strings.Contains(pw, string(ch)) {
14				continue CLASSES
15			}
16		}
17		return false
18	}
19	return true
20}
21
22// containsOnlyClasses validates that the password only contains characters
23// from the given classes. Must not satisfy all classes.
24func containsOnlyClasses(pw string, classes ...string) bool {
25	for _, c := range pw {
26		for _, class := range classes {
27			if !strings.Contains(class, string(c)) {
28				return false
29			}
30		}
31	}
32	return true
33}
34
35func containsMaxConsecutive(pw string, n int) bool {
36	last := ""
37	repCnt := 1
38	for _, r := range pw {
39		if last == string(r) {
40			repCnt++
41			if repCnt >= n {
42				return false
43			}
44		} else {
45			repCnt = 1
46		}
47		last = string(r)
48	}
49	return true
50}
51