mirror of https://github.com/mitchell/selfpass.git
Modify create and update command flags; refactor GeneratePassword
This commit is contained in:
parent
5a42345c08
commit
33987c940c
|
@ -19,8 +19,8 @@ import (
|
|||
|
||||
func makeCreate(repo clitypes.ConfigRepo, initClient CredentialsClientInit) *cobra.Command {
|
||||
var length uint
|
||||
var numbers bool
|
||||
var specials bool
|
||||
var noNumbers bool
|
||||
var noSpecials bool
|
||||
|
||||
createCmd := &cobra.Command{
|
||||
Use: "create",
|
||||
|
@ -77,7 +77,7 @@ password.`,
|
|||
check(survey.AskOne(prompt, &newpass, nil))
|
||||
|
||||
if newpass {
|
||||
ci.Password = crypto.GeneratePassword(int(length), numbers, specials)
|
||||
ci.Password = crypto.GeneratePassword(int(length), !noNumbers, !noSpecials)
|
||||
|
||||
var copypass bool
|
||||
prompt = &survey.Confirm{Message: "Copy new pass to clipboard?", Default: true}
|
||||
|
@ -147,8 +147,8 @@ password.`,
|
|||
},
|
||||
}
|
||||
|
||||
createCmd.Flags().BoolVarP(&numbers, "numbers", "n", true, "use numbers in the generated password")
|
||||
createCmd.Flags().BoolVarP(&specials, "specials", "s", false, "use special characters in the generated password")
|
||||
createCmd.Flags().BoolVarP(&noNumbers, "no-numbers", "n", false, "do not use numbers in the generated password")
|
||||
createCmd.Flags().BoolVarP(&noSpecials, "no-specials", "s", false, "do not use special characters in the generated password")
|
||||
createCmd.Flags().UintVarP(&length, "length", "l", 32, "length of the generated password")
|
||||
|
||||
return createCmd
|
||||
|
|
|
@ -19,8 +19,8 @@ import (
|
|||
|
||||
func makeUpdate(repo clitypes.ConfigRepo, initClient CredentialsClientInit) *cobra.Command {
|
||||
var length uint
|
||||
var numbers bool
|
||||
var specials bool
|
||||
var noNumbers bool
|
||||
var noSpecials bool
|
||||
|
||||
updateCmd := &cobra.Command{
|
||||
Use: "update",
|
||||
|
@ -111,7 +111,7 @@ password.`,
|
|||
check(survey.AskOne(prompt, &randpass, nil))
|
||||
|
||||
if randpass {
|
||||
ci.Password = crypto.GeneratePassword(int(length), numbers, specials)
|
||||
ci.Password = crypto.GeneratePassword(int(length), !noNumbers, !noSpecials)
|
||||
|
||||
var copypass bool
|
||||
prompt = &survey.Confirm{Message: "Copy new pass to clipboard?", Default: true}
|
||||
|
@ -182,8 +182,8 @@ password.`,
|
|||
},
|
||||
}
|
||||
|
||||
updateCmd.Flags().BoolVarP(&numbers, "numbers", "n", true, "use numbers in the generated password")
|
||||
updateCmd.Flags().BoolVarP(&specials, "specials", "s", false, "use special characters in the generated password")
|
||||
updateCmd.Flags().BoolVarP(&noNumbers, "no-numbers", "n", false, "do not use numbers in the generated password")
|
||||
updateCmd.Flags().BoolVarP(&noSpecials, "no-specials", "s", false, "do not use special characters in the generated password")
|
||||
updateCmd.Flags().UintVarP(&length, "length", "l", 32, "length of the generated password")
|
||||
|
||||
return updateCmd
|
||||
|
|
|
@ -6,22 +6,6 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func GenerateKeyFromPassword(pass []byte) ([]byte, error) {
|
||||
if len(pass) < 8 {
|
||||
return nil, fmt.Errorf("master password must be at least 8 characters")
|
||||
}
|
||||
|
||||
for idx := 0; len(pass) < 32; idx++ {
|
||||
pass = append(pass, pass[idx])
|
||||
|
||||
if idx == len(pass) {
|
||||
idx = 0
|
||||
}
|
||||
}
|
||||
|
||||
return pass, nil
|
||||
}
|
||||
|
||||
func CombinePasswordAndKey(pass, key []byte) ([]byte, error) {
|
||||
if len(pass) < 8 {
|
||||
return nil, fmt.Errorf("master password must be at least 8 characters")
|
||||
|
@ -38,30 +22,25 @@ func CombinePasswordAndKey(pass, key []byte) ([]byte, error) {
|
|||
}
|
||||
|
||||
func GeneratePassword(length int, numbers, specials bool) string {
|
||||
const alphas = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
const alphanumerics = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
||||
const alphasAndSpecials = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()"
|
||||
const alphanumericsAndSpecials = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()"
|
||||
const alphaValues = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
const numberValues = "1234567890"
|
||||
const specialValues = "!@#$%^&*()_-+="
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
pass := make([]byte, length)
|
||||
|
||||
values := alphaValues
|
||||
|
||||
switch {
|
||||
case numbers && specials:
|
||||
for idx := 0; idx < length; idx++ {
|
||||
pass[idx] = alphanumericsAndSpecials[rand.Int63()%int64(len(alphanumericsAndSpecials))]
|
||||
}
|
||||
values += numberValues + specialValues
|
||||
case numbers:
|
||||
for idx := 0; idx < length; idx++ {
|
||||
pass[idx] = alphanumerics[rand.Int63()%int64(len(alphanumerics))]
|
||||
}
|
||||
values += numberValues
|
||||
case specials:
|
||||
for idx := 0; idx < length; idx++ {
|
||||
pass[idx] = alphasAndSpecials[rand.Int63()%int64(len(alphasAndSpecials))]
|
||||
}
|
||||
default:
|
||||
for idx := 0; idx < length; idx++ {
|
||||
pass[idx] = alphas[rand.Int63()%int64(len(alphas))]
|
||||
values += specialValues
|
||||
}
|
||||
|
||||
for idx := range pass {
|
||||
pass[idx] = values[rand.Int63()%int64(len(values))]
|
||||
}
|
||||
|
||||
return string(pass)
|
||||
|
|
Loading…
Reference in New Issue