Skip to content

Commit

Permalink
modify commands and consider uuid as file name prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
MGTheTrain committed Nov 15, 2024
1 parent 907d9a6 commit b81e160
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 39 deletions.
5 changes: 3 additions & 2 deletions cmd/crypto-vault-cli/crypto-vault-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
}
decryptAESFileCmd.Flags().StringP("input", "i", "", "Input encrypted file path")
decryptAESFileCmd.Flags().StringP("output", "o", "", "Output decrypted file path")
decryptAESFileCmd.Flags().StringP("keyDir", "d", "", "Directory to read the encryption key from")
decryptAESFileCmd.Flags().StringP("symmetricKey", "k", "", "Path to the symmetric key")
rootCmd.AddCommand(decryptAESFileCmd)

// RSA Commands
Expand All @@ -43,7 +43,8 @@ func main() {
}
encryptRSAFileCmd.Flags().StringP("input", "i", "", "Input file path")
encryptRSAFileCmd.Flags().StringP("output", "o", "", "Output encrypted file path")
encryptRSAFileCmd.Flags().StringP("publicKey", "p", "", "Path to RSA public key")
encryptRSAFileCmd.Flags().StringP("keyDir", "d", "", "Directory to store the encryption key")

rootCmd.AddCommand(encryptRSAFileCmd)

var decryptRSAFileCmd = &cobra.Command{
Expand Down
24 changes: 14 additions & 10 deletions cmd/crypto-vault-cli/internal/commands/aes-commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"os"
"path/filepath"

"github.com/google/uuid" // Import UUID package
"github.com/spf13/cobra"
)

// Encrypts a file using AES and saves the encryption key
// Encrypts a file using AES and saves the symmetric key with a UUID prefix
func EncryptAESCmd(cmd *cobra.Command, args []string) {
inputFile, _ := cmd.Flags().GetString("input")
outputFile, _ := cmd.Flags().GetString("output")
Expand Down Expand Up @@ -49,30 +50,33 @@ func EncryptAESCmd(cmd *cobra.Command, args []string) {
}
fmt.Printf("Encrypted data saved to %s\n", outputFile)

// Save the AES key to the specified key directory
keyFilePath := filepath.Join(keyDir, "encryption_key.bin")
// Generate a UUID for the key filename
uniqueID := uuid.New().String() // Generate a unique UUID

// Save the AES key with the UUID prefix in the specified key directory
keyFilePath := filepath.Join(keyDir, fmt.Sprintf("%s-symmetric_key.bin", uniqueID))
err = utils.WriteFile(keyFilePath, key)
if err != nil {
log.Fatalf("Error writing AES key to file: %v\n", err)
}
fmt.Printf("AES key saved to %s\n", keyFilePath)
}

// Decrypts a file using AES and reads the corresponding symmetric key with a UUID prefix
func DecryptAESCmd(cmd *cobra.Command, args []string) {
inputFile, _ := cmd.Flags().GetString("input")
outputFile, _ := cmd.Flags().GetString("output")
keyDir, _ := cmd.Flags().GetString("keyDir")
symmetricKey, _ := cmd.Flags().GetString("symmetricKey")

// Validate input arguments
if inputFile == "" || outputFile == "" || keyDir == "" {
log.Fatalf("Error: input, output, and keyDir flags are required\n")
if inputFile == "" || outputFile == "" || symmetricKey == "" {
log.Fatalf("Error: input, output and symmetricKey flags are required\n")
}

// Read the encryption key from the specified directory
keyFilePath := filepath.Join(keyDir, "encryption_key.bin")
key, err := os.ReadFile(keyFilePath)
// Read the symmetric key from the file
key, err := os.ReadFile(symmetricKey)
if err != nil {
log.Fatalf("Error reading encryption key from file: %v\n", err)
log.Fatalf("Error reading symmetric key from file: %v\n", err)
}

// Decrypt the file
Expand Down
14 changes: 11 additions & 3 deletions cmd/crypto-vault-cli/internal/commands/ecdsa-commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"os"

"github.com/google/uuid"
"github.com/spf13/cobra"
)

Expand All @@ -19,6 +20,11 @@ func SignECCCmd(cmd *cobra.Command, args []string) {
inputFile, _ := cmd.Flags().GetString("input") // File to sign
keyDir, _ := cmd.Flags().GetString("keyDir") // Directory to save keys

// Validate input arguments
if inputFile == "" || keyDir == "" {
log.Fatalf("Error: input and keyDir flags are required\n")
}

// ECC implementation
ecdsaImpl := &cryptography.ECDSAImpl{}
var privateKey *ecdsa.PrivateKey
Expand Down Expand Up @@ -46,9 +52,11 @@ func SignECCCmd(cmd *cobra.Command, args []string) {
// Output the signature
fmt.Printf("Signature: %x\n", signature)

uniqueID := uuid.New()
// Save the private and public keys to files (if they were generated)
if privateKey != nil && keyDir != "" {
privateKeyFilePath := fmt.Sprintf("%s/private_key.pem", keyDir)
privateKeyFilePath := fmt.Sprintf("%s/%s-private_key.pem", keyDir, uniqueID.String())

err = ecdsaImpl.SavePrivateKeyToFile(privateKey, privateKeyFilePath)
if err != nil {
log.Fatalf("Error saving private key: %v\n", err)
Expand All @@ -57,7 +65,7 @@ func SignECCCmd(cmd *cobra.Command, args []string) {
}

if publicKey != nil && keyDir != "" {
publicKeyFilePath := fmt.Sprintf("%s/public_key.pem", keyDir)
publicKeyFilePath := fmt.Sprintf("%s/%s-public_key.pem", keyDir, uniqueID.String())
err = ecdsaImpl.SavePublicKeyToFile(publicKey, publicKeyFilePath)
if err != nil {
log.Fatalf("Error saving public key: %v\n", err)
Expand All @@ -67,7 +75,7 @@ func SignECCCmd(cmd *cobra.Command, args []string) {

// Save the signature to a file in the data folder (optional, based on the input file)
if keyDir != "" {
signatureFilePath := fmt.Sprintf("%s/signature.sig", keyDir)
signatureFilePath := fmt.Sprintf("%s/%s-signature.sig", keyDir, uniqueID.String())
err = ecdsaImpl.SaveSignatureToFile(signatureFilePath, signature)
if err != nil {
log.Fatalf("Error saving signature: %v\n", err)
Expand Down
52 changes: 28 additions & 24 deletions cmd/crypto-vault-cli/internal/commands/rsa-commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,49 @@ import (
"fmt"
"log"

"github.com/google/uuid"
"github.com/spf13/cobra"
)

// RSA Command
func EncryptRSACmd(cmd *cobra.Command, args []string) {
inputFile, _ := cmd.Flags().GetString("input")
outputFile, _ := cmd.Flags().GetString("output")
publicKeyPath, _ := cmd.Flags().GetString("publicKey")
keyDir, _ := cmd.Flags().GetString("keyDir") // Directory to save keys

// Validate input arguments
if inputFile == "" || outputFile == "" || keyDir == "" {
log.Fatalf("Error: input, output and keyDir flags are required\n")
}

// Generate RSA keys if no public key is provided
var publicKey *rsa.PublicKey
var err error
rsa := &cryptography.RSAImpl{}
if publicKeyPath == "" {
// Generate RSA keys

privateKey, pubKey, genErr := rsa.GenerateKeys(2048)
if genErr != nil {
log.Fatalf("Error generating RSA keys: %v\n", genErr)
}
publicKey = pubKey
uniqueID := uuid.New()
// Generate RSA keys

// Optionally save the private and public keys
err = rsa.SavePrivateKeyToFile(privateKey, "data/private_key.pem")
if err != nil {
log.Fatalf("Error saving private key: %v\n", err)
}
err = rsa.SavePublicKeyToFile(publicKey, "data/public_key.pem")
if err != nil {
log.Fatalf("Error saving public key: %v\n", err)
}
fmt.Println("Generated and saved RSA keys.")
} else {
// Read the provided public key
publicKey, err = rsa.ReadPublicKey(publicKeyPath)
if err != nil {
log.Fatalf("Error reading public key: %v\n", err)
}
privateKey, publicKey, genErr := rsa.GenerateKeys(2048)
if genErr != nil {
log.Fatalf("Error generating RSA keys: %v\n", genErr)
}

privateKeyFilePath := fmt.Sprintf("%s/%s-private_key.pem", keyDir, uniqueID.String())
// Optionally save the private and public keys
err = rsa.SavePrivateKeyToFile(privateKey, privateKeyFilePath)
if err != nil {
log.Fatalf("Error saving private key: %v\n", err)
}

publicKeyFilePath := fmt.Sprintf("%s/%s-public_key.pem", keyDir, uniqueID.String())
err = rsa.SavePublicKeyToFile(publicKey, publicKeyFilePath)
if err != nil {
log.Fatalf("Error saving public key: %v\n", err)
}
fmt.Println("Generated and saved RSA keys.")
fmt.Println("Private key path:", privateKeyFilePath)
fmt.Println("Public key path:", publicKeyFilePath)

// Encrypt the file
plainText, err := utils.ReadFile(inputFile)
Expand Down

0 comments on commit b81e160

Please sign in to comment.