1
0
mirror of https://github.com/Luzifer/password.git synced 2024-09-19 18:32:57 +00:00

Add commandline flag to generate multiple passwords

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-10-31 12:57:19 +01:00
parent c73955d8af
commit f9c435af1a
Signed by: luzifer
GPG Key ID: DC2729FDD34BE99E
2 changed files with 29 additions and 23 deletions

View File

@ -19,6 +19,7 @@ func getCmdGet() *cobra.Command {
cmd.Flags().BoolVarP(&flags.CLI.JSON, "json", "j", false, "return output in JSON format") cmd.Flags().BoolVarP(&flags.CLI.JSON, "json", "j", false, "return output in JSON format")
cmd.Flags().IntVarP(&flags.CLI.Length, "length", "l", 20, "length of the generated password") cmd.Flags().IntVarP(&flags.CLI.Length, "length", "l", 20, "length of the generated password")
cmd.Flags().IntVarP(&flags.CLI.Num, "number", "n", 1, "number of passwords to generate")
cmd.Flags().BoolVarP(&flags.CLI.SpecialCharacters, "special", "s", false, "use special characters in your password") cmd.Flags().BoolVarP(&flags.CLI.SpecialCharacters, "special", "s", false, "use special characters in your password")
cmd.Flags().BoolVarP(&flags.CLI.XKCD, "xkcd", "x", false, "use XKCD style password") cmd.Flags().BoolVarP(&flags.CLI.XKCD, "xkcd", "x", false, "use XKCD style password")
@ -33,32 +34,36 @@ func actionCmdGet(cmd *cobra.Command, args []string) {
err error err error
) )
if flags.CLI.XKCD { for i := 0; i < flags.CLI.Num; i++ {
password, err = pwd.DefaultXKCD.GeneratePassword(flags.CLI.Length, flags.CLI.PrependDate)
} else {
password, err = pwd.NewSecurePassword().GeneratePassword(flags.CLI.Length, flags.CLI.SpecialCharacters)
}
if err != nil { if flags.CLI.XKCD {
switch { password, err = pwd.DefaultXKCD.GeneratePassword(flags.CLI.Length, flags.CLI.PrependDate)
case err == pwd.ErrLengthTooLow: } else {
fmt.Println("The password has to be more than 4 characters long to meet the security considerations") password, err = pwd.NewSecurePassword().GeneratePassword(flags.CLI.Length, flags.CLI.SpecialCharacters)
default:
fmt.Println("An unknown error occured")
} }
os.Exit(1)
}
if !flags.CLI.JSON { if err != nil {
fmt.Println(password) switch {
os.Exit(0) case err == pwd.ErrLengthTooLow:
} fmt.Println("The password has to be more than 4 characters long to meet the security considerations")
default:
fmt.Println("An unknown error occured")
}
os.Exit(1)
}
if !flags.CLI.JSON {
fmt.Println(password)
continue
}
hashes, err := hasher.GetHashMap(password)
if err != nil {
fmt.Printf("Unable to generate hashes: %s", err)
os.Exit(1)
}
hashes["password"] = password
json.NewEncoder(os.Stdout).Encode(hashes)
hashes, err := hasher.GetHashMap(password)
if err != nil {
fmt.Printf("Unable to generate hashes: %s", err)
os.Exit(1)
} }
hashes["password"] = password
json.NewEncoder(os.Stdout).Encode(hashes)
} }

View File

@ -5,6 +5,7 @@ var flags = struct {
Length int Length int
SpecialCharacters bool SpecialCharacters bool
JSON bool JSON bool
Num int
XKCD bool XKCD bool
PrependDate bool PrependDate bool