package password import ( "fmt" "regexp" "strings" "unicode" ) // Check checks if password is valid, and returns message, and if its a strong password func Check(input string, wordList, passwordList []string) (string, error) { // Variables to check state of password hasCapitalLetter := false hasSpecialCharacter := false isTwelveCharacters := false hasNumber := false // Check if password is shorter than 8 characters if len(input) < 2 { return "Weak password", fmt.Errorf("password cannot be shorter than 12 characters") } else { isTwelveCharacters = true } // Check if password contains a number and a special character and a capital letter for _, letter := range input { // Check if letter is a symbol if !(unicode.IsLetter(letter) || unicode.IsDigit(letter) || unicode.IsSpace(letter)) { hasSpecialCharacter = true } // Check if letter is a number if unicode.IsDigit(letter) { hasNumber = true } // Check if letter is a capital letter if unicode.IsUpper(letter) { hasCapitalLetter = true } // Check for spaces if unicode.IsSpace(letter) { return "Invalid password", fmt.Errorf("password cannot contain spaces") } } // Check if password is in common passwords for _, entry := range passwordList { if strings.Contains(strings.ToLower(entry), strings.ToLower(input)) { return "Weak password", fmt.Errorf("password found inside common passwords") } } // Check if password contains any common words re, _ := regexp.Compile(`[^a-zA-Z]+`) // Use regex to match anything thats not a letter // Replace all nonletter characters with blank space strippedPassword := re.ReplaceAllString(input, "") // Iterate through common words and see if input contains any for _, word := range wordList { if strings.Contains(strings.ToLower(word), strings.ToLower(strippedPassword)) { return "Weak password", fmt.Errorf("password contains common word from wordlist") } } if !(hasNumber && hasSpecialCharacter && isTwelveCharacters && hasCapitalLetter) { return "Weak password", fmt.Errorf("Has number: %v, Has special character: %v, Has 12+ characters: %v, Has capital letter: %v", hasNumber, hasSpecialCharacter, isTwelveCharacters, hasCapitalLetter) } return "Strong password", nil }