UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

74 lines (73 loc) • 2.55 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var web_exports = {}; __export(web_exports, { webAesEngine: () => webAesEngine, webDigestSource: () => webDigestSource }); module.exports = __toCommonJS(web_exports); function subtle(verb) { const found = globalThis.crypto?.subtle; if (!found) { throw new Error(`${verb} needs a browser with WebCrypto or a native build that includes one`); } return found; } const webDigestSource = async (algorithm, buffer, offset, length) => { if (algorithm === "MD5") { throw new Error("Crypto.digest: MD5 needs an iOS or Android build"); } return subtle("Crypto.digest").digest(algorithm, new Uint8Array(buffer, offset, length)); }; async function importKey(raw) { return subtle("Crypto.aesEncrypt").importKey("raw", raw, "AES-GCM", false, ["encrypt", "decrypt"]); } const webAesEngine = { async seal(key, iv, plaintext, additionalData, tagLength) { const sealed = await subtle("Crypto.aesEncrypt").encrypt({ name: "AES-GCM", iv, tagLength: tagLength * 8, ...additionalData ? { additionalData } : {} }, await importKey(key), plaintext); return new Uint8Array(sealed); }, async open(key, iv, ciphertextWithTag, additionalData, tagLength) { try { const opened = await subtle("Crypto.aesDecrypt").decrypt({ name: "AES-GCM", iv, tagLength: tagLength * 8, ...additionalData ? { additionalData } : {} }, await importKey(key), ciphertextWithTag); return new Uint8Array(opened); } catch { throw new Error("Crypto.aesDecrypt: authentication failed"); } }, random(bytes) { const getRandomValues = globalThis.crypto?.getRandomValues; if (typeof getRandomValues !== "function") { throw new Error("secure random: web crypto is unavailable in this browser."); } getRandomValues.call(globalThis.crypto, bytes); } };