@@ -155,22 +155,63 @@ func encryptFile(filename string) error {
155
155
if err != nil {
156
156
return fmt .Errorf ("error encrypting file: %w" , err )
157
157
}
158
-
159
158
fmt .Printf ("File encrypted successfully: %s.gpg\n " , filename )
159
+
160
+ // Use askUserYN to ask if the user wants to upload to IPFS
161
+ if askUserYN ("Do you want to upload the encrypted file to IPFS?" ) {
162
+ // Call ipfsUpload function with the encrypted file
163
+ if err := ipfsUpload (filename + ".gpg" ); err != nil {
164
+ return fmt .Errorf ("error uploading file to IPFS: %w" , err )
165
+ }
166
+ }
167
+
168
+ return nil
169
+ }
170
+
171
+ func saveCID (cid string ) error {
172
+ // Write CID to log_CID.log
173
+ f , err := os .OpenFile ("log_CID.log" , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
174
+ if err != nil {
175
+ return err
176
+ }
177
+ defer f .Close ()
178
+
179
+ if _ , err := f .WriteString (cid + "\n " ); err != nil {
180
+ return err
181
+ }
182
+
160
183
return nil
161
184
}
162
185
186
+
187
+ func ipfsUpload (filePath string ) error {
188
+ // Call AddFileToIPFS function
189
+ cid , err := ipfs_link .AddFileToIPFS (filePath )
190
+ if err != nil {
191
+ return err
192
+ }
193
+
194
+ // Call saveCID function with the returned CID
195
+ if err := saveCID (cid ); err != nil {
196
+ return fmt .Errorf ("error saving CID: %w" , err )
197
+ }
198
+
199
+ fmt .Println ("File uploaded to IPFS successfully, CID:" , cid )
200
+ return nil
201
+ }
202
+
203
+
163
204
func decryptFile (filename string ) error {
164
205
// Ask user for CID
165
206
cid , err := generalAskUser ("Enter the CID for the file to decrypt: " )
166
207
if err != nil {
167
208
return fmt .Errorf ("error reading CID: %w" , err )
168
209
}
169
210
170
- outputPath := "./" // Set the output path, adjust if necessary
211
+ ipfsFilePath := "retrieved_" + filename // Save the file retrieved from IPFS with a prefixed name
171
212
172
- // Retrieve the file from IPFS using ipfs_link library
173
- err = ipfs_link .GetFileFromIPFS (cid , outputPath )
213
+ // Retrieve the file from IPFS
214
+ err = ipfs_link .GetFileFromIPFS (cid , ipfsFilePath )
174
215
if err != nil {
175
216
return fmt .Errorf ("error retrieving file from IPFS: %w" , err )
176
217
}
@@ -186,15 +227,14 @@ func decryptFile(filename string) error {
186
227
if err != nil {
187
228
return fmt .Errorf ("error reading passphrase: %w" , err )
188
229
}
189
-
190
230
// Generate the symmetric key
191
231
seedKDF := publicKey + passphrase
192
232
kdfKey := sha256 .Sum256 ([]byte (seedKDF ))
193
233
decryptedKey := fmt .Sprintf ("%x" , kdfKey )
194
234
195
- // Decrypt the file using GPG and the derived key
196
- decryptedFilePath := outputPath // Adjust as needed
197
- cmd := exec .Command ("gpg" , "--decrypt" , "--batch" , "--passphrase" , decryptedKey , "--output" , decryptedFilePath , outputPath )
235
+ // Decrypt the file using GPG
236
+ decryptedFilePath := "decrypted_" + filename // This is the path where the decrypted file will be saved
237
+ cmd := exec .Command ("gpg" , "--decrypt" , "--batch" , "--passphrase" , decryptedKey , "--output" , decryptedFilePath , ipfsFilePath )
198
238
cmd .Stdout = os .Stdout
199
239
cmd .Stderr = os .Stderr
200
240
err = cmd .Run ()
@@ -206,6 +246,3 @@ func decryptFile(filename string) error {
206
246
return nil
207
247
}
208
248
209
-
210
-
211
-
0 commit comments