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

Add encrypter transfomer #35

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pajowu
Copy link

@pajowu pajowu commented Feb 16, 2025

This PR adds a Transformer that performs encryption. It can be used to create a TransformStream that encrypts the data passed to it like this:

const identity = await age.generateIdentity()
const recipient = await age.identityToRecipient(identity)
console.log(identity)
console.log(recipient)

const encryptor = new EncrypterTransformer({ recipients: [recipient)] })
const encryptionStream = new TransformStream(encryptor)

Plaintext can now be written to the WritableStream at encryptionStream.writeable and the encrypted data read from encryptionStream.readable

EncrypterTransformer currently duplicates a lot of the code from Encrypter/encryptSTREAM, as they did not offer a simple interface to implement this and I wanted to prevent larger changes for this prototype

@FiloSottile
Copy link
Owner

Oh this is interesting, thank you.

Why pass an object to the constructor instead of mirroring the addRecipient methods?

Or, even better, return the transformer from an Encrypter method?

@pajowu
Copy link
Author

pajowu commented Feb 16, 2025

short answer:
Returning it from an encryptor sounds like a great idea! I just didn't think about that option :D.

slightly longer answer:
I decided to pass it as options instead of mirroring the methods to indicate more clearly that the recipients cannot be modified once the encryption is started. Since Encryptor does all encryption at once, this is not an issue. But with a stream you could write some data, call addRecipient, add more data. Preventing this would have complicated the interface, so I decided to pass them directly to the constructor.

Having a method on Encryptor that creates a transformer with the currently set options (recipients etc) would solve this.

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