From 33987c940c18e613bb7f809d23364a2cd4936187 Mon Sep 17 00:00:00 2001 From: mitchell Date: Wed, 17 Jul 2019 22:27:03 -0400 Subject: [PATCH] Modify create and update command flags; refactor GeneratePassword --- sp/commands/create.go | 10 +++++----- sp/commands/update.go | 10 +++++----- sp/crypto/helpers.go | 45 ++++++++++++------------------------------- 3 files changed, 22 insertions(+), 43 deletions(-) diff --git a/sp/commands/create.go b/sp/commands/create.go index 6cf2901..6a65887 100644 --- a/sp/commands/create.go +++ b/sp/commands/create.go @@ -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 diff --git a/sp/commands/update.go b/sp/commands/update.go index d2a5c34..f370e2e 100644 --- a/sp/commands/update.go +++ b/sp/commands/update.go @@ -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 diff --git a/sp/crypto/helpers.go b/sp/crypto/helpers.go index 5dee648..06a69e2 100644 --- a/sp/crypto/helpers.go +++ b/sp/crypto/helpers.go @@ -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)