Skip to content

Commit

Permalink
corrected some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdiooa committed Mar 14, 2024
1 parent 06c9efe commit 514ea91
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 45 deletions.
7 changes: 0 additions & 7 deletions cmd/zeroward/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ var decryptCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
filePath, _ := cmd.Flags().GetString("filePath")

// dekkeyPath, _ := cmd.Flags().GetString("dekkey")

kekk := viper.GetString("KEKkey")

kekBytes, err := hex.DecodeString(kekk)
Expand Down Expand Up @@ -56,15 +54,10 @@ var decryptCmd = &cobra.Command{
}

}
// if err := os.Remove(dekkeyPath); err != nil {
// fmt.Println("Error:", err)
// return
// }
},
}

func init() {
rootCmd.AddCommand(decryptCmd)

// decryptCmd.Flags().StringP("dekkey", "k", "", "DEK Key to decrypt the file please!")
}
7 changes: 1 addition & 6 deletions cmd/zeroward/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,11 @@ var encryptCmd = &cobra.Command{

cobra.CheckErr(err)
if filePath != "" {
if err := encryption.EncryptFile(filePath, dek); err != nil {
if err := encryption.EncryptFile(filePath, dek, kekBytes); err != nil {
fmt.Println("Error encrypting File:", err)
return
}
}
if err := encryption.EncryptKey(dek, kekBytes, filePath); err != nil {
fmt.Println("Error encrypting DEK:", err)
return
}

},
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/zeroward/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ func handleEncryptionAndUpload(accessKeyID, secretAccessKey, bcktName, filePath,
if err != nil {
return fmt.Errorf("error generating DEK: %v", err)
}

if filePath != "" {
if err := encryption.EncryptFile(filePath, dek); err != nil {
if err := encryption.EncryptFile(filePath, dek, kekk); err != nil {
return fmt.Errorf("error encrypting file: %v", err)
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/zeroward/downloading/downloadobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ func DownloadObject(awsRegion, accessKeyId, accessKeySecret, bucketName, localFi
if err != nil {
return fmt.Errorf("error decrypting DEK key: %v", err)
}

keySize := 60
encryptedBody = encryptedBody[:len(encryptedBody)-keySize]
body, err := decryption.DecryptFile(encryptedBody, dekkey)
if err != nil {
return fmt.Errorf("error decrypting file body: %v", err)
Expand Down
38 changes: 9 additions & 29 deletions pkg/zeroward/encryption/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,7 @@ import (
"os"
)

func EncryptKey(dek []byte, kek []byte, filePath string) error {
encryptedData, err := EncryptData(dek, kek)
if err != nil {
return err
}
// outputDEKDir := filepath.Dir(filePath)
// outputDEKFilePath := filepath.Join(outputDEKDir, "DEK.key.enc")
outputFilePath := filePath + ".enc"
file, err := os.OpenFile(outputFilePath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
// file, err := os.Create(outputDEKFilePath)
// if err != nil {
// return err
// }
// defer file.Close()

_, err = file.Write(encryptedData)
if err != nil {
return err
}

return nil
}

func EncryptFile(filePath string, dek []byte) error {
func EncryptFile(filePath string, dek []byte, kek []byte) error {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return err
}
Expand Down Expand Up @@ -92,7 +65,14 @@ func EncryptFile(filePath string, dek []byte) error {
if err != nil {
return err
}

encryptedKey, err := EncryptData(dek, kek)
if err != nil {
return err
}
_, err = dstFile.Write(encryptedKey)
if err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit 514ea91

Please sign in to comment.