style: rename generatePassword function to RandomString
This commit is contained in:
parent
0cdfe3c615
commit
fb0e36b050
3 changed files with 28 additions and 28 deletions
29
gpm/cli.go
29
gpm/cli.go
|
@ -17,11 +17,9 @@ package gpm
|
||||||
import(
|
import(
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
|
||||||
"github.com/atotto/clipboard"
|
"github.com/atotto/clipboard"
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
"golang.org/x/crypto/ssh/terminal"
|
"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)
|
// error print a message and exit)
|
||||||
func (c *Cli) error(msg string) {
|
func (c *Cli) error(msg string) {
|
||||||
fmt.Printf("ERROR: %s\n", msg)
|
fmt.Printf("ERROR: %s\n", msg)
|
||||||
|
@ -213,7 +188,7 @@ func (c *Cli) addEntry() {
|
||||||
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)
|
||||||
if *RANDOM {
|
if *RANDOM {
|
||||||
entry.Password = c.generatePassword(c.Config.PasswordLength,
|
entry.Password = RandomString(c.Config.PasswordLength,
|
||||||
c.Config.PasswordLetter, c.Config.PasswordDigit, c.Config.PasswordSpecial)
|
c.Config.PasswordLetter, c.Config.PasswordDigit, c.Config.PasswordSpecial)
|
||||||
} else {
|
} else {
|
||||||
entry.Password = c.input("Enter the new password: ", entry.Password, false)
|
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.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)
|
||||||
if *RANDOM {
|
if *RANDOM {
|
||||||
entry.Password = c.generatePassword(c.Config.PasswordLength,
|
entry.Password = RandomString(c.Config.PasswordLength,
|
||||||
c.Config.PasswordLetter, c.Config.PasswordDigit, c.Config.PasswordSpecial)
|
c.Config.PasswordLetter, c.Config.PasswordDigit, c.Config.PasswordSpecial)
|
||||||
} else {
|
} else {
|
||||||
entry.Password = c.input("Enter the new password: ", entry.Password, false)
|
entry.Password = c.input("Enter the new password: ", entry.Password, false)
|
||||||
|
|
|
@ -21,6 +21,8 @@ import(
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"io"
|
"io"
|
||||||
|
mrand "math/rand"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/crypto/pbkdf2"
|
"golang.org/x/crypto/pbkdf2"
|
||||||
)
|
)
|
||||||
|
@ -78,3 +80,26 @@ func Decrypt(data string, passphrase string, salt string) ([]byte, error) {
|
||||||
|
|
||||||
return plaintext, nil
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ func Run() {
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else if *PASSWD {
|
} else if *PASSWD {
|
||||||
fmt.Println(cli.generatePassword(*LENGTH, *LETTER, *DIGIT, *SPECIAL))
|
fmt.Println(RandomString(*LENGTH, *LETTER, *DIGIT, *SPECIAL))
|
||||||
} else if *LIST {
|
} else if *LIST {
|
||||||
cli.listEntry()
|
cli.listEntry()
|
||||||
} else if *COPY {
|
} else if *COPY {
|
||||||
|
|
Loading…
Reference in a new issue