diff --git a/go.mod b/go.mod index 247f544..4f8d169 100644 --- a/go.mod +++ b/go.mod @@ -1,22 +1,3 @@ module create go 1.25.0 - -require ( - github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect - github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect - github.com/charmbracelet/lipgloss v1.1.0 // indirect - github.com/charmbracelet/log v0.4.2 // indirect - github.com/charmbracelet/x/ansi v0.8.0 // indirect - github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect - github.com/charmbracelet/x/term v0.2.1 // indirect - github.com/go-logfmt/logfmt v0.6.0 // indirect - github.com/lucasb-eyer/go-colorful v1.2.0 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-runewidth v0.0.16 // indirect - github.com/muesli/termenv v0.16.0 // indirect - github.com/rivo/uniseg v0.4.7 // indirect - github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect - golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect - golang.org/x/sys v0.30.0 // indirect -) diff --git a/go.sum b/go.sum index 0eb40f3..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,34 +0,0 @@ -github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= -github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= -github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs= -github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk= -github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY= -github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30= -github.com/charmbracelet/log v0.4.2 h1:hYt8Qj6a8yLnvR+h7MwsJv/XvmBJXiueUcI3cIxsyig= -github.com/charmbracelet/log v0.4.2/go.mod h1:qifHGX/tc7eluv2R6pWIpyHDDrrb/AG71Pf2ysQu5nw= -github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2llXn7xE= -github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q= -github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8= -github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs= -github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ= -github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg= -github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= -github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= -github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= -github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= -github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc= -github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= -github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= -github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= -github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= -golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= -golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= -golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/main.go b/main.go index 08703b6..e0860eb 100644 --- a/main.go +++ b/main.go @@ -1,24 +1,41 @@ package main import ( + "create/password" "create/web" - - "github.com/charmbracelet/log" + "fmt" + "os" ) func main() { - log.Info("Welcome to Password Checker") + var input string // Users inputted password + + fmt.Println("Welcome to Password Checker") // Fetch common words commonWords, err := web.FetchCommonWords() if err != nil { - log.Fatal(err) + fmt.Printf("ERROR: %v\n", err) + os.Exit(1) } // Fetch common passwords commonPasswords, err := web.FetchCommonPasswords() if err != nil { - log.Fatal(err) + fmt.Printf("ERROR: %v\n", err) + os.Exit(1) } + // Fetch user input + fmt.Print("Enter your password: ") + fmt.Scanf("%s", &input) + + // Check users password + message, err := password.Check(input, commonWords, commonPasswords) + if err != nil { + fmt.Printf("ERROR: %v\n", err) + os.Exit(1) + } + fmt.Println(message) + } diff --git a/password/password.go b/password/password.go index d2a5be2..e07a557 100644 --- a/password/password.go +++ b/password/password.go @@ -2,17 +2,69 @@ package password import ( "fmt" + "regexp" + "strings" ) // 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) < 8 { - return "Weak password", fmt.Errorf("password cannot be 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 + // 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 { + hasSpecialCharacter = true + } + // Check ASCII code to see if password contains a number + if letter >= 48 && letter <= 57 { + hasNumber = true + } + + // Check ASCII code to see if password contains a capital letter + if letter >= 65 && letter <= 90 { + 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 if password is in common passwords + for _, entry := range passwordList { + if strings.Contains(entry, 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(word, strippedPassword) { + return "Weak password", fmt.Errorf("password contains common word from wordlist") + } + } + + if !(hasNumber && hasSpecialCharacter && isTwelveCharacters && hasCapitalLetter) { + return "Weak password", nil + } + + return "Strong password", nil }