Skip to content

Commit

Permalink
adding support to generate public key from an existing secret key
Browse files Browse the repository at this point in the history
  • Loading branch information
shibme committed Sep 4, 2024
1 parent 6b9d93f commit 190ec02
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ builds:
goarch:
- arm
goarm:
- 7
- "7"
env:
- CC=arm-linux-gnueabihf-gcc
- CXX=arm-linux-gnueabihf-g++
Expand Down Expand Up @@ -205,7 +205,7 @@ checksum:
name_template: "{{ .ProjectName }}_checksums.txt"

snapshot:
name_template: "{{ incpatch .Version }}-dev"
version_template: "{{ incpatch .Version }}-dev"

changelog:
sort: asc
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ require (
github.com/fatih/color v1.17.0
github.com/spf13/cobra v1.8.1
golang.org/x/crypto v0.26.0
golang.org/x/term v0.23.0
golang.org/x/term v0.24.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/sys v0.25.0 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 2 additions & 2 deletions internal/cli/commands/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func getSecretKeyOrPwd() (string, error) {
secret = new(string)
*secret = os.Getenv(envar_XIPHER_SECRET)
if *secret == "" {
password, err := getPasswordFromUser(false, true)
passwordOrSecretKey, err := getPasswordOrSecretKeyFromUser(false, true)
if err != nil {
return "", err
}
*secret = string(password)
*secret = string(passwordOrSecretKey)
}
}
return *secret, nil
Expand Down
21 changes: 11 additions & 10 deletions internal/cli/commands/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"syscall"
"unicode"

"dev.shib.me/xipher/utils"
"golang.org/x/term"
)

Expand Down Expand Up @@ -63,28 +64,28 @@ func getHiddenInputFromUser(prompt string) ([]byte, error) {
return input, err
}

func getPasswordFromUser(confirm, ignorePolicyCheck bool) ([]byte, error) {
initialPrompt := "Enter a Password: "
if !confirm {
initialPrompt = "Enter Password/Secret Key: "
}
password, err := getHiddenInputFromUser(initialPrompt)
func getPasswordOrSecretKeyFromUser(confirm, ignorePolicyCheck bool) ([]byte, error) {
initialPrompt := "Enter a Password/Secret Key: "
passwordOrSecretKey, err := getHiddenInputFromUser(initialPrompt)
if err != nil {
return nil, err
}
if utils.IsSecretKeyStr(string(passwordOrSecretKey)) {
return passwordOrSecretKey, nil
}
if !ignorePolicyCheck {
if err = pwdCheck(string(password)); err != nil {
if err = pwdCheck(string(passwordOrSecretKey)); err != nil {
return nil, err
}
}
if confirm {
if confirmPassword, err := getHiddenInputFromUser("Confirm Password: "); err != nil {
if confirmPassword, err := getHiddenInputFromUser("Confirm Password/Secret Key: "); err != nil {
return nil, err
} else if !bytes.Equal(password, confirmPassword) {
} else if !bytes.Equal(passwordOrSecretKey, confirmPassword) {
return nil, fmt.Errorf("passwords do not match")
}
}
return password, nil
return passwordOrSecretKey, nil
}

func readBufferFromStdin(prompt string) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/commands/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func keygenCommand() *cobra.Command {
}
keygenCmd = &cobra.Command{
Use: "keygen",
Short: "Generate a new key pair or public key based on a password",
Short: "Generate a new random key pair or a public key based on a given password or secret key",
Run: func(cmd *cobra.Command, args []string) {
publicKeyFilePath := cmd.Flag(publicKeyFileFlag.name).Value.String()
ignoreFlag, _ := cmd.Flags().GetBool(ignorePasswordCheckFlag.name)
Expand All @@ -30,7 +30,7 @@ func keygenCommand() *cobra.Command {
}
fmt.Println("Secret Key:", color.HiBlackString(secret))
} else {
password, err := getPasswordFromUser(true, ignoreFlag)
password, err := getPasswordOrSecretKeyFromUser(true, ignoreFlag)
if err != nil {
exitOnError(err)
}
Expand Down

0 comments on commit 190ec02

Please sign in to comment.