base.go (1368B)
1 package passwordvalidator 2 3 import "strings" 4 5 const ( 6 replaceChars = `!@$&*` 7 sepChars = `_-., ` 8 otherSpecialChars = `"#%'()+/:;<=>?[\]^{|}~` 9 lowerChars = `abcdefghijklmnopqrstuvwxyz` 10 upperChars = `ABCDEFGHIJKLMNOPQRSTUVWXYZ` 11 digitsChars = `0123456789` 12 ) 13 14 func getBase(password string) int { 15 chars := map[rune]struct{}{} 16 for _, c := range password { 17 chars[c] = struct{}{} 18 } 19 20 hasReplace := false 21 hasSep := false 22 hasOtherSpecial := false 23 hasLower := false 24 hasUpper := false 25 hasDigits := false 26 base := 0 27 28 for c := range chars { 29 if strings.ContainsRune(replaceChars, c) { 30 hasReplace = true 31 continue 32 } 33 if strings.ContainsRune(sepChars, c) { 34 hasSep = true 35 continue 36 } 37 if strings.ContainsRune(otherSpecialChars, c) { 38 hasOtherSpecial = true 39 continue 40 } 41 if strings.ContainsRune(lowerChars, c) { 42 hasLower = true 43 continue 44 } 45 if strings.ContainsRune(upperChars, c) { 46 hasUpper = true 47 continue 48 } 49 if strings.ContainsRune(digitsChars, c) { 50 hasDigits = true 51 continue 52 } 53 base++ 54 } 55 56 if hasReplace { 57 base += len(replaceChars) 58 } 59 if hasSep { 60 base += len(sepChars) 61 } 62 if hasOtherSpecial { 63 base += len(otherSpecialChars) 64 } 65 if hasLower { 66 base += len(lowerChars) 67 } 68 if hasUpper { 69 base += len(upperChars) 70 } 71 if hasDigits { 72 base += len(digitsChars) 73 } 74 return base 75 }