aead-stream
Version:
Authenticated encryption on arbitrary large files
30 lines (29 loc) • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decrypt = void 0;
const chunkify_1 = require("./chunkify");
const crypto_1 = require("crypto");
const options_1 = require("./options");
async function* decrypt(key, ciphertext, options = options_1.defaultOptions) {
const { algorithm, authTagLength, nonceLength, associatedData, chunkSize, } = options;
let chunkIndex = 0;
for await (const chunk of (0, chunkify_1.chunkify)(chunkSize, ciphertext)) {
const nonce = chunk.slice(0, nonceLength);
const authTag = chunk.slice(-authTagLength);
let cipher = (0, crypto_1.createDecipheriv)(algorithm, key, nonce, {
authTagLength,
});
let aad = Buffer.from([chunkIndex++]);
if (associatedData) {
aad = Buffer.concat([aad, associatedData]);
}
cipher.setAAD(aad, {
plaintextLength: chunk.length,
});
cipher.setAuthTag(authTag);
const plaintext = cipher.update(chunk.slice(nonceLength, chunk.length - authTagLength));
cipher.final();
yield plaintext;
}
}
exports.decrypt = decrypt;