updated parser
This commit is contained in:
1
main.go
1
main.go
@@ -29,6 +29,7 @@ func main() {
|
||||
// Fetch user input
|
||||
fmt.Print("Enter your password: ")
|
||||
fmt.Scanf("%s", &input)
|
||||
fmt.Println(input)
|
||||
|
||||
// Check users password
|
||||
message, err := password.Check(input, commonWords, commonPasswords)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Check checks if password is valid, and returns message, and if its a strong password
|
||||
@@ -23,30 +24,30 @@ func Check(input string, wordList, passwordList []string) (string, error) {
|
||||
|
||||
// Check if password contains a number and a special character and a capital letter
|
||||
for _, letter := range input {
|
||||
// Check ASCII code to see if has special character
|
||||
if letter >= 33 && letter <= 47 || letter >= 58 && letter <= 62 {
|
||||
// Check if letter is a symbol
|
||||
if !(unicode.IsLetter(letter) || unicode.IsDigit(letter) || unicode.IsSpace(letter)) {
|
||||
hasSpecialCharacter = true
|
||||
}
|
||||
|
||||
// Check ASCII code to see if password contains a number
|
||||
if letter >= 48 && letter <= 57 {
|
||||
// Check if letter is a number
|
||||
if unicode.IsDigit(letter) {
|
||||
hasNumber = true
|
||||
}
|
||||
|
||||
// Check ASCII code to see if password contains a capital letter
|
||||
if letter >= 65 && letter <= 90 {
|
||||
// Check if letter is a capital letter
|
||||
if unicode.IsUpper(letter) {
|
||||
hasCapitalLetter = true
|
||||
}
|
||||
|
||||
// Check ASCII code to see if there is a space
|
||||
if letter == 32 {
|
||||
return "Weak password", fmt.Errorf("password cannot contain a space")
|
||||
// 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(entry, input) {
|
||||
if strings.Contains(strings.ToLower(entry), strings.ToLower(input)) {
|
||||
return "Weak password", fmt.Errorf("password found inside common passwords")
|
||||
}
|
||||
}
|
||||
@@ -57,13 +58,13 @@ func Check(input string, wordList, passwordList []string) (string, error) {
|
||||
strippedPassword := re.ReplaceAllString(input, "")
|
||||
// Iterate through common words and see if input contains any
|
||||
for _, word := range wordList {
|
||||
if strings.Contains(word, strippedPassword) {
|
||||
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", nil
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user