feat: generate a random password

This commit is contained in:
Adrien Waksberg 2019-07-16 22:56:17 +02:00
parent dcf7a8f52b
commit 0e80f704bc
5 changed files with 63 additions and 6 deletions

View file

@ -7,6 +7,11 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/)
## Unreleased ## Unreleased
### Added
- Use go module to get this software
- Generate random password
## v1.0.0 - 2019-07-12 ## v1.0.0 - 2019-07-12
### Added ### Added

View file

@ -10,6 +10,7 @@ gpm is passwords manager write in go and use AES-256 to encrypt the wallets
- generate OTP code - generate OTP code
- copy your login, password or otp in clipboard - copy your login, password or otp in clipboard
- manage multiple wallets - manage multiple wallets
- generate random password
## Install ## Install

View file

@ -17,6 +17,7 @@ package gpm
import( import(
"bufio" "bufio"
"fmt" "fmt"
"math/rand"
"os" "os"
"strconv" "strconv"
"syscall" "syscall"
@ -65,6 +66,29 @@ func (c *Cli) printEntries(entries []Entry) {
} }
} }
// generate a random password
func (c *Cli) generatePassword(length int, letter bool, digit bool, special bool) string {
digits := "0123456789"
specials := "~=+%^*/()[]{}/!@#$?|"
letters := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
chars := ""
password := make([]byte, length)
if letter { chars = chars + letters }
if digit { chars = chars + digits }
if special { chars = chars + specials }
if !letter && !digit && !special {
chars = digits + letters
}
rand.Seed(time.Now().UnixNano())
for i := 0; i < length; i++ {
password[i] = chars[rand.Intn(len(chars))]
}
return string(password)
}
// error print a message and exit) // error print a message and exit)
func (c *Cli) error(msg string) { func (c *Cli) error(msg string) {
fmt.Println(msg) fmt.Println(msg)
@ -190,7 +214,12 @@ func (c *Cli) addEntry() {
entry.Group = c.input("Enter the group: ", "", true) entry.Group = c.input("Enter the group: ", "", true)
entry.URI = c.input("Enter the URI: ", "", true) entry.URI = c.input("Enter the URI: ", "", true)
entry.User = c.input("Enter the username: ", "", true) entry.User = c.input("Enter the username: ", "", true)
entry.Password = c.input("Enter the password: ", "", false) if *RANDOM {
entry.Password = c.generatePassword(c.Config.PasswordLength,
c.Config.PasswordLetter, c.Config.PasswordDigit, c.Config.PasswordSpecial)
} else {
entry.Password = c.input("Enter the new password: ", entry.Password, false)
}
entry.OTP = c.input("Enter the OTP key: ", "", false) entry.OTP = c.input("Enter the OTP key: ", "", false)
entry.Comment = c.input("Enter a comment: ", "", true) entry.Comment = c.input("Enter a comment: ", "", true)
@ -210,7 +239,12 @@ func (c *Cli) updateEntry() {
entry.Group = c.input("Enter the new group: ", entry.Group, true) entry.Group = c.input("Enter the new group: ", entry.Group, true)
entry.URI = c.input("Enter the new URI: ", entry.URI, true) entry.URI = c.input("Enter the new URI: ", entry.URI, true)
entry.User = c.input("Enter the new username: ", entry.User, true) entry.User = c.input("Enter the new username: ", entry.User, true)
entry.Password = c.input("Enter the new password: ", entry.Password, false) if *RANDOM {
entry.Password = c.generatePassword(c.Config.PasswordLength,
c.Config.PasswordLetter, c.Config.PasswordDigit, c.Config.PasswordSpecial)
} else {
entry.Password = c.input("Enter the new password: ", entry.Password, false)
}
entry.OTP = c.input("Enter the new OTP key: ", entry.OTP, false) entry.OTP = c.input("Enter the new OTP key: ", entry.OTP, false)
entry.Comment = c.input("Enter a new comment: ", entry.Comment, true) entry.Comment = c.input("Enter a new comment: ", entry.Comment, true)

View file

@ -25,8 +25,12 @@ import (
// Config struct contain the config // Config struct contain the config
type Config struct { type Config struct {
WalletDir string `json:"wallet_dir"` WalletDir string `json:"wallet_dir"`
WalletDefault string `json:"wallet_default"` WalletDefault string `json:"wallet_default"`
PasswordLength int `json:"password_length"`
PasswordLetter bool `json:"password_letter"`
PasswordDigit bool `json:"password_digit"`
PasswordSpecial bool `json:"password_special"`
} }
// Init the configuration // Init the configuration
@ -43,7 +47,11 @@ func (c *Config) Init() error {
} else { } else {
c.WalletDir = fmt.Sprintf("%s/.config/mpw", user.HomeDir) c.WalletDir = fmt.Sprintf("%s/.config/mpw", user.HomeDir)
} }
c.WalletDefault = "default" c.WalletDefault = "default"
c.PasswordLength = 16
c.PasswordLetter = true
c.PasswordDigit = true
c.PasswordSpecial = false
return nil return nil
} }

View file

@ -15,6 +15,7 @@
package gpm package gpm
import( import(
"fmt"
"flag" "flag"
"os" "os"
) )
@ -25,11 +26,17 @@ var(
UPDATE = flag.Bool("update", false, "update an entry") UPDATE = flag.Bool("update", false, "update an entry")
DELETE = flag.Bool("delete", false, "delete an entry") DELETE = flag.Bool("delete", false, "delete an entry")
LIST = flag.Bool("list", false, "list the entries in a wallet") LIST = flag.Bool("list", false, "list the entries in a wallet")
LENGTH = flag.Int("length", 16, "specify the password length")
COPY = flag.Bool("copy", false, "enter an copy mode for an entry") COPY = flag.Bool("copy", false, "enter an copy mode for an entry")
CONFIG = flag.String("config", "", "specify the config file") CONFIG = flag.String("config", "", "specify the config file")
GROUP = flag.String("group", "", "search the entries in this group ") GROUP = flag.String("group", "", "search the entries in this group ")
PATTERN = flag.String("pattern", "", "search the entries with this pattern")
WALLET = flag.String("wallet", "", "specify the wallet") WALLET = flag.String("wallet", "", "specify the wallet")
PATTERN = flag.String("pattern", "", "search the entries with this pattern")
RANDOM = flag.Bool("random", false, "generate a random password for a new entry or an update")
PASSWD = flag.Bool("password", false, "generate and print a random password")
DIGIT = flag.Bool("digit", false, "use digit to generate a random password")
LETTER = flag.Bool("letter", false, "use letter to generate a random password")
SPECIAL = flag.Bool("special", false, "use special chars to generate a random password")
HELP = flag.Bool("help", false, "print this help message") HELP = flag.Bool("help", false, "print this help message")
) )
@ -42,6 +49,8 @@ func Run() {
if *HELP { if *HELP {
flag.PrintDefaults() flag.PrintDefaults()
os.Exit(1) os.Exit(1)
} else if *PASSWD {
fmt.Println(cli.generatePassword(*LENGTH, *LETTER, *DIGIT, *SPECIAL))
} else if *LIST { } else if *LIST {
cli.listEntry() cli.listEntry()
} else if *COPY { } else if *COPY {