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

Open
wants to merge 45 commits into
base: master
Choose a base branch
from

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 (2578a93) to head (db345a0).

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        134      167      +33     
============================================
+ 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
@TharmiganK TharmiganK changed the title Add support for PGP encryption/decryption with files Add support for PGP encryption/decryption with streams Oct 9, 2024
@TharmiganK TharmiganK changed the title Add support for PGP encryption/decryption with streams [Update 11 Feature] Add support for PGP encryption/decryption with streams Oct 9, 2024
@TharmiganK TharmiganK added Skip GraalVM Check This will skip the GraalVM compatibility check and removed Skip GraalVM Check This will skip the GraalVM compatibility check labels Oct 9, 2024
docs/spec/spec.md Outdated Show resolved Hide resolved
docs/spec/spec.md Outdated Show resolved Hide resolved
# 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

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
7 participants