Skip to content

Commit

Permalink
Merge pull request #2 from penguingovernor/2.0.1
Browse files Browse the repository at this point in the history
2.0.1
  • Loading branch information
penguingovernor authored Dec 14, 2018
2 parents 49daacc + 466bd20 commit 021c182
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 283 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
goxor
goxor.exe
*.xor
*.xor.key
3 changes: 0 additions & 3 deletions .travis.yml

This file was deleted.

10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GO_FILES := $(shell find . -name '*.go')
VERSION := 2.0.1

all: clean build

build: $(GO_FILES)
go build -ldflags="-X github.com/penguingovernor/goxor/internal/constants.Version=${VERSION}"

clean:
go clean
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# goxor #
[![Go Report Card](https://goreportcard.com/badge/github.com/PenguinGovernor/goxor)](https://goreportcard.com/report/github.com/PenguinGovernor/goxor) [![Build Status](https://travis-ci.com/PenguinGovernor/goxor.svg?branch=master)](https://travis-ci.com/PenguinGovernor/goxor)
[![Go Report Card](https://goreportcard.com/badge/github.com/PenguinGovernor/goxor)](https://goreportcard.com/report/github.com/PenguinGovernor/goxor)

GoXor is an encryption method written in the [Go programming language](https://golang.org/), and is based on a [xor cipher](https://en.wikipedia.org/wiki/XOR_cipher) in conjunction with a [one-time pad](https://en.wikipedia.org/wiki/One-time_pad).

Expand Down
File renamed without changes.
File renamed without changes.
91 changes: 4 additions & 87 deletions cmd/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@
package cmd

import (
"fmt"
"io/ioutil"
"log"
"os"

"github.com/penguingovernor/goxor/xor"

"github.com/penguingovernor/goxor/protocol"

"github.com/penguingovernor/goxor/internal/cmdutil"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -52,88 +46,11 @@ $ goxor decrypt -i out.xor -k out.xor.key -o test`,
log.Println(err)
}

decrypt(intputFlag, keyFlag, outFlag)

},
}

func decrypt(in, key, out string) {
input := dGetInput(in)
keyData := dGetKey(key)
data, err := xor.Decrypt(input, keyData)
if err != nil {
log.Fatal(err)
}
fmt.Println("Done decrypting file")
dWriteOutput(data, out)
}

func dWriteOutput(data *protocol.Data, out string) {

if out == "" {
fmt.Println("---BEGIN DECRYPTED DATA---")
if _, err := os.Stdout.Write(data.PayLoad); err != nil {
log.Fatalf("could not write data: %v", err)
if err := cmdutil.Decrypt(intputFlag, keyFlag, outFlag); err != nil {
log.Fatalf("decryption err: %v\n", err)
}
fmt.Println("---END DECRYPTED DATA---")
return
}

file, err := os.Create(out + ".xor")
if err != nil {
log.Fatalf("Could not create file %s.xor: %v", out, err)
}

if _, err := file.Write(data.PayLoad); err != nil {
log.Fatalf("could not write to file %s.xor: %v", out, err)
}

if err := file.Close(); err != nil {
log.Fatalf("Could not close file %s.xor: %v", out, err)
}

fmt.Println("Data written to:", out, ".xor")

}

func dGetInput(in string) *protocol.Data {
if in == "" {
log.Fatal("flag input is required")
}

fileBytes, err := ioutil.ReadFile(in)
if err != nil {
log.Fatalf("could not read %s: %v\n", in, err)
}

data, err := xor.LoadData(fileBytes)
if err != nil {
log.Fatalf("could not load file %s: %v\n", in, err)
}

fmt.Println("Using file:", in, "as input")

return data
}

func dGetKey(in string) *protocol.Key {
if in == "" {
log.Fatal("flag key is required")
}

fileBytes, err := ioutil.ReadFile(in)
if err != nil {
log.Fatalf("could not read %s: %v", in, err)
}

data, err := xor.LoadKey(fileBytes)
if err != nil {
log.Fatalf("could not load file %s: %v", in, err)
}

fmt.Println("Using file:", in, "as key")

return data
},
}

func init() {
Expand Down
193 changes: 4 additions & 189 deletions cmd/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@
package cmd

import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"strings"
"time"

"github.com/penguingovernor/goxor/protocol"

"github.com/penguingovernor/goxor/xor"
"github.com/penguingovernor/goxor/internal/cmdutil"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -67,7 +59,9 @@ goxor encrypt -i hello -k test -s sig`,
log.Println(err)
}
// Encrypt the files
encrypt(inFlag, keyFlag, sigFlag, outputFlag, keyOutFlag)
if err := cmdutil.Encrypt(inFlag, keyFlag, sigFlag, outputFlag, keyOutFlag); err != nil {
log.Fatalf("encryption error: %v\n", err)
}
},
}

Expand Down Expand Up @@ -113,182 +107,3 @@ if the input is "stdin", then stdin will be used as the signautre`
encryptCmd.Flags().StringP("signature", "s", "", sigMsg)

}

func encrypt(inputFlag, keyFlag, signatureFlag, outputFlag, outputKeyFlag string) {

// Get the bytes from user input
inputBytes := getInput(inputFlag)
keyBytes := getKey(keyFlag, len(inputBytes))
signatureBytes := getSignature(signatureFlag)

// Generate the appropriate data
data := xor.GenerateData(inputBytes, signatureBytes)
key := xor.GenerateKey(keyBytes, signatureBytes)

// Encrypt the data
eData, err := xor.Encrypt(data, key)
if err != nil {
log.Fatalf("error while encryptng file: %v", err)
}

// Say we're done and output the files
fmt.Println("Done encrypting file")
writeData(eData, outputFlag)
writeKey(key, outputKeyFlag)

}

func writeData(data *protocol.Data, dest string) {
outname := dest

if strings.ToLower(dest) == "stdout" {
fmt.Println("---BEGIN ENCRYPTED DATA---")
xor.WriteData(os.Stdout, data)
fmt.Println("---END ENCRYPTED DATA---")
return
}

if dest == "" {
outname = "out"
}

file, err := os.Create(outname + ".xor")
if err != nil {
log.Fatalf("Could not create file %s: %v\n", outname+".xor", err)
}

if err := xor.WriteData(file, data); err != nil {
log.Fatalf("could not write key %s: %v\n", outname+".xor", err)
}

if err := file.Close(); err != nil {
log.Fatalf("could not close file %s: %v\n", outname+".xor.key", err)
}

fmt.Println("Data written to:", outname+".xor")
}

func writeKey(data *protocol.Key, dest string) {
outname := dest

if strings.ToLower(dest) == "stdout" {
fmt.Println("---BEGIN KEY DATA---")
xor.WriteKey(os.Stdout, data)
fmt.Println("---END KEY DATA---")
return
}

if dest == "" {
outname = "out"
}

file, err := os.Create(outname + ".xor.key")
if err != nil {
log.Fatalf("Could not create file %s: %v\n", outname+".xor.key", err)
}

if err := xor.WriteKey(file, data); err != nil {
log.Fatalf("could not write key %s: %v\n", outname+".xor.key", err)
}

if err := file.Close(); err != nil {
log.Fatalf("could not close file %s: %v\n", outname+".xor.key", err)
}

fmt.Println("Key written to:", outname+".xor.key")

}

func getInput(input string) []byte {
// If omitted
if input == "" {
fmt.Println("Reading from stdin for input, press ctrl-d to stop")
stdinBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("Could not read from stdin: %v", err)
}
return stdinBytes
}
// Try to open the file
fileBytes, err := ioutil.ReadFile(input)
// If we couldn't open the file, treat input as string
if err != nil {
fmt.Printf("Using string: \"%s\" as input\n", input)
return []byte(input)
}
// If we could open the file, return the bytes
fmt.Println("Using file:", input, "as input")
return fileBytes
}

func getKey(input string, length int) []byte {

// If omitted, use otp
if input == "" {
fmt.Println("Using one time pad as key")
// Seed random
rand.Seed(time.Now().Unix())
// Make the pad
key := make([]byte, length)
_, err := rand.Read(key)
if err != nil {
log.Fatalf("Could not generate one time pad: %v", err)
}
return key
}

// If stdin
if strings.ToLower(input) == "stdin" {
fmt.Println("Reading from stdin for key, press ctrl-d to stop")
key, err := ioutil.ReadAll(os.Stdin)
fmt.Println("")
if err != nil {
log.Fatalf("Could not read from stdin: %v", err)
}
return key
}

// Try to open the file
fileBytes, err := ioutil.ReadFile(input)
// If we couldn't open the file, treat input as string
if err != nil {
fmt.Printf("Using string: \"%s\" as key\n", input)
return []byte(input)
}

// If we could open the file, return the bytes
fmt.Println("Using file:", input, "as key")
return fileBytes
}

func getSignature(input string) []byte {
// If omitted, use goxor
if input == "" {
fmt.Println("Using \"goxor\" as signature")
return []byte("goxor")
}

// If stdin
if strings.ToLower(input) == "stdin" {
fmt.Println("Reading from stdin for signature, press ctrl-d to stop")
key, err := ioutil.ReadAll(os.Stdin)
fmt.Println("")
if err != nil {
log.Fatalf("Could not read from stdin: %v", err)
}
return key
}

// Try to open the file
fileBytes, err := ioutil.ReadFile(input)
// If we couldn't open the file, treat input as string
if err != nil {
fmt.Printf("Using string: \"%s\" as signature\n", input)
return []byte(input)
}

// If we could open the file, return the bytes
fmt.Println("Using file:", input, "as signature")
return fileBytes

}
17 changes: 16 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ package cmd

import (
"fmt"
"log"
"os"
"runtime"

"github.com/penguingovernor/goxor/internal/constants"
"github.com/spf13/cobra"
)

Expand All @@ -29,7 +32,19 @@ var rootCmd = &cobra.Command{
Short: "goxor is a cli encryption and decryption tool",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Run: func(cmd *cobra.Command, args []string) {
version, err := cmd.Flags().GetBool("version")
if err != nil {
log.Fatalf("internal error: could not get flag %q: %v\n", "version", err)
}
if version {
fmt.Printf("Goxor version %s %s/%s\n", constants.Version, runtime.GOOS, runtime.GOARCH)
}
},
}

func init() {
rootCmd.Flags().BoolP("version", "v", false, "Print version infromation and quit")
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
Loading

0 comments on commit 021c182

Please sign in to comment.