Skip to content

Commit

Permalink
refactor and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
enrichman committed Oct 19, 2022
1 parent b09aabc commit a18af2d
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 243 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
images/*
out/
out/
stego
104 changes: 0 additions & 104 deletions cmd/stego/decrypt.go

This file was deleted.

95 changes: 0 additions & 95 deletions cmd/stego/main.go

This file was deleted.

2 changes: 2 additions & 0 deletions images/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
111 changes: 111 additions & 0 deletions internal/cli/decrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package cli

import (
"bufio"
"fmt"
"os"

"github.com/enrichman/stegosecrets/internal/decrypt"
"github.com/enrichman/stegosecrets/pkg/file"
"github.com/spf13/cobra"
)

var (
encryptedFile string
masterKeyFile string
keyFiles []string
imageFiles []string
)

func newDecryptCmd() *cobra.Command {
decryptCmd := &cobra.Command{
Use: "decrypt",
Short: "decrypt",
Long: ``,
RunE: runDecryptCmd,
}

decryptCmd.Flags().StringVarP(&encryptedFile, "file", "f", "", "file")
decryptCmd.Flags().StringVar(&masterKeyFile, "master-key", "", "masterkey")
decryptCmd.Flags().StringArrayVar(&keyFiles, "key", []string{}, "keys")
decryptCmd.Flags().StringArrayVar(&imageFiles, "img", []string{}, "images")

return decryptCmd
}

func runDecryptCmd(cmd *cobra.Command, args []string) error {

decrypterOpts := []decrypt.OptFunc{}

if masterKeyFile != "" {
decrypterOpts = append(decrypterOpts, decrypt.WithMasterKeyFile(masterKeyFile))
}

for _, filename := range keyFiles {
decrypterOpts = append(decrypterOpts, decrypt.WithPartialKeyFile(filename))
}

for _, filename := range imageFiles {
decrypterOpts = append(decrypterOpts, decrypt.WithPartialKeyImageFile(filename))
}

decrypter, err := decrypt.NewDecrypter(decrypterOpts...)
if err != nil {
return err
}

// _, err := sss.Combine(parts)
// if err != nil {
// panic(err)
// }

var encryptedBytes []byte
if encryptedFile != "" {
encryptedBytes, err = getEncryptedInputFromFile(encryptedFile)
if err != nil {
return err
}
} else {
encryptedBytes, err = getEncryptedInputFromStdin()
if err != nil {
return err
}
}

return decrypter.Decrypt(encryptedBytes)
}

func getEncryptedInputFromFile(filename string) ([]byte, error) {
return file.ReadFile(filename)
}

func getEncryptedInputFromStdin() ([]byte, error) {
fi, err := os.Stdin.Stat()
if err != nil {
return nil, err
}

if fi.Mode()&os.ModeNamedPipe == 0 {
// TODO ?
os.Exit(1)
}

input := []byte{}
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Println("scanning")
scanner.Scan()
// Holds the string that was scanned
text := scanner.Bytes()
if len(text) == 0 {
break
}
input = append(input, text...)
}

if scanner.Err() != nil {
fmt.Println("Error: ", scanner.Err())
}

return input, nil
}
39 changes: 39 additions & 0 deletions internal/cli/encrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cli

import (
"bufio"
"bytes"
"fmt"
"os"

"github.com/enrichman/stegosecrets/internal/encrypt"
"github.com/spf13/cobra"
)

func newEncryptCmd() *cobra.Command {
return &cobra.Command{
Use: "encrypt",
Short: "encrypt",
Long: ``,
RunE: runEncryptCmd,
}
}

func runEncryptCmd(cmd *cobra.Command, args []string) error {
fi, err := os.Stdin.Stat()
if err != nil {
return err
}

// no pipe
if fi.Mode()&os.ModeNamedPipe == 0 {
fmt.Print("Enter message: ")
}

message, _ := bufio.NewReader(os.Stdin).ReadBytes('\n')

encrypter := encrypt.Encrypter{Parts: 2, Threshold: 2}
encrypter.Encrypt(bytes.NewReader(message))

return nil
}
Loading

0 comments on commit a18af2d

Please sign in to comment.