GoLang 101 — (18) Generate Secure Password Checker Using Go
Yeah, Day 19
Concept will always be concept, it will never be improved until we build up a real world project. So this article, let me tell you about the ways for generating a secure password in Go.
A secure password will involves several considerations, such as using a mix of uppercase and lowercase letters, numbers, and special characters, as well as ensuring a sufficient length. Here’s an example in Go:
Why we need secure password?
Nowadays, everyone has social media, but are you sure that you already generating secure password. Well, let’s the computer program did the job to check the security of your password.
Okay let’s discuss about it.
Program
With the concept of If-Else, I will try to explain every details in generating this program.
The Package
First of all, I will do import two packagefmt
and unicode
. If you already following this tutorial from the beginning, you will see that I often usually use fmt
package, what is that? it’s a package to provides functions for formatting input and output. For unicode
it’s offers functionality for working with unicode characters and strings.
package main
import (
"fmt"
"unicode"
)
Function: passwordChecker
To simplify the program, I am generating functions, well it will explore more about this later, but let’s try to understand it first.
Part 1 — Variable Declaration
A secure password should contains combinations of number, word, special character, upper and lower case. This part, declare every variable that will be used in a secure password.
func passwordChecker(password string) string {
var (
hasUpperCase bool
hasLowerCase bool
hasDigit bool
hasSpecial bool
)
Part 2 — First If-Else
This is the place when the password check whether it’s meet the requirements or not.
for _, char := range password {
if unicode.IsUpper(char) {
hasUpperCase = true
} else if unicode.IsLower(char) {
hasLowerCase = true
} else if unicode.IsDigit(char) {
hasDigit = true
} else {
hasSpecial = true
}
}
Part 3 — Second If-Else
This is the place for every output based on every condition generated at the part 2 section.
if len(password) < 8 {
return "Password is too short. It should be at least 8 characters long"
} else if !hasUpperCase {
return "Password should contain at least one uppercase letter"
} else if !hasLowerCase {
return "Password should contain at least one lowercase letter"
} else if !hasDigit {
return "Password should contain at least one digit"
} else if !hasSpecial {
return "Password should contain at least one special character"
}
Part 4 — Closing the program
Well, if there is no problem with your password, it will give closing statements like this.
return "Password meets all the requirements."
}
Function: main
It’s the core of the entire program; without it, all the preceding code is useless. In fact, it’s only a few lines long. The program will prompt you to type your password, and then it will check whether the password meets the specified criteria or not.
func main() {
var password string
fmt.Print("Enter your password: ")
fmt.Scanln(&password)
result := passwordChecker(password)
fmt.Println(result)
}
Running The Program
If you already generating the code and running it. Try different input and the program will give you different output based on your if-else statements you declare before.
Conclusions
Well, this program checks whether a password meets the following criteria:
- It should be at least 8 characters long.
- It should contain at least one uppercase letter.
- It should contain at least one lowercase letter.
- It should contain at least one digit.
- It should contain at least one special character.
Each condition is checked using `if-else` statements, ensuring that the password meets all the requirements. Thanks for time.
func main() {
passwordLength := 12 // Change the length as per your requirement
password, err := generateSecurePassword(passwordLength)
if err != nil {
fmt.Println(“Error generating password:”, err)
return
}
fmt.Println(“Generated Password:”, password)
}
```
Explanation:
- The `generateSecurePassword` function takes an integer parameter `length` which specifies the desired length of the password to be generated.
- The `charset` constant contains all the characters that can be used in the password. You can modify this charset to suit your requirements.
- Inside the function, a byte slice of length `length` is created to store the password.
- A random number generator is used to select characters from the charset, and these characters are then appended to the password slice.
- Finally, the password slice is converted to a string and returned.
- In the `main` function, you can specify the desired length of the password to be generated.