Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Update 11 Feature] Add support for PGP encryption/decryption with streams #567

Merged
merged 47 commits into from
Nov 18, 2024

Conversation

TharmiganK
Copy link
Contributor

@TharmiganK TharmiganK commented Oct 2, 2024

Purpose

$Subject

Fixes: ballerina-platform/ballerina-library#7064
Part of: ballerina-platform/ballerina-library#7252

With this PR, two new functionalities are added to the crypto module:

  • encryptStreamAsPgp: Read the content from a stream, encrypt with PGP and return a encrypted stream
  • decryptStreamFromPgp: Read the content from an PGP encrypted stream, decrypt with PGP and return a decrypted stream

With this new APIs, large files can be encrypted/decrypted with PGP by loading them as streams rather than loading the entire content into the memory.

Find the following results with large files(the files are generated with random ASCII characters):

File Size Encryption duration Encryption max memory usage Decryption(from the encrypted file) duration Decryption max memory usage
1 GB ~1 min ~100 MB ~30 s ~125 MB
5 GB ~5 min ~100 MB ~2 min 20 s ~75MB

Examples

  • Encryption sample:
stream<byte[], error?> inputStream = check io:fileReadBlocksAsStream("input.txt");
stream<byte[], crypto:Error?>|crypto:Error encryptedStream = crypto:encryptStreamAsPgp(inputStream, "public_key.asc");
  • Decryption sample:
byte[] passphrase = check io:fileReadBytes("pass_phrase.txt");
stream<byte[], error?> inputStream = check io:fileReadBlocksAsStream("pgb_encrypted.txt");
stream<byte[], crypto:Error?>|crypto:Error decryptedStream = crypto:decryptStreamFromPgp(inputStream, "private_key.asc", passphrase);

Checklist

  • Linked to an issue
  • Updated the changelog
  • Added tests
  • Updated the spec
  • Checked native-image compatibility

Copy link

codecov bot commented Oct 2, 2024

Codecov Report

Attention: Patch coverage is 68.09816% with 104 lines in your changes missing coverage. Please review.

Project coverage is 84.06%. Comparing base (16e5127) to head (df0e19d).
Report is 48 commits behind head on master.

Files with missing lines Patch % Lines
...allerina/stdlib/crypto/nativeimpl/StreamUtils.java 58.25% 30 Missing and 13 partials ⚠️
.../ballerina/stdlib/crypto/BallerinaInputStream.java 56.81% 13 Missing and 6 partials ⚠️
ballerina/stream_iterators.bal 65.62% 11 Missing ⚠️
...allerina/stdlib/crypto/PgpDecryptionGenerator.java 75.55% 7 Missing and 4 partials ⚠️
...allerina/stdlib/crypto/SequentialBufferedPipe.java 61.53% 8 Missing and 2 partials ⚠️
...io/ballerina/stdlib/crypto/nativeimpl/Encrypt.java 71.42% 6 Missing ⚠️
...io/ballerina/stdlib/crypto/nativeimpl/Decrypt.java 76.47% 4 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master     #567      +/-   ##
============================================
- Coverage     89.11%   84.06%   -5.06%     
- Complexity      172      205      +33     
============================================
  Files            23       27       +4     
  Lines           873     1161     +288     
  Branches        124      167      +43     
============================================
+ Hits            778      976     +198     
- Misses           71      142      +71     
- Partials         24       43      +19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@NipunaRanasinghe
Copy link
Contributor

@TharmiganK we may need to update the spec and the changelog along with the new changes

@TharmiganK TharmiganK marked this pull request as ready for review October 2, 2024 10:52
# Conflicts:
#	ballerina/Ballerina.toml
#	ballerina/Dependencies.toml
#	ballerina/encrypt_decrypt.bal
Bhashinee
Bhashinee previously approved these changes Oct 14, 2024
Copy link

sonarcloud bot commented Oct 14, 2024

MohamedSabthar
MohamedSabthar previously approved these changes Oct 18, 2024
@TharmiganK
Copy link
Contributor Author

Closing this PR since this requires Java 21 changes to the master. The feature will be available in the pgp-files branch and a PR will be sent to the master once the Java 21 changes are added to the master

Copy link

sonarcloud bot commented Nov 18, 2024

@TharmiganK TharmiganK merged commit 40658a2 into master Nov 18, 2024
7 of 8 checks passed
@TharmiganK TharmiganK deleted the pgp-files branch November 18, 2024 11:10
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.

Introduce PGP encryption/decryption APIs to handle files
5 participants