Skip to content

Commit

Permalink
Add .env / .env.sample + dotenv pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
theognis1002 committed Aug 1, 2024
1 parent 8755790 commit d7c374e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
KEY=example key 1234
INPUT_FILE=example.csv
ENCRYPTED_FILE=encrypted.csv
DECRYPTED_FILE=decrypted.csv
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ Temporary Items
# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,macos,go

# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)

.env
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ This project provides a simple implementation of file encryption and decryption
1. **Clone the repository:**

```sh
git clone <repository-url>
cd <repository-directory>
git clone https://github.com/theognis1002/go-encrypt
cd go-encrypt/
```

2. **Set the secret key:**

Update the key variable in the main.go file with your 16, 24, or 32 bytes key for AES-128, AES-192, or AES-256 respectively.
Update the key variables in the `.env` file (using `.env.sample` as a reference) with your 16, 24, or 32 bytes key for AES-128, AES-192, or AES-256 respectively.

## Makefile Commands

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module go-encrypt

go 1.22.5

require github.com/joho/godotenv v1.5.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
29 changes: 24 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,36 @@ import (
"crypto/rand"
"fmt"
"io"
"log"
"os"

"github.com/joho/godotenv"
)

func main() {
key := []byte("example key 1234") // 16 bytes key for AES-128
inputFile := "example.csv"
encryptedFile := "encrypted.csv"
decryptedFile := "decrypted.csv"
// Load the environment variables from the .env file
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}

key := []byte(os.Getenv("KEY"))
inputFile := os.Getenv("INPUT_FILE")
encryptedFile := os.Getenv("ENCRYPTED_FILE")
decryptedFile := os.Getenv("DECRYPTED_FILE")

// Check if the key length is correct
if len(key) != 16 {
log.Fatalf("Key length must be 16 bytes")
}

fmt.Println("Key:", string(key))
fmt.Println("Input File:", inputFile)
fmt.Println("Encrypted File:", encryptedFile)
fmt.Println("Decrypted File:", decryptedFile)

// Encrypt the file
err := encryptFile(inputFile, encryptedFile, key)
err = encryptFile(inputFile, encryptedFile, key)
if err != nil {
fmt.Println("Error encrypting file:", err)
return
Expand Down

0 comments on commit d7c374e

Please sign in to comment.