Commit 91f6257

Karn Wong <[email protected]>
2023-12-29 16:35:18
add security: generate passphrase
1 parent df047d9
cmd/security/security_generate_passphrase.go
@@ -0,0 +1,43 @@
+package security
+
+import (
+	"fmt"
+	"log"
+	"strings"
+
+	"github.com/kahnwong/swissknife/cmd/utils"
+
+	"github.com/fatih/color"
+	"github.com/sethvargo/go-diceware/diceware"
+	"github.com/spf13/cobra"
+)
+
+func generatePassphrase() string {
+	// Generate 6 words using the diceware algorithm.
+	list, err := diceware.Generate(6)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	res := strings.Join(list, "-")
+
+	return res
+}
+
+var generatePassphraseCmd = &cobra.Command{
+	Use:   "generate-passphrase",
+	Short: "Generate passphrase",
+	Long:  `Generate passphrase. Result is copied to clipboard.`,
+	Run: func(cmd *cobra.Command, args []string) {
+		// main
+		color.Green("Security: generate passphrase")
+
+		passphrase := generatePassphrase()
+		utils.CopyToClipboard(passphrase)
+		fmt.Printf("%s\n", passphrase)
+	},
+}
+
+func init() {
+	Cmd.AddCommand(generatePassphraseCmd)
+}
go.mod
@@ -4,6 +4,7 @@ go 1.20
 
 require (
 	github.com/fatih/color v1.15.0
+	github.com/sethvargo/go-diceware v0.3.0
 	github.com/sethvargo/go-password v0.2.0
 	github.com/spf13/cobra v1.7.0
 	golang.design/x/clipboard v0.7.0
go.sum
@@ -10,6 +10,8 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
 github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
 github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
 github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/sethvargo/go-diceware v0.3.0 h1:UVVEfmN/uF50JfWAN7nbY6CiAlp5xeSx+5U0lWKkMCQ=
+github.com/sethvargo/go-diceware v0.3.0/go.mod h1:lH5Q/oSPMivseNdhMERAC7Ti5oOPqsaVddU1BcN1CY0=
 github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI=
 github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE=
 github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=