Skip to content

Commit bac5605

Browse files
authored
Add files via upload
1 parent 57c5e3b commit bac5605

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

IPFSS_IPFS-Secure

1.82 KB
Binary file not shown.

keycard_link/keycard_link.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
// GetKeycardPublicKey retrieves the public key from the keycard.
1313
func GetKeycardPublicKey() (string, error) {
1414
// Command to execute
15+
fmt.Print("Scan your card now! :) \n")
1516
cmd := exec.Command("./keycard-linux-amd64", "info")
1617

1718
// Capture the output of the command
@@ -46,7 +47,7 @@ func GetKeycardPublicKey() (string, error) {
4647

4748
// ReadPassphrase prompts the user to enter a passphrase.
4849
func ReadPassphrase() (string, error) {
49-
fmt.Print("Enter a unique passphrase for this file upload: ")
50+
fmt.Print("Enter a unique passphrase for this particular file: ")
5051
reader := bufio.NewReader(os.Stdin)
5152
passphrase, err := reader.ReadString('\n')
5253
if err != nil {

main.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,22 +155,63 @@ func encryptFile(filename string) error {
155155
if err != nil {
156156
return fmt.Errorf("error encrypting file: %w", err)
157157
}
158-
159158
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+
160183
return nil
161184
}
162185

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+
163204
func decryptFile(filename string) error {
164205
// Ask user for CID
165206
cid, err := generalAskUser("Enter the CID for the file to decrypt: ")
166207
if err != nil {
167208
return fmt.Errorf("error reading CID: %w", err)
168209
}
169210

170-
outputPath := "./" // Set the output path, adjust if necessary
211+
ipfsFilePath := "retrieved_" + filename // Save the file retrieved from IPFS with a prefixed name
171212

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)
174215
if err != nil {
175216
return fmt.Errorf("error retrieving file from IPFS: %w", err)
176217
}
@@ -186,15 +227,14 @@ func decryptFile(filename string) error {
186227
if err != nil {
187228
return fmt.Errorf("error reading passphrase: %w", err)
188229
}
189-
190230
// Generate the symmetric key
191231
seedKDF := publicKey + passphrase
192232
kdfKey := sha256.Sum256([]byte(seedKDF))
193233
decryptedKey := fmt.Sprintf("%x", kdfKey)
194234

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)
198238
cmd.Stdout = os.Stdout
199239
cmd.Stderr = os.Stderr
200240
err = cmd.Run()
@@ -206,6 +246,3 @@ func decryptFile(filename string) error {
206246
return nil
207247
}
208248

209-
210-
211-

0 commit comments

Comments
 (0)