DEV Community

Harry Douglas
Harry Douglas

Posted on

Building a Simple Password Generator in Go: From Idea to Release

Hello friends! 👋

Do you need a quick password in the terminal? I made PGen for that. It is a small CLI tool. It makes passwords fast. It is simple, but very useful. No more open browser for passwords. Just type pgen and done.

Why PGen?

We developers need passwords often. For databases, APIs, tests. I wanted a light tool for my work. PGen is that. It is simple and safe. It makes random passwords. You can choose length and types of letters.

The Technical Parts (Go Stuff)

PGen uses Go. Go is good for this.

  • crypto/rand for safe random numbers. No weak passwords.
  • os.Args for command line. First number is length (default 8). Second is options like "u" for big letters, "l" for small, "n" for numbers, "s" for symbols.
  • Go builds for many systems easy. I used GOOS and GOARCH for Linux, Windows, macOS.

The main code is in generate.go. It picks letters from a set. It uses random to choose. Here is a simple part:

func generatePassword(length int, options string) string {
    charset := ""
    if strings.Contains(options, "l") { charset += "abcdefghijklmnopqrstuvwxyz" }
    if strings.Contains(options, "u") { charset += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" }
    if strings.Contains(options, "n") { charset += "0123456789" }
    if strings.Contains(options, "s") { charset += "!@#$%^&*()_+-=[]{}|;:,.<>?" }

    password := make([]byte, length)
    for i := range password {
        password[i] = charset[rand.Intn(len(charset))]
    }
    return string(password)
}
Enter fullscreen mode Exit fullscreen mode

The real code uses crypto/rand for safety.

Install and Use

Start is easy. If you have Go:

go install github.com/hrrydgls/pgen@latest
Enter fullscreen mode Exit fullscreen mode

No Go? Download from releases.

  • Linux/macOS: sudo curl -L https://github.com/hrrydgls/pgen/releases/download/v1.0.0/pgen -o /usr/local/bin/pgen && sudo chmod +x /usr/local/bin/pgen
  • Windows: Download pgen-windows.exe and add to PATH.

Use:

pgen 20           # 20 letters with all
pgen 12 ul        # 12 letters, big and small
pgen              # 8 letters default
Enter fullscreen mode Exit fullscreen mode

Examples:

➜ pgen 20
j3T72Y869XDM}8U9?d)v

➜ pgen 20 u
YGRMKFFNSPQFJHPWVNGZ
Enter fullscreen mode Exit fullscreen mode

The Release

Making release was fun. I made tag v1.0.0. I used gh release create for binaries. Now anyone can download. No need to build.

End

PGen is simple, but strong. It helps a lot. Code is open on GitHub. Change it if you want.

What do you think? Do you have simple tools? Tell me! 🚀

Top comments (0)