aead-stream
v0.1.0
Published
Authenticated encryption on arbitrary large files
Maintainers
Readme
aead-stream
Authenticated encryption on arbitrary large files
aead-stream is a simple API to perform authenticated symmetric encrytion on data of arbitrary size.
Install
npm install aead-streamUsage
encrypt(key, plaintext[, options])
key<Uint8Array> the symmetric key used to encrypt the dataplaintext<AsyncIterable<Uint8Array>> a stream of Uint8Arrays with the plaintext dataoptions<Object> Default: default optionsalgorithm<string> a valid algorithm forcrypto.createCipherivnonceLength<number> the length of generated nonces in bytesauthTagLength<number> the length of the authentication tagchunkSize<number> the size of a ciphertext chunk (including nonce and authentication tag)associatedData<Uint8Array> optional additional data to be used for authentication tag calculation
- Returns: <AsyncIterable<Uint8Array>> a stream of Uint8Arrays with the ciphertext data
const { encrypt } = require("aead-stream");
const { createReadStream } = require("fs");
/**
* @param {Uint8Array} key 256 bit key material
* @param {string} filepath a file path
*/
async function encyptFile(key, filepath) {
const readStream = createReadStream(filepath);
for await (const encryptedChunk of encrypt(key, readStream)) {
// store encryptedChunk - it is a Uint8Array with at most 64K size
}
}decrypt(key, ciphertext[, options])
key<Uint8Array> the symmetric key used to encrypt the dataciphertext<AsyncIterable<Uint8Array>> a stream of Uint8Arrays with the ciphertext dataoptions<Object> Default: default optionsalgorithm<string> a valid algorithm forcrypto.createCipherivnonceLength<number> the length of nonces in bytesauthTagLength<number> the length of the authentication tagchunkSize<number> the size of a ciphertext chunk (including nonce and authentication tag)associatedData<Uint8Array> optional additional data to be used for authentication tag calculation
- Returns: <AsyncIterable<Uint8Array>> a stream of Uint8Arrays with the plaintext data
const { decrypt } = require("aead-stream");
const { createReadStream } = require("fs");
/**
* @param {Uint8Array} key 256 bit key material
* @param {string} filepath a file path to an encrypted file
*/
async function decyptFile(key, filepath) {
const readStream = createReadStream(filepath);
for await (const plain of decrypt(key, readStream)) {
// process plain - a Uint8Array
}
}Default options
algorithm"chacha20-poly1305",nonceLength12,authTagLength16,chunkSize64 * 1024 (64K)
