Skip to content

Commit

Permalink
Clean up and add some comments.
Browse files Browse the repository at this point in the history
Remove unnecessary dependencies.
Update README.
  • Loading branch information
ro-tex committed Nov 25, 2022
1 parent b89d0f3 commit f1ca04b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 29 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
# aes256cli

A simple file encrypt/decrypt tool.

The tool uses [Go](https://go.dev/)'s built-in [crypto/aes](https://pkg.go.dev/crypto/aes) library to encrypt the input
file with [AES-256](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard).

### Caveat

Currently, the `aes256cli` reads the entire input file in memory before encrypting it and writing it to disk. This means
that you can easily run out of memory if you try encrypting a large file. I hope to address this in the near future.

## Installation

If you have [Go](https://go.dev/) installed:

```shell
go install github.com/ro-tex/aes256cli@latest
```

If you prefer a binary, you can download a Linux amd64 one from https://github.com/ro-tex/aes256cli/releases.

## Usage

To encrypt a file:

```shell
aes256cli -e myFile.dat
```

To decrypt a file:

```shell
aes256cli -d myFile.dat.aes
```

To see the usage information run the tool without parameters:

```shell
$ aes256cli
You must choose to either encrypt (-e/--encrypt) or decrypt (-d/--decrypt) a file.

Usage of aes256cli:

aes256cli [operation] FILENAME

-d decrypt a file
-decrypt
decrypt a file
-e encrypt a file
-encrypt
encrypt a file
```
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/ro-tex/aes256cli
go 1.19

require (
gitlab.com/NebulousLabs/errors v0.0.0-20200929122200-06c536cf6975
golang.org/x/crypto v0.3.0
golang.org/x/term v0.2.0
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
gitlab.com/NebulousLabs/errors v0.0.0-20200929122200-06c536cf6975 h1:L/ENs/Ar1bFzUeKx6m3XjlmBgIUlykX9dzvp5k9NGxc=
gitlab.com/NebulousLabs/errors v0.0.0-20200929122200-06c536cf6975/go.mod h1:ZkMZ0dpQyWwlENaeZVBiQRjhMEZvk6VTXquzl3FOFP8=
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
Expand Down
46 changes: 20 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package main

import (
"bufio"
"crypto/aes"
"crypto/cipher"
"errors"
"flag"
"fmt"
"io"
"os"
"strings"

"gitlab.com/NebulousLabs/errors"
"golang.org/x/crypto/blake2b"
"golang.org/x/term"
)
Expand All @@ -21,15 +20,6 @@ const (
FilePerm = 0600
)

// readFile opens the given file for reading and returns a reader and a closing function.
func readFile(fPath string) (r *bufio.Reader, closeFn func() error, err error) {
file, err := os.Open(fPath)
if err != nil {
return nil, nil, err
}
return bufio.NewReader(file), file.Close, nil
}

// readPasswordFromTerminal prompts the user to enter a password and then reads
// it from stdin.
func readPasswordFromTerminal() (passwd []byte, err error) {
Expand All @@ -51,18 +41,20 @@ func readPasswordFromTerminal() (passwd []byte, err error) {
return passwd, nil
}

func outputFile(inputFile string, actionEncrypt bool) (*os.File, string, error) {
var outputFile string
// createOutputFile determines the name of the required output file and creates
// it. It does *NOT* close it - that is a responsibility of the caller.
func createOutputFile(inFileName string, actionEncrypt bool) (*os.File, string, error) {
var outFileName string
if actionEncrypt {
outputFile = inputFile + FileExtension
outFileName = inFileName + FileExtension
} else {
outputFile = strings.TrimSuffix(inputFile, FileExtension)
outFileName = strings.TrimSuffix(inFileName, FileExtension)
}
// Check if the output file already exists and (if so) whether the user
// wants to overwrite it or not.
if _, err := os.Stat(outputFile); err == nil {
if _, err := os.Stat(outFileName); err == nil {
for {
fmt.Printf("Output file %s already exists.\nDo you want to overwrite it? (y/n) ", outputFile)
fmt.Printf("Output file %s already exists.\nDo you want to overwrite it? (y/n) ", outFileName)
var answer string
_, err = fmt.Scanln(&answer)
if err != nil {
Expand All @@ -77,23 +69,23 @@ func outputFile(inputFile string, actionEncrypt bool) (*os.File, string, error)
}
}
}
outFile, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY, FilePerm)
outFile, err := os.OpenFile(outFileName, os.O_CREATE|os.O_WRONLY, FilePerm)
if err != nil {
return nil, "", fmt.Errorf("Failed to open output file %s for writing! Error: %v\n", outputFile, err)
return nil, "", fmt.Errorf("Failed to open output file %s for writing! Error: %v\n", outFileName, err)
}
return outFile, outputFile, nil
return outFile, outFileName, nil
}

// encDec handles encryption and decryption.
func encDec(filename string, actionEncrypt bool) error {
inFile, closeFn, err := readFile(filename)
// encodeDecode handles encryption and decryption.
func encodeDecode(filename string, actionEncrypt bool) error {
inFile, err := os.Open(filename)
if err != nil {
fmt.Printf("Failed to read file %s! Error: %v\n", filename, err)
os.Exit(1)
}
defer func() { _ = closeFn() }()
defer func() { _ = inFile.Close() }()

outFile, outFName, err := outputFile(filename, actionEncrypt)
outFile, outFName, err := createOutputFile(filename, actionEncrypt)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down Expand Up @@ -138,12 +130,14 @@ func encDec(filename string, actionEncrypt bool) error {
if actionEncrypt {
nonce := make([]byte, aead.NonceSize())
outBytes = aead.Seal(nonce, nonce, inBytes, nil)
inBytes = nil
} else {
nonceSize := aead.NonceSize()
if len(inBytes) < nonceSize {
return errors.New("Unexpected end of ciphertext.")
}
nonce, ciphertext := inBytes[:nonceSize], inBytes[nonceSize:]
inBytes = nil
outBytes, err = aead.Open(nil, nonce, ciphertext, nil)
if err != nil {
return err
Expand Down Expand Up @@ -182,7 +176,7 @@ func main() {
}
inFName := flag.Arg(0)

err := encDec(inFName, *actionEncrypt)
err := encodeDecode(inFName, *actionEncrypt)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down

0 comments on commit f1ca04b

Please sign in to comment.