dyndns/cmd/hash-password.go
Torben Nehmer 9952343cda Cleaned up config handling
- moved everything to cmd/root.go
- Sanitize all paths to full absolute ones
- Set a config key with the current config basedir
- moved default config logging to a central location
- resolve user config dir relative to config dir
- change cwd to config dir
2021-08-25 21:17:19 +02:00

47 lines
1.1 KiB
Go

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)
}