Skip to content

Commit 963af93

Browse files
authored
Fixes and functions
Added functionality to upload / download without cryptography, (will be needed in future for clusters and predetermined keys etc to not corrupt files. Split functionality of retrieving file from decryptFile function. One function means one function. Also fixed the multilevel encryption!!! Signed-off-by: Anthony Grace <[email protected]>
1 parent 56ecb87 commit 963af93

File tree

1 file changed

+46
-26
lines changed

1 file changed

+46
-26
lines changed

main.go

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,40 @@ func menu() (string, error) {
144144
fmt.Println("\033[1;33m=============================================\033[0m")
145145
// Normal text for the question
146146
fmt.Println(" What would you like to do?")
147-
// Menu options with green arrow, green number, and normal text
148-
fmt.Println("\033[1;32m>\033[0;32m 1.\033[0m Encrypt / upload sensitive data to IPFS")
149-
fmt.Println("\033[1;32m>\033[0;32m 2.\033[0m Decrypt / pull file with CID")
150-
fmt.Println("\033[1;32m>\033[0;32m 3.\033[0m Print CID Log to QR code")
151-
fmt.Println("\033[1;32m>\033[0;32m 4.\033[0m Install Dependencies (Java, GPP)")
152-
fmt.Println("\033[1;32m>\033[0;32m 5.\033[0m Install Keycard onto Implant")
153-
fmt.Println("\033[1;32m>\033[0;32m 6.\033[0m Run Connection Test to IPFS")
154-
fmt.Println("\033[1;32m>\033[0;32m 7.\033[0m Exit the Program")
147+
148+
fmt.Println("\033[1;32m>\033[0;32m 1.\033[0m Encrypt and Upload")
149+
fmt.Println("\033[1;32m>\033[0;32m 2.\033[0m Decrypt and Download")
150+
fmt.Println("\033[1;32m>\033[0;32m 3.\033[0m Just Download from IPFS")
151+
fmt.Println("\033[1;32m>\033[0;32m 4.\033[0m Just Upload to IPFS")
152+
fmt.Println("\033[1;32m>\033[0;32m 5.\033[0m Print CID Log to QR code")
153+
fmt.Println("\033[1;32m>\033[0;32m 6.\033[0m Install Dependencies (Java, GPP)")
154+
fmt.Println("\033[1;32m>\033[0;32m 7.\033[0m Install Keycard onto Implant")
155+
fmt.Println("\033[1;32m>\033[0;32m 8.\033[0m Run Connection Test to IPFS")
156+
fmt.Println("\033[1;32m>\033[0;32m 9.\033[0m Exit the Program")
157+
// Yellow lines
155158
// Yellow lines
156159
fmt.Println("\033[1;33m=============================================\033[0m")
157160

158161
return generalAskUser("Enter your choice: ")
159162
}
160163

164+
func encryptAndUploadFile(filename string, level string) error {
165+
if err := encryptFile(filename); err != nil {
166+
return err
167+
}
168+
encryptedFilename := filename + ".aes"
169+
return ipfsUpload(encryptedFilename, level)
170+
}
171+
//
172+
func decryptAndDownloadFile() error {
173+
filePath, err := ipfsDownload()
174+
if err != nil {
175+
return err
176+
}
177+
return decryptFile(filePath)
178+
}
179+
180+
161181
// runIPFSTestWithViu encapsulates the entire process.
162182
func runIPFSTestWithViu(cid string) {
163183
if err := checkAndInstallViu(); err != nil {
@@ -514,34 +534,34 @@ func ipfsUpload(filePath string, level string) error {
514534
}
515535

516536

517-
func ipfsDownload(){
518-
//Split, include logic to download a file from IPFS
519-
//Enter CID, etc...
520537

521-
}
522-
523-
func decryptFile(filename string) error {
524-
cid, err := generalAskUser("Enter the CID for the file to decrypt: ")
538+
func ipfsDownload() (string, error) {
539+
cid, err := generalAskUser("Enter the CID of the file to download from IPFS: ")
525540
if err != nil {
526-
return fmt.Errorf("\033[1;31merror reading CID: %w\033[0m", err)
541+
return "", fmt.Errorf("\033[1;31merror reading CID: %w\033[0m", err)
527542
}
528543

529-
ipfsFilePath := "retrieved_" + filename
544+
savePath, err := generalAskUser("Enter the path to save the downloaded file: ")
545+
if err != nil {
546+
return "", fmt.Errorf("\033[1;31merror reading save path: %w\033[0m", err)
547+
}
530548

531-
err = ipfs_link.GetFileFromIPFS(cid, ipfsFilePath)
549+
err = ipfs_link.GetFileFromIPFS(cid, savePath)
532550
if err != nil {
533-
return fmt.Errorf("\033[1;31merror retrieving file from IPFS: %w\033[0m", err)
551+
return "", fmt.Errorf("\033[1;31merror downloading file from IPFS: %w\033[0m", err)
534552
}
535553

536-
level, err := generalAskUser("Enter the encryption level used (easy, medium, hard): ")
554+
fmt.Printf("\033[1;32mFile downloaded successfully: %s\033[0m\n", savePath)
555+
return savePath, nil
556+
}
557+
558+
func decryptFile(filePath string) error {
559+
level, err := generalAskUser("Enter the encryption level used on the file (easy, medium, hard): ")
537560
if err != nil {
538561
return fmt.Errorf("\033[1;31merror reading encryption level: %w\033[0m", err)
539562
}
540563

541-
var combinedKey string
542-
var passphrase string
543-
var saltPhrase string
544-
564+
var combinedKey, passphrase, saltPhrase string
545565
if level == "medium" || level == "hard" {
546566
passphrase, err = generalAskUser("Enter your passphrase for the file: ")
547567
if err != nil {
@@ -570,7 +590,7 @@ func decryptFile(filename string) error {
570590
iterationCount := 1000000
571591
aesKey := pbkdf2.Key(combinedKeyBytes, saltBytes, iterationCount, 32, sha256.New)
572592

573-
encryptedData, err := os.ReadFile(ipfsFilePath)
593+
encryptedData, err := os.ReadFile(filePath)
574594
if err != nil {
575595
return fmt.Errorf("\033[1;31merror reading encrypted file: %w\033[0m", err)
576596
}
@@ -581,7 +601,7 @@ func decryptFile(filename string) error {
581601
return fmt.Errorf("\033[1;31merror decrypting file: %w\033[0m", err)
582602
}
583603

584-
decryptedFilePath := "decrypted_" + cid
604+
decryptedFilePath := "decrypted_" + filePath
585605
err = os.WriteFile(decryptedFilePath, decryptedData, 0644)
586606
if err != nil {
587607
return fmt.Errorf("\033[1;31merror writing decrypted file: %w\033[0m", err)

0 commit comments

Comments
 (0)