79cc2d9ea2925e495d14a7d99147661b7af4b3876d6fc773b65fe2a612b5b207.json raw

   1  {"ast":null,"code":"import _asyncToGenerator from \"/home/mleku/src/orly.dev/next/signer/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js\";\nimport { Buffer } from 'buffer';\nexport class CryptoHelper {\n  /**\n   * Generate a base64 encoded IV.\n   */\n  static generateIV() {\n    const iv = crypto.getRandomValues(new Uint8Array(12));\n    return Buffer.from(iv).toString('base64');\n  }\n  /**\n   * Hash (SHA-256) a text string.\n   */\n  static hash(text) {\n    return _asyncToGenerator(function* () {\n      const textUint8 = new TextEncoder().encode(text); // encode as (utf-8) Uint8Array\n      const hashBuffer = yield crypto.subtle.digest('SHA-256', textUint8); // hash the message\n      const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array\n      const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string\n      return hashHex;\n    })();\n  }\n  static v4() {\n    return crypto.randomUUID();\n  }\n  static deriveKey(password) {\n    return _asyncToGenerator(function* () {\n      const algo = {\n        name: 'PBKDF2',\n        hash: 'SHA-256',\n        salt: new TextEncoder().encode('3e7cdebd-3b4c-4125-a18c-05750cad8ec3'),\n        iterations: 1000\n      };\n      return crypto.subtle.deriveKey(algo, yield crypto.subtle.importKey('raw', new TextEncoder().encode(password), {\n        name: algo.name\n      }, false, ['deriveKey']), {\n        name: 'AES-GCM',\n        length: 256\n      }, false, ['encrypt', 'decrypt']);\n    })();\n  }\n  static encrypt(text, ivBase64String, password) {\n    return _asyncToGenerator(function* () {\n      const algo = {\n        name: 'AES-GCM',\n        length: 256,\n        iv: Buffer.from(ivBase64String, 'base64')\n      };\n      const cipherText = yield crypto.subtle.encrypt(algo, yield CryptoHelper.deriveKey(password), new TextEncoder().encode(text));\n      return Buffer.from(cipherText).toString('base64');\n    })();\n  }\n  static decrypt(encryptedBase64String, ivBase64String, password) {\n    return _asyncToGenerator(function* () {\n      const algo = {\n        name: 'AES-GCM',\n        length: 256,\n        iv: Buffer.from(ivBase64String, 'base64')\n      };\n      return new TextDecoder().decode(yield crypto.subtle.decrypt(algo, yield CryptoHelper.deriveKey(password), Buffer.from(encryptedBase64String, 'base64')));\n    })();\n  }\n}","map":null,"metadata":{},"sourceType":"module","externalDependencies":[]}