style: rename generatePassword function to RandomString

This commit is contained in:
Adrien Waksberg 2019-07-20 17:12:21 +02:00
parent 0cdfe3c615
commit fb0e36b050
3 changed files with 28 additions and 28 deletions

View file

@ -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)

View file

@ -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)
}

View file

@ -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 {