()
| 53 | } |
| 54 | |
| 55 | export function getPlatformCreateKey() { |
| 56 | /** |
| 57 | * Given a user encryptStr and initialization vector, generate a private key |
| 58 | * @param encryptStr String to encrypt (can be user password or some kind of lookup key) |
| 59 | * @param ivHex hex string iv value |
| 60 | */ |
| 61 | const createKey = async (encryptStr: string, ivHex: string) => { |
| 62 | if (isWebEnv()) { |
| 63 | return new Promise<PrivateKey>((resolve) => { |
| 64 | const worker = WebWorker(require("./authWorker.js").toString()); |
| 65 | worker.postMessage(JSON.stringify({ encryptStr, ivHex })); |
| 66 | |
| 67 | worker.onmessage = (event) => { |
| 68 | resolve(event.data); |
| 69 | }; |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | if (isNodeEnv()) { |
| 74 | return new Promise<PrivateKey>((resolve, reject) => { |
| 75 | const N = 32768; |
| 76 | const r = 8; |
| 77 | const p = 1; |
| 78 | const dkLen = 32; |
| 79 | const encryptStrBuffer = Buffer.from(encryptStr); |
| 80 | const ivBuffer = Buffer.from(ivHex); |
| 81 | // https://github.com/nodejs/node/issues/21524#issuecomment-400012811 |
| 82 | const maxmem = 128 * p * r + 128 * (2 + N) * r; |
| 83 | |
| 84 | crypto.scrypt( |
| 85 | encryptStrBuffer, |
| 86 | ivBuffer, |
| 87 | dkLen, |
| 88 | { N, r, p, maxmem }, |
| 89 | (err, derivedKey) => { |
| 90 | if (err) { |
| 91 | reject(err); |
| 92 | } else { |
| 93 | const keyHex = derivedKey.toString("hex"); |
| 94 | |
| 95 | // This is the private key |
| 96 | const keyBuffer = bufferFromHexString(keyHex); |
| 97 | resolve({ keyHex, keyBuffer }); |
| 98 | } |
| 99 | } |
| 100 | ); |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | throw new Error( |
| 105 | "Please pass in valid createKey function into the Hedgehog constructor" |
| 106 | ); |
| 107 | }; |
| 108 | |
| 109 | return createKey; |
| 110 | } |
| 111 | |
| 112 | export function waitUntil(condition: () => boolean) { |
no outgoing calls
no test coverage detected