@sebastianp265/safe-server-side-storage-client
Version:
Library for Confidential Server-Side Message Storage Using the Labyrinth Protocol
23 lines (22 loc) • 959 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NONCE_LENGTH = void 0;
exports.encryptWithRandomNonce = encryptWithRandomNonce;
exports.encrypt = encrypt;
exports.decrypt = decrypt;
const aes_1 = require("@noble/ciphers/aes");
const utils_1 = require("@noble/ciphers/utils");
const utils_2 = require("./utils");
exports.NONCE_LENGTH = 12;
function encryptWithRandomNonce(key, aad, plaintext) {
const nonce = (0, utils_2.random)(exports.NONCE_LENGTH);
return encrypt(key, nonce, aad, plaintext);
}
function encrypt(key, nonce, aad, plaintext) {
(0, utils_2.cryptoAssert)(nonce.length === exports.NONCE_LENGTH);
return (0, utils_1.concatBytes)(nonce, (0, aes_1.gcm)(key, nonce, aad).encrypt(plaintext));
}
function decrypt(key, aad, ciphertext) {
const nonce = ciphertext.subarray(0, exports.NONCE_LENGTH);
return (0, aes_1.gcm)(key, nonce, aad).decrypt(ciphertext.subarray(exports.NONCE_LENGTH));
}