Stream received data into encrypted file

This commit is contained in:
clerie 2024-11-24 18:06:27 +01:00
parent 9ec8374721
commit 9e0474a68f

35
main.go
View File

@ -3,7 +3,6 @@ package main
import (
"errors"
"io"
"io/ioutil"
"os"
"strings"
"time"
@ -99,40 +98,42 @@ func (driver *ForwarderDriver) GetFile(path string, offset int64) (int64, io.Rea
}
func (driver *ForwarderDriver) PutFile(destPath string, data io.Reader, appendData bool) (int64, error) {
log.Printf("Receiving %v", destPath)
log.Printf("Receiving file %v", destPath)
log.Printf("ReadAll…")
contents, err := ioutil.ReadAll(data)
log.Printf("Opening output file")
encryptedFile, err := os.Create("file.pdf.asc")
if err != nil {
log.Printf("Error: %v", err)
return 0, err
}
log.Printf("crypto.PGP…")
log.Printf("Preparing GPG encryption handle")
pgp := crypto.PGP()
encHandle, err := pgp.Encryption().Recipient(driver.RecipientKey).New()
if err != nil {
log.Printf("Error: %v", err)
return 0, err
}
log.Printf("Encrypt…")
pgpMessage, err := encHandle.Encrypt(contents)
if err != nil {
log.Printf("Error: %v", err)
return 0, err
}
log.Printf("ArmorBytes…")
armored, err := pgpMessage.ArmorBytes()
log.Printf("Preparing encrypting writer")
encryptingWriter, err := encHandle.EncryptingWriter(encryptedFile, crypto.Armor)
if err != nil {
log.Printf("Error: %v", err)
return 0, err
}
log.Printf("WriteFile…")
os.WriteFile("file.txt", armored, 0666)
log.Printf("Read, encrypt and write data")
transferred_bytes, err := io.Copy(encryptingWriter, data)
if err != nil {
log.Printf("Error: %v", err)
return 0, err
}
log.Printf("Status: %v, Transferred bytes: %v, errmes: %v", 0, len(contents), err)
return int64(len(contents)), err
encryptingWriter.Close()
encryptedFile.Close()
log.Printf("Transferred bytes: %v", transferred_bytes)
return transferred_bytes, err
}
type ForwarderDriverFactory struct {