typage icon indicating copy to clipboard operation
typage copied to clipboard

Add encrypter transfomer

Open pajowu opened this issue 10 months ago • 2 comments

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

pajowu avatar Feb 16 '25 11:02 pajowu

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?

FiloSottile avatar Feb 16 '25 11:02 FiloSottile

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.

pajowu avatar Feb 16 '25 12:02 pajowu