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