"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Building a Password Manager in Go: Part 2

Building a Password Manager in Go: Part 2

Published on 2024-11-01
Browse:146

Welcome back to our journey of building a password manager in Go! In this second installment, we'll explore the progress we've made since our initial commit. We've added new features, improved the code structure, and implemented testing. Let's dive in!

Evolving the Project Structure

One of the first changes you'll notice is the improved project structure. We've separated our code into multiple files and packages, following Go's best practices:

dost/
.
├── LICENSE
├── README.md
├── go.mod
├── go.sum
├── internal
│   ├── internal_test.go
│   └── passgen.go
└── main.go

This structure allows for better organization and maintainability as our project grows.

Enhanced Command-Line Interface

We've significantly improved our CLI, making it more flexible and user-friendly. Here's a snippet from our main.go:

func main() {
    generateCmd := flag.NewFlagSet("generate", flag.ExitOnError)
    flag.Parse()

    switch os.Args[1] {
    case "generate":
        internal.Generate(generateCmd)
    }
}

This setup allows for subcommands, currently supporting the generate command. Users can now interact with our tool like this:

go run main.go generate email/[email protected] 15

Customizable Password Generation

We've added options to customize password generation. Users can now specify password length and choose to exclude special characters:

func Generate(generateFlags *flag.FlagSet) {
    generateFlags.BoolVar(&noSymbols, "n", false, "Skip symbols while generating password")
    generateFlags.BoolVar(&copyToClipBoard, "c", false, "Copy to clipboard.")
    generateFlags.Parse(os.Args[2:])
    passwordLength := 25
    // ... (code to parse custom length)

    password, err := generatePassword(passwordLength, noSymbols)
    // ... (code to handle password output)
}

This function allows users to use flags like -n to exclude symbols and -c to copy the password to clipboard instead of displaying it.

Improved Password Generation Algorithm

We've refined our password generation function to handle the new customization options:

func generatePassword(length int, noSymbols bool) (string, error) {
    const (
        uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
        digits           = "0123456789"
        specialChars     = "!@#$%^&*()-_= []{}|;:'\",./?"
    )

    allChars := uppercaseLetters   lowercaseLetters   digits
    if !noSymbols {
        allChars  = specialChars
    }

    var password string
    for i := 0; i 


This function now respects the noSymbols flag, allowing for more flexible password generation.

Implementing Tests

We've taken a significant step towards ensuring the reliability of our code by implementing tests. Here's a snippet from our test file:

func TestPasswordLength(t *testing.T) {
    password, err := generatePassword(10, true)
    if err != nil {
        t.Errorf("Expected no error, got %v", err)
    } else {
        if len(password) != 10 {
            t.Errorf("Expected 10 character password, got %d", len(password))
        }
    }
}

func TestSpecialCharacter10K(t *testing.T) {
    splCharMissing := 0
    for i := 1; i  0 {
        t.Errorf("Special character was missing in %d / 10000 instances.", splCharMissing)
    }
}

These tests check for correct password length and the inclusion of special characters. Interestingly, our special character test revealed an area for improvement: in 10,000 generated passwords, 234 didn't contain special characters. This gives us a clear direction for our next refinement.

What's Next?

While we've made significant progress, there's still room for improvement:

  1. Refine the password generation algorithm to ensure consistent inclusion of special characters.
  2. Implement password storage functionality.
  3. Add encryption for stored passwords.
  4. Develop search and retrieval features.

Stay tuned for the next part of our series, where we'll tackle these challenges and continue to evolve our password manager!

Remember, the full source code is available on GitHub. Feel free to clone, fork, and contribute to the project. Your feedback and contributions are always welcome!

Happy coding, and stay secure! ??

Building a Password Manager in Go: Part 2 svemaraju / dost

dost command line password manager written in Go

dost

dost is a CLI password manager written in Go.

Inspired by (Pass)[https://www.passwordstore.org/]

Features

  • Generate random passwords of configurable length
  • Copy generated passwords to clipboard automatically
  • Skip using symbols

Usage

> go build -o dost main.go
Enter fullscreen modeExit fullscreen mode

Generating password:

> ./dost generate email/[email protected]
Generated Password: );XE,7-Dv?)Aa &

Generating password with specified length (default is 25):

> ./dost generate email/[email protected] 12
Generated Password: si

Copy generated password to clipboard without printing:

> ./dost generate -c email/[email protected] 
Copied to clipboard! ✅

Avoid symbols for generating passwords:

> ./dost generate -n email/[email protected] 
Generated Password: E2UST}^{Ac[Fb&D|cD%;Eij>H

Under development

  • Insert a new password manually
  • Show an existing password
  • List all entries
  • Password storage
  • GPG Key based encryption

License

MIT




View on GitHub


Release Statement This article is reproduced at: https://dev.to/svemaraju/building-a-password-manager-in-go-part-2-5adf?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3