UNPKG

web-enc-at-rest

Version:
40 lines (39 loc) 2.15 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.decryptAppData = exports.encryptAppData = void 0; const protectedCrypto_1 = require("./protectedCrypto"); const randomUtil_1 = require("./randomUtil"); const AES_GCM_IV_LENGTH = 12; function encryptAppData(credentialKey, data) { return __awaiter(this, void 0, void 0, function* () { const subtle = (0, protectedCrypto_1.getSubtle)(); const iv = (0, randomUtil_1.randomBytes)(AES_GCM_IV_LENGTH); const algorithmParams = { name: 'AES-GCM', iv }; const cipherText = yield subtle.encrypt(algorithmParams, credentialKey, data); const fullMessage = new Uint8Array(AES_GCM_IV_LENGTH + cipherText.byteLength); fullMessage.set(iv); fullMessage.set(new Uint8Array(cipherText), AES_GCM_IV_LENGTH); return fullMessage; }); } exports.encryptAppData = encryptAppData; function decryptAppData(credentialKey, ivPlusEncryptedData) { return __awaiter(this, void 0, void 0, function* () { const subtle = (0, protectedCrypto_1.getSubtle)(); const iv = ivPlusEncryptedData.slice(0, AES_GCM_IV_LENGTH); const cipherText = ivPlusEncryptedData.slice(AES_GCM_IV_LENGTH); const algorithmParams = { name: 'AES-GCM', iv }; const plainText = yield subtle.decrypt(algorithmParams, credentialKey, cipherText); return new Uint8Array(plainText); }); } exports.decryptAppData = decryptAppData;