Golang Password Generator
Contents
I continue learning Go, so this time I created a password generator to generate secure passwords that exclude ambiguous characters
Features
- Generate multiple passwords at once.
- Customizable password length (minimum 8 characters).
- Excludes ambiguous characters (e.g.,
0,1,O,I,l,5,S,q,g). - Memory efficient
Implementation Details
To make the development easy and more maintainable creating a modern CLI tool I use spf13/cobra, Cobra framework and keep the dependencies at minimum with crypto/rand for cryptographically secure random number generation and strings.Builder for efficient string construction.
The code is available here https://github.com/jms/passwordGenerator
Build/Development (Linux)
the executable can be built using go, or for convenience use the makefile and run make
# get the dependencies
go mod tidy
# then
go build -o password-gen
# or
make
go build -o password-gen
# running the unit tests
make test
go test -v
=== RUN TestValidateInput
=== RUN TestValidateInput/Valid_input
=== RUN TestValidateInput/Minimum_length
=== RUN TestValidateInput/Invalid_count_(zero)
=== RUN TestValidateInput/Invalid_count_(negative)
=== RUN TestValidateInput/Invalid_length_(too_short)
--- PASS: TestValidateInput (0.00s)
--- PASS: TestValidateInput/Valid_input (0.00s)
--- PASS: TestValidateInput/Minimum_length (0.00s)
--- PASS: TestValidateInput/Invalid_count_(zero) (0.00s)
--- PASS: TestValidateInput/Invalid_count_(negative) (0.00s)
--- PASS: TestValidateInput/Invalid_length_(too_short) (0.00s)
=== RUN TestGeneratePassword
=== RUN TestGeneratePassword/Valid_length_16
=== RUN TestGeneratePassword/Valid_length_8_(min)
--- PASS: TestGeneratePassword (0.00s)
--- PASS: TestGeneratePassword/Valid_length_16 (0.00s)
--- PASS: TestGeneratePassword/Valid_length_8_(min) (0.00s)
=== RUN TestGeneratePasswords
--- PASS: TestGeneratePasswords (0.00s)
PASS
ok passwordGenerator 0.003sDemo
time ./password-gen -n 1000000
...
________________________________________________________
Executed in 3.57 secs fish external
usr time 2.66 secs 882.00 micros 2.65 secs
sys time 1.26 secs 7.00 micros 1.26 secs