diff --git a/gpm/cli.go b/gpm/cli.go index 829878d..c229b2a 100644 --- a/gpm/cli.go +++ b/gpm/cli.go @@ -17,11 +17,9 @@ package gpm import( "bufio" "fmt" - "math/rand" "os" "strconv" "syscall" - "time" "github.com/atotto/clipboard" "github.com/olekukonko/tablewriter" "golang.org/x/crypto/ssh/terminal" @@ -65,29 +63,6 @@ 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) func (c *Cli) error(msg string) { fmt.Printf("ERROR: %s\n", msg) @@ -213,7 +188,7 @@ func (c *Cli) addEntry() { entry.URI = c.input("Enter the URI: ", "", true) entry.User = c.input("Enter the username: ", "", true) if *RANDOM { - entry.Password = c.generatePassword(c.Config.PasswordLength, + entry.Password = RandomString(c.Config.PasswordLength, c.Config.PasswordLetter, c.Config.PasswordDigit, c.Config.PasswordSpecial) } else { entry.Password = c.input("Enter the new password: ", entry.Password, false) @@ -238,7 +213,7 @@ func (c *Cli) updateEntry() { entry.URI = c.input("Enter the new URI: ", entry.URI, true) entry.User = c.input("Enter the new username: ", entry.User, true) if *RANDOM { - entry.Password = c.generatePassword(c.Config.PasswordLength, + entry.Password = RandomString(c.Config.PasswordLength, c.Config.PasswordLetter, c.Config.PasswordDigit, c.Config.PasswordSpecial) } else { entry.Password = c.input("Enter the new password: ", entry.Password, false) diff --git a/gpm/crypto.go b/gpm/crypto.go index 1df9429..6488594 100644 --- a/gpm/crypto.go +++ b/gpm/crypto.go @@ -21,6 +21,8 @@ import( "crypto/rand" "encoding/base64" "io" + mrand "math/rand" + "time" "golang.org/x/crypto/pbkdf2" ) @@ -78,3 +80,26 @@ func Decrypt(data string, passphrase string, salt string) ([]byte, error) { return plaintext, nil } + +// RandomString generate a random string +func RandomString(length int, letter bool, digit bool, special bool) string { + digits := "0123456789" + specials := "~=+%^*/()[]{}/!@#$?|" + letters := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + chars := "" + randomString := 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 + } + + mrand.Seed(time.Now().UnixNano()) + for i := 0; i < length; i++ { + randomString[i] = chars[mrand.Intn(len(chars))] + } + + return string(randomString) +} diff --git a/gpm/main.go b/gpm/main.go index 5f5dc18..b9b88c5 100644 --- a/gpm/main.go +++ b/gpm/main.go @@ -50,7 +50,7 @@ func Run() { flag.PrintDefaults() os.Exit(1) } else if *PASSWD { - fmt.Println(cli.generatePassword(*LENGTH, *LETTER, *DIGIT, *SPECIAL)) + fmt.Println(RandomString(*LENGTH, *LETTER, *DIGIT, *SPECIAL)) } else if *LIST { cli.listEntry() } else if *COPY {