UNPKG

6.39 kBSource Map (JSON)View Raw
1{"version":3,"file":"aes-gcm.cjs","sources":["../crypto/aes-gcm.js"],"sourcesContent":["/**\n * AES-GCM is a symmetric key for encryption\n */\n\nimport * as encoding from '../encoding.js'\nimport * as decoding from '../decoding.js'\nimport * as webcrypto from 'lib0/webcrypto'\nimport * as string from '../string.js'\nexport { exportKeyJwk, exportKeyRaw } from './common.js'\n\n/**\n * @typedef {Array<'encrypt'|'decrypt'>} Usages\n */\n\n/**\n * @type {Usages}\n */\nconst defaultUsages = ['encrypt', 'decrypt']\n\n/**\n * @param {CryptoKey} key\n * @param {Uint8Array} data\n */\nexport const encrypt = (key, data) => {\n const iv = webcrypto.getRandomValues(new Uint8Array(16)) // 92bit is enough. 128bit is recommended if space is not an issue.\n return webcrypto.subtle.encrypt(\n {\n name: 'AES-GCM',\n iv\n },\n key,\n data\n ).then(cipher => {\n const encryptedDataEncoder = encoding.createEncoder()\n // iv may be sent in the clear to the other peers\n encoding.writeUint8Array(encryptedDataEncoder, iv)\n encoding.writeVarUint8Array(encryptedDataEncoder, new Uint8Array(cipher))\n return encoding.toUint8Array(encryptedDataEncoder)\n })\n}\n\n/**\n * @experimental The API is not final!\n *\n * Decrypt some data using AES-GCM method.\n *\n * @param {CryptoKey} key\n * @param {Uint8Array} data\n * @return {PromiseLike<Uint8Array>} decrypted buffer\n */\nexport const decrypt = (key, data) => {\n const dataDecoder = decoding.createDecoder(data)\n const iv = decoding.readUint8Array(dataDecoder, 16)\n const cipher = decoding.readVarUint8Array(dataDecoder)\n return webcrypto.subtle.decrypt(\n {\n name: 'AES-GCM',\n iv\n },\n key,\n cipher\n ).then(data => new Uint8Array(data))\n}\n\nconst aesAlgDef = {\n name: 'AES-GCM',\n length: 256\n}\n\n/**\n * @param {any} jwk\n * @param {Object} opts\n * @param {Usages} [opts.usages]\n * @param {boolean} [opts.extractable]\n */\nexport const importKeyJwk = (jwk, { usages, extractable = false } = {}) => {\n if (usages == null) {\n /* c8 ignore next */\n usages = jwk.key_ops || defaultUsages\n }\n return webcrypto.subtle.importKey('jwk', jwk, 'AES-GCM', extractable, /** @type {Usages} */ (usages))\n}\n\n/**\n * Only suited for importing public keys.\n *\n * @param {Uint8Array} raw\n * @param {Object} opts\n * @param {Usages} [opts.usages]\n * @param {boolean} [opts.extractable]\n */\nexport const importKeyRaw = (raw, { usages = defaultUsages, extractable = false } = {}) =>\n webcrypto.subtle.importKey('raw', raw, aesAlgDef, extractable, /** @type {Usages} */ (usages))\n\n/**\n * @param {Uint8Array | string} data\n */\n/* c8 ignore next */\nconst toBinary = data => typeof data === 'string' ? string.encodeUtf8(data) : data\n\n/**\n * @experimental The API is not final!\n *\n * Derive an symmetric key using the Password-Based-Key-Derivation-Function-2.\n *\n * @param {Uint8Array|string} secret\n * @param {Uint8Array|string} salt\n * @param {Object} opts\n * @param {boolean} [opts.extractable]\n * @param {Usages} [opts.usages]\n */\nexport const deriveKey = (secret, salt, { extractable = false, usages = defaultUsages } = {}) =>\n webcrypto.subtle.importKey(\n 'raw',\n toBinary(secret),\n 'PBKDF2',\n false,\n ['deriveKey']\n ).then(keyMaterial =>\n webcrypto.subtle.deriveKey(\n {\n name: 'PBKDF2',\n salt: toBinary(salt), // NIST recommends at least 64 bits\n iterations: 600000, // OWASP recommends 600k iterations\n hash: 'SHA-256'\n },\n keyMaterial,\n aesAlgDef,\n extractable,\n usages\n )\n )\n"],"names":["webcrypto","encoding.createEncoder","encoding.writeUint8Array","encoding.writeVarUint8Array","encoding.toUint8Array","decoding.createDecoder","decoding.readUint8Array","decoding.readVarUint8Array","string.encodeUtf8"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AACA;AACA;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,aAAa,GAAG,CAAC,SAAS,EAAE,SAAS,EAAC;AAC5C;AACA;AACA;AACA;AACA;AACY,MAAC,OAAO,GAAG,CAAC,GAAG,EAAE,IAAI,KAAK;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