Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@smorrell
Copy link
Contributor

@smorrell smorrell commented Jun 2, 2025

In a previous version the code called lData.Close() but that is marked as obsolete. The Obsolete attribute says it all.
[Obsolete("Dispose any opened Stream directly")]

https://github.com/bcgit/bc-csharp/blob/5095a56b7d23ec370e58d45c6445d17ca85f94f6/crypto/src/bcpg/BcpgOutputStream.cs#L66
If you look here you can see how once a stream length exceeds 4gigs (0xFFFFFFFFL) it starts using a different approach that includes the partialBuffer.

The Close() used to call the PartialFlush() which would write out the contents of the partialBuffer.
With that gone, there is still some bytes in the partialBuffer.

Finish() calls PartialFlush.
Finish is called in the Dispose method

In a previous version the code called lData.Close() but that is marked as obsolete.
The Obsolete attribute says it all.
[Obsolete("Dispose any opened Stream directly")]
@smorrell smorrell force-pushed the bug/304-Missing-Bytes-After-Encrypt-and-Decrypt-Large-files branch from 813442d to 78dfccb Compare June 2, 2025 11:36
@smorrell smorrell marked this pull request as draft June 2, 2025 11:37
@smorrell smorrell marked this pull request as ready for review June 2, 2025 11:59
@mattosaurus
Copy link
Owner

Thanks for this. Looks good to me but seems that the workflows have started failing, probably some of the actions are out-of-date.

I'll take a look at them this evening and then try and put a new release.

@smorrell
Copy link
Contributor Author

smorrell commented Jun 2, 2025

FYI - take it or leave it, I just wanted to share what I observed

Armoring
In my tinkering around I noticed that armoring is the default and it doubles the time it takes in my test program to encrypt a file and the resulting encrypted file is at least 1.5 times as big.
For large files, I dont think armoring makes sense because I dont think it is meant to be emailed etc.
Armoring is meant to convert the encrypted data into a format that wont get messed with during emailing and other processes where legacy systems may not be able to handle anything other than ASCII.
Or at least that is my understanding.

BufferSize of 4096
A buffer size of 4096 is being used in PipeStreamContentsAsync
Using Stream.CopyTo seems to use a buffer size of 80k and cuts the encryption time in half as well.

@mattosaurus mattosaurus merged commit f3eaee8 into mattosaurus:master Jun 2, 2025
3 of 6 checks passed
@mattosaurus
Copy link
Owner

Thanks for looking into the performance. That's a good catch on the PipeStreamContentsAsync, I can't remember why I was using this rather than Stream.CopyTo but I'll create another PR to switch over to using Stream.CopyTo given the performance improvements.

I hadn't realised that armouring had such a big impact. I think it's probably too disruptive to change the default at the moment though I'll update the readme with your findings so people are aware that they can increase performance if they don't use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants