Skip to content

Commit

Permalink
Support react native environment when polyfilled
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti committed Jul 5, 2024
1 parent 52b1c67 commit 997abab
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type AesFunctionType = (iv: Buffer, key: Buffer, data: Buffer) => Promise<Buffer

function getAes(op: "encrypt" | "decrypt"): AesFunctionType {
return async function (iv: Buffer, key: Buffer, data: Buffer) {
if (subtle) {
if (subtle && subtle[op] && subtle.importKey) {

Check warning on line 71 in src/index.ts

View workflow job for this annotation

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

Generic Object Injection Sink
const importAlgorithm = {
name: "AES-CBC",
};
Expand All @@ -77,8 +77,20 @@ function getAes(op: "encrypt" | "decrypt"): AesFunctionType {
name: "AES-CBC",
iv,
};
// encrypt and decrypt ops are not implemented in react-native-quick-crypto yet.
const result = await subtle[op](encAlgorithm, cryptoKey, data);

Check warning on line 81 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" && browserCrypto.createCipheriv) {
// This is available if crypto is polyfilled in react native environment
const cipher = browserCrypto.createCipheriv("aes-256-cbc", key, iv);
const firstChunk = cipher.update(data);
const secondChunk = cipher.final();
return Buffer.concat([firstChunk, secondChunk]);
} else if (op === "decrypt" && browserCrypto.createDecipheriv) {
const decipher = browserCrypto.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 Down

0 comments on commit 997abab

Please sign in to comment.