Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove node crypto #39

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 13 additions & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import nodeCrypto from "crypto";
import { ec as EC } from "elliptic";

const ec = new EC("secp256k1");
// eslint-disable-next-line @typescript-eslint/no-explicit-any, n/no-unsupported-features/node-builtins
const browserCrypto = global.crypto || (global as any).msCrypto || {};
const browserCrypto = globalThis.crypto || (globalThis as any).msCrypto || {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const subtle = browserCrypto.subtle || (browserCrypto as any).webkitSubtle;

Expand Down Expand Up @@ -44,7 +43,7 @@
}
let res = 0;
for (let i = 0; i < b1.length; i++) {
res |= b1[i] ^ b2[i]; // jshint ignore:line

Check warning on line 46 in src/index.ts

View workflow job for this annotation

GitHub Actions / run tests (20.x, ubuntu-latest)

Generic Object Injection Sink

Check warning on line 46 in src/index.ts

View workflow job for this annotation

GitHub Actions / run tests (20.x, ubuntu-latest)

Generic Object Injection Sink
}

return res === 0;
Expand All @@ -55,23 +54,14 @@
not convert using browserify */
function randomBytes(size: number): Buffer {
const arr = new Uint8Array(size);
if (typeof browserCrypto.getRandomValues === "undefined") {
return Buffer.from(nodeCrypto.randomBytes(size));
}
browserCrypto.getRandomValues(arr);

return Buffer.from(arr);
}

async function sha512(msg: Buffer): Promise<Uint8Array> {
if (subtle) {
const hash = await subtle.digest("SHA-512", msg);
const result = new Uint8Array(hash);
return result;
}
const hash = nodeCrypto.createHash("sha512");
const result = hash.update(msg).digest();
return new Uint8Array(result);
const hash = await subtle.digest("SHA-512", msg);
const result = new Uint8Array(hash);
return result;
}

type AesFunctionType = (iv: Buffer, key: Buffer, data: Buffer) => Promise<Buffer>;
Expand All @@ -87,18 +77,8 @@
name: "AES-CBC",
iv,
};
const result = await subtle[op](encAlgorithm, cryptoKey, data);

Check warning on line 80 in src/index.ts

View workflow job for this annotation

GitHub Actions / run tests (20.x, ubuntu-latest)

Function Call Object Injection Sink
return Buffer.from(new Uint8Array(result));
} else if (op === "encrypt") {
const cipher = nodeCrypto.createCipheriv("aes-256-cbc", key, iv);
const firstChunk = cipher.update(data);
const secondChunk = cipher.final();
return Buffer.concat([firstChunk, secondChunk]);
} else if (op === "decrypt") {
const decipher = nodeCrypto.createDecipheriv("aes-256-cbc", key, iv);
const firstChunk = decipher.update(data);
const secondChunk = decipher.final();
return Buffer.concat([firstChunk, secondChunk]);
}
throw new Error(`Unsupported operation: ${op}`);
};
Expand All @@ -107,21 +87,15 @@
const aesCbcDecrypt = getAes("decrypt");

async function hmacSha256Sign(key: Buffer, msg: Buffer): Promise<Buffer> {
if (subtle) {
const importAlgorithm = {
name: "HMAC",
hash: {
name: "SHA-256",
},
};
const cryptoKey = await subtle.importKey("raw", new Uint8Array(key), importAlgorithm, false, ["sign", "verify"]);
const sig = await subtle.sign("HMAC", cryptoKey, msg);
const result = Buffer.from(new Uint8Array(sig));
return result;
}
const hmac = nodeCrypto.createHmac("sha256", Buffer.from(key));
hmac.update(msg);
const result = hmac.digest();
const importAlgorithm = {
name: "HMAC",
hash: {
name: "SHA-256",
},
};
const cryptoKey = await subtle.importKey("raw", new Uint8Array(key), importAlgorithm, false, ["sign", "verify"]);
const sig = await subtle.sign("HMAC", cryptoKey, msg);
const result = Buffer.from(new Uint8Array(sig));
return result;
}
async function hmacSha256Verify(key: Buffer, msg: Buffer, sig: Buffer): Promise<boolean> {
Expand Down
Loading