UNPKG

@trap_stevo/legendarybuilderproreact-ui

Version:

The legendary UI & utility API that makes your application a legendary application. ~ Created by Steven Compton

113 lines 4.16 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime/regenerator"; export function getCryptoKey(_x, _x2, _x3) { return _getCryptoKey.apply(this, arguments); } function _getCryptoKey() { _getCryptoKey = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(seed, offset, depth) { var enc, keyMaterial; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: enc = new TextEncoder(); _context.next = 3; return crypto.subtle.importKey("raw", enc.encode(seed), "PBKDF2", false, ["deriveKey"]); case 3: keyMaterial = _context.sent; return _context.abrupt("return", crypto.subtle.deriveKey({ name: "PBKDF2", salt: enc.encode(offset), iterations: depth, hash: "SHA-256" }, keyMaterial, { name: "AES-GCM", length: 256 }, false, ["encrypt", "decrypt"])); case 5: case "end": return _context.stop(); } }, _callee); })); return _getCryptoKey.apply(this, arguments); } ; export function encryptData(_x4, _x5, _x6, _x7) { return _encryptData.apply(this, arguments); } function _encryptData() { _encryptData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(data, seed, offset, depth) { var key, iv, encoded, ciphertext, full; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return getCryptoKey(seed, offset, depth); case 2: key = _context2.sent; iv = crypto.getRandomValues(new Uint8Array(12)); encoded = new TextEncoder().encode(JSON.stringify(data)); _context2.next = 7; return crypto.subtle.encrypt({ name: "AES-GCM", iv: iv }, key, encoded); case 7: ciphertext = _context2.sent; full = new Uint8Array(iv.length + ciphertext.byteLength); full.set(iv); full.set(new Uint8Array(ciphertext), iv.length); return _context2.abrupt("return", btoa(String.fromCharCode.apply(String, _toConsumableArray(full)))); case 12: case "end": return _context2.stop(); } }, _callee2); })); return _encryptData.apply(this, arguments); } ; export function decryptData(_x8, _x9, _x10, _x11) { return _decryptData.apply(this, arguments); } function _decryptData() { _decryptData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(base64, seed, offset, depth) { var binary, bytes, iv, ciphertext, key, decrypted; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: binary = atob(base64); bytes = Uint8Array.from(binary, function (_char) { return _char.charCodeAt(0); }); iv = bytes.slice(0, 12); ciphertext = bytes.slice(12); _context3.next = 6; return getCryptoKey(seed, offset, depth); case 6: key = _context3.sent; _context3.next = 9; return crypto.subtle.decrypt({ name: "AES-GCM", iv: iv }, key, ciphertext); case 9: decrypted = _context3.sent; return _context3.abrupt("return", JSON.parse(new TextDecoder().decode(decrypted))); case 11: case "end": return _context3.stop(); } }, _callee3); })); return _decryptData.apply(this, arguments); } ; export function generateIdentityKey() { var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 32; var array = new Uint8Array(length); crypto.getRandomValues(array); return btoa(String.fromCharCode.apply(String, _toConsumableArray(array))).slice(0, length); } ;