Skip to content

Commit

Permalink
Wrap bigint operations with try catch (#25839)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaKhD authored Oct 19, 2023
1 parent 47000fd commit 217f64a
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions packages/devextreme/js/__internal/core/license/rsa_bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,31 @@

import { PublicKey } from './key';

const ZERO = BigInt(0);
const EIGHT = BigInt(8);

function bigIntFromBytes(bytes: Uint8Array): bigint {
// eslint-disable-next-line no-bitwise
return bytes.reduce((acc, cur) => (acc << EIGHT) + BigInt(cur), ZERO);
}

interface Args {
key: PublicKey;
signature: Uint8Array;
actual: Uint8Array;
}
export function compareSignatures(args: Args): boolean {
if (typeof BigInt === 'undefined') {
return true;
}
const actual = bigIntFromBytes(args.actual);
try {
const zero = BigInt(0);
const eight = BigInt(8);

const signature = bigIntFromBytes(args.signature);
const exponent = BigInt(args.key.e);
const modulus = bigIntFromBytes(args.key.n);
const expected = (signature ** exponent) % modulus;
const bigIntFromBytes = (bytes: Uint8Array): bigint => bytes.reduce(
// eslint-disable-next-line no-bitwise
(acc, cur) => (acc << eight) + BigInt(cur),
zero,
);

return expected === actual;
const actual = bigIntFromBytes(args.actual);

const signature = bigIntFromBytes(args.signature);
const exponent = BigInt(args.key.e);
const modulus = bigIntFromBytes(args.key.n);
const expected = (signature ** exponent) % modulus;

return expected === actual;
} catch {
return true;
}
}

0 comments on commit 217f64a

Please sign in to comment.