package cmd import ( "bytes" "fmt" "log" "os" "gitea.nehmer.net/torben/dyndns/service" "github.com/spf13/cobra" "golang.org/x/crypto/ssh/terminal" ) var cmdHashPassword = &cobra.Command{ Use: "hash-password", Short: "Hashes a password for usage in user config files", Long: `Creates a salted hash using bcrypt for use in userconfig files, so that we don't have to store a plaintext password.`, Run: func(cmd *cobra.Command, args []string) { service.LoadConfig() fmt.Println("Enter password:") password, err := terminal.ReadPassword(int(os.Stdin.Fd())) if err != nil { log.Fatalf("failed to read password: %v", err) } fmt.Println("Enter password again:") passwordCheck, err := terminal.ReadPassword(int(os.Stdin.Fd())) if err != nil { log.Fatalf("failed to read password: %v", err) } if !bytes.Equal(password, passwordCheck) { log.Fatalln("the passwords do not match.") } hash, err := service.HashPassword(password) if err != nil { log.Fatalf("failed to create password hash: %v", err) } fmt.Printf("Hashed password: %s\n", hash) }, } func init() { rootCmd.AddCommand(cmdHashPassword) }