Skip to content

Commit

Permalink
adding support for string encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
shibme committed Jan 10, 2024
1 parent 7ef36eb commit 8d62c6e
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 36 deletions.
19 changes: 19 additions & 0 deletions cli/internal/commands/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,20 @@ var (
// Encrypt Command
encryptCmd *cobra.Command

// Encrypt String Command
encryptStrCmd *cobra.Command

// Encrypt File Command
encryptFileCmd *cobra.Command

// Decrypt Command
decryptCmd *cobra.Command

// Decrypt String Command
decryptStrCmd *cobra.Command

// Decrypt File Command
decryptFileCmd *cobra.Command
)

type flagDef struct {
Expand Down Expand Up @@ -52,6 +64,13 @@ var (
usage: "Specify a key string",
}

// String Flag
stringFlag = flagDef{
name: "string",
shorthand: "s",
usage: "Specify a string",
}

// File Flag
fileFlag = flagDef{
name: "file",
Expand Down
95 changes: 81 additions & 14 deletions cli/internal/commands/decrypt.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package commands

import (
"bytes"
"fmt"
"os"

"dev.shib.me/xipher"
"github.com/spf13/cobra"
"golang.org/x/term"
)

func decryptCommand() *cobra.Command {
Expand All @@ -15,6 +15,76 @@ func decryptCommand() *cobra.Command {
}
decryptCmd = &cobra.Command{
Use: "decrypt",
Short: "Decrypts the data",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
decryptCmd.AddCommand(decryptStringCommand())
decryptCmd.AddCommand(decryptFileCommand())
return decryptCmd
}

func decryptStringCommand() *cobra.Command {
if decryptStrCmd != nil {
return decryptStrCmd
}
decryptStrCmd = &cobra.Command{
Use: "string",
Aliases: []string{"str"},
Short: "Decrypts a xipher encrypted string",
Run: func(cmd *cobra.Command, args []string) {
cipheredStr, err := decode(cmd.Flag(stringFlag.name).Value.String())
if err != nil {
exitOnError(err)
}
var src, dst bytes.Buffer
src.Write(cipheredStr)
keyStr := cmd.Flag(keyFlag.name).Value.String()
if keyStr != "" {
keyBytes, err := decode(keyStr)
if err != nil {
exitOnError(err)
}
privKey, err := xipher.ParsePrivateKey(keyBytes)
if err != nil {
exitOnError(err)
}
err = privKey.DecryptStream(&dst, &src)
if err != nil {
exitOnError(err)
}
} else {
// Get password from user
password, err := getPasswordFromUser(false)
if err != nil {
exitOnError(err)
}
privKey, err := xipher.NewPrivateKeyForPassword(password)
if err != nil {
exitOnError(err)
}
err = privKey.DecryptStream(&dst, &src)
if err != nil {
exitOnError(err)
}
}
fmt.Println(dst.String())
safeExit()
},
}
decryptStrCmd.Flags().StringP(stringFlag.name, stringFlag.shorthand, "", stringFlag.usage)
decryptStrCmd.Flags().StringP(keyFlag.name, keyFlag.shorthand, "", keyFlag.usage)
decryptStrCmd.MarkFlagRequired(stringFlag.name)
return decryptStrCmd
}

func decryptFileCommand() *cobra.Command {
if decryptFileCmd != nil {
return decryptFileCmd
}
decryptFileCmd = &cobra.Command{
Use: "file",
Short: "Decrypts a xipher encrypted file",
Run: func(cmd *cobra.Command, args []string) {
srcPath := cmd.Flag(fileFlag.name).Value.String()
Expand Down Expand Up @@ -43,33 +113,30 @@ func decryptCommand() *cobra.Command {
}
err = privKey.DecryptStream(dst, src)
if err != nil {
exitOnErrorWithMessage("Error decrypting file.")
exitOnError(err)
}
} else {
// Get password from user
fmt.Print("Password: ")
var password []byte
password, err = term.ReadPassword(int(os.Stdin.Fd()))
password, err := getPasswordFromUser(false)
if err != nil {
exitOnErrorWithMessage("Error reading password.")
exitOnError(err)
}
fmt.Println()
privKey, err := xipher.NewPrivateKeyForPassword(password)
if err != nil {
exitOnError(err)
}
err = privKey.DecryptStream(dst, src)
if err != nil {
exitOnErrorWithMessage("Error decrypting file.")
exitOnError(err)
}
}
safeExit()
},
}
decryptCmd.Flags().StringP(fileFlag.name, fileFlag.shorthand, "", fileFlag.usage)
decryptCmd.Flags().StringP(outFlag.name, outFlag.shorthand, "", outFlag.usage)
decryptCmd.Flags().StringP(keyFlag.name, keyFlag.shorthand, "", keyFlag.usage)
decryptCmd.MarkFlagRequired(fileFlag.name)
decryptCmd.MarkFlagRequired(outFlag.name)
return decryptCmd
decryptFileCmd.Flags().StringP(fileFlag.name, fileFlag.shorthand, "", fileFlag.usage)
decryptFileCmd.Flags().StringP(outFlag.name, outFlag.shorthand, "", outFlag.usage)
decryptFileCmd.Flags().StringP(keyFlag.name, keyFlag.shorthand, "", keyFlag.usage)
decryptFileCmd.MarkFlagRequired(fileFlag.name)
decryptFileCmd.MarkFlagRequired(outFlag.name)
return decryptFileCmd
}
101 changes: 88 additions & 13 deletions cli/internal/commands/encrypt.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package commands

import (
"bytes"
"fmt"
"os"

"dev.shib.me/xipher"
"github.com/spf13/cobra"
"golang.org/x/term"
)

func encryptCommand() *cobra.Command {
Expand All @@ -15,7 +15,85 @@ func encryptCommand() *cobra.Command {
}
encryptCmd = &cobra.Command{
Use: "encrypt",
Short: "Encrypts a file",
Short: "Encrypts the data",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
encryptCmd.AddCommand(encryptStringCommand())
encryptCmd.AddCommand(encryptFileCommand())
return encryptCmd
}

func encryptStringCommand() *cobra.Command {
if encryptStrCmd != nil {
return encryptStrCmd
}
encryptStrCmd = &cobra.Command{
Use: "string",
Aliases: []string{"str"},
Short: "Encrypts a given string",
Run: func(cmd *cobra.Command, args []string) {
inputStr := cmd.Flag(stringFlag.name).Value.String()
var src, dst bytes.Buffer
src.WriteString(inputStr)
keyStr := cmd.Flag(keyFlag.name).Value.String()
if keyStr != "" {
keyBytes, err := decode(keyStr)
if err != nil {
exitOnError(err)
}
if len(keyBytes) == xipher.PrivateKeyLength {
privKey, err := xipher.ParsePrivateKey(keyBytes)
if err != nil {
exitOnError(err)
}
err = privKey.EncryptStream(&dst, &src, true)
if err != nil {
exitOnError(err)
}
} else {
pubKey, err := xipher.ParsePublicKey(keyBytes)
if err != nil {
exitOnError(err)
}
err = pubKey.EncryptStream(&dst, &src, true)
if err != nil {
exitOnError(err)
}
}
} else {
// Get password from user
password, err := getPasswordFromUser(true)
if err != nil {
exitOnError(err)
}
privKey, err := xipher.NewPrivateKeyForPassword(password)
if err != nil {
exitOnError(err)
}
err = privKey.EncryptStream(&dst, &src, true)
if err != nil {
exitOnError(err)
}
}
fmt.Println(encode(dst.Bytes()))
safeExit()
},
}
encryptStrCmd.Flags().StringP(stringFlag.name, stringFlag.shorthand, "", stringFlag.usage)
encryptStrCmd.Flags().StringP(keyFlag.name, keyFlag.shorthand, "", keyFlag.usage)
encryptStrCmd.MarkFlagRequired(stringFlag.name)
return encryptStrCmd
}

func encryptFileCommand() *cobra.Command {
if encryptFileCmd != nil {
return encryptFileCmd
}
encryptFileCmd = &cobra.Command{
Use: "file",
Short: "Encrypts a given file",
Run: func(cmd *cobra.Command, args []string) {
srcPath := cmd.Flag(fileFlag.name).Value.String()
src, err := os.Open(srcPath)
Expand Down Expand Up @@ -58,13 +136,10 @@ func encryptCommand() *cobra.Command {
}
} else {
// Get password from user
fmt.Print("Password: ")
var password []byte
password, err = term.ReadPassword(int(os.Stdin.Fd()))
password, err := getPasswordFromUser(true)
if err != nil {
exitOnErrorWithMessage("Error reading password.")
exitOnError(err)
}
fmt.Println()
privKey, err := xipher.NewPrivateKeyForPassword(password)
if err != nil {
exitOnError(err)
Expand All @@ -77,10 +152,10 @@ func encryptCommand() *cobra.Command {
safeExit()
},
}
encryptCmd.Flags().StringP(fileFlag.name, fileFlag.shorthand, "", fileFlag.usage)
encryptCmd.Flags().StringP(outFlag.name, outFlag.shorthand, "", outFlag.usage)
encryptCmd.Flags().StringP(keyFlag.name, keyFlag.shorthand, "", keyFlag.usage)
encryptCmd.Flags().BoolP(compressFlag.name, compressFlag.shorthand, false, compressFlag.usage)
encryptCmd.MarkFlagRequired(fileFlag.name)
return encryptCmd
encryptFileCmd.Flags().StringP(fileFlag.name, fileFlag.shorthand, "", fileFlag.usage)
encryptFileCmd.Flags().StringP(outFlag.name, outFlag.shorthand, "", outFlag.usage)
encryptFileCmd.Flags().StringP(keyFlag.name, keyFlag.shorthand, "", keyFlag.usage)
encryptFileCmd.Flags().BoolP(compressFlag.name, compressFlag.shorthand, false, compressFlag.usage)
encryptFileCmd.MarkFlagRequired(fileFlag.name)
return encryptFileCmd
}
8 changes: 2 additions & 6 deletions cli/internal/commands/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"dev.shib.me/xipher"
"github.com/spf13/cobra"
"golang.org/x/term"
)

func keygenCommand() *cobra.Command {
Expand All @@ -24,13 +23,10 @@ func keygenCommand() *cobra.Command {
var privKey *xipher.PrivateKey
if pwdFlag {
// Get password from user
fmt.Print("Password: ")
var password []byte
password, err = term.ReadPassword(int(os.Stdin.Fd()))
password, err := getPasswordFromUser(true)
if err != nil {
exitOnErrorWithMessage("Error reading password.")
exitOnError(err)
}
fmt.Println()
privKey, err = xipher.NewPrivateKeyForPassword(password)
if err != nil {
exitOnError(err)
Expand Down
30 changes: 30 additions & 0 deletions cli/internal/commands/password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package commands

import (
"bytes"
"fmt"
"syscall"

"golang.org/x/term"
)

func getPasswordFromUser(confirm bool) ([]byte, error) {
fmt.Print("Enter Password:\t")
password, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, err
}
fmt.Println()
if confirm {
fmt.Print("Confirm Password:\t")
confirmPassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, err
}
fmt.Println()
if !bytes.Equal(password, confirmPassword) {
return nil, fmt.Errorf("passwords do not match")
}
}
return password, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/fatih/color v1.16.0
github.com/spf13/cobra v1.8.0
golang.org/x/crypto v0.17.0
golang.org/x/crypto v0.18.0
golang.org/x/term v0.16.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
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.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
Expand Down

0 comments on commit 8d62c6e

Please sign in to comment.