Let decryption happen in a seperate goroutine to hopefully speed it up

This commit is contained in:
Joren Schipman 2024-05-02 16:41:50 +02:00
parent 7235048705
commit 736310d469
Signed by untrusted user who does not match committer: Joren
GPG Key ID: 280E33DFBC0F1B55

View File

@ -104,7 +104,7 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
// Receive and handle multiple files
for {
var plaintext []byte
var chunks []string
for {
chunk, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
@ -115,23 +115,14 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
fmt.Printf("Received file\n")
break
}
ciphertext, err := base64.StdEncoding.DecodeString(strings.TrimSpace(chunk))
if err != nil {
log.Printf("Error decoding chunk: %v", err)
return
}
plaintextChunk, err := decrypt(ciphertext, key, iv)
if err != nil {
log.Printf("Error decrypting chunk: %v", err)
return
}
plaintext = append(plaintext, plaintextChunk...)
chunks = append(chunks, chunk)
conn.Write([]byte("Received and decrypted chunk\n"))
}
go handleDecrypted(plaintext, uid)
go decryptAndHandle(chunks, key, iv, uid)
conn.Write([]byte("Received and decrypted\n"))
// Check if there are more files to receive
@ -147,6 +138,25 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
}
}
func decryptAndHandle(chunks []string, key []byte, iv []byte, uid []byte){
var plaintext []byte
for _, chunk := range chunks{
ciphertext, err := base64.StdEncoding.DecodeString(strings.TrimSpace(chunk))
if err != nil {
log.Printf("Error decoding chunk: %v", err)
return
}
plaintextChunk, err := decrypt(ciphertext, key, iv)
if err != nil {
log.Printf("Error decrypting chunk: %v", err)
return
}
plaintext = append(plaintext, plaintextChunk...)
}
handleDecrypted(plaintext, uid)
}
func decrypt(cipherText []byte, key []byte, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(key)