From 167c053b8c5c9205fb08b54eb30cc1510b17f38d Mon Sep 17 00:00:00 2001 From: lmdulz Date: Thu, 18 Jan 2024 10:23:27 +0000 Subject: [PATCH] use callbacks from crypto in browser --- src/anonymizer.ts | 16 +++++++++------- src/randomizer.ts | 41 ++++++++++++++++++++++++++++++----------- src/uianonymizer.ts | 6 +++++- tsconfig.json | 2 +- 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/anonymizer.ts b/src/anonymizer.ts index b31819a..4eb72d4 100644 --- a/src/anonymizer.ts +++ b/src/anonymizer.ts @@ -42,13 +42,15 @@ export class Anonymizer { this.patientID = patientID; } this.randomizer = new Randomizer(seed); - this.dateOffsetHours = Number( - -( - (this.randomizer.toInt("dateOffset") % - (BigInt(maximumOffsetHours) - BigInt(minimumOffsetHours))) + - BigInt(minimumOffsetHours) - ) - ); + this.dateOffsetHours = Number(0); + this.randomizer.toInt("dateOffset", (res) => { + this.dateOffsetHours = Number( + -( + (res % (BigInt(maximumOffsetHours) - BigInt(minimumOffsetHours))) + + BigInt(minimumOffsetHours) + ) + ); + }); //this.data = data; this.addressAnonymizer = new AddressAnonymizer(this.randomizer); this.elementHandlers = [ diff --git a/src/randomizer.ts b/src/randomizer.ts index fc604c1..2c7bfe8 100644 --- a/src/randomizer.ts +++ b/src/randomizer.ts @@ -1,4 +1,5 @@ import * as crypto from "crypto"; +import { data } from "dcmjs"; import getRandomValues from "get-random-values"; // use only in node env @@ -25,10 +26,14 @@ export class Randomizer { private calculateMD5Digest(data: Uint8Array): Uint8Array { const hash = crypto.createHash("md5"); hash.update(data); - return hash.digest(); } + private async calculateMD5DigestWeb(data: Uint8Array): Promise { + const hashBuffer = await crypto.subtle.digest("MD5", data); + return new Uint8Array(hashBuffer); + } + private calculateResult(hash: Uint8Array): bigint { let result = 0; for (const byte of hash) { @@ -47,23 +52,37 @@ export class Randomizer { return seed; } - public toInt(originalValue: string): bigint { + public toInt(originalValue: string, callback: (result: bigint) => void): void { const message = this.seed + String(originalValue); const encoder = new TextEncoder(); const encoded = encoder.encode(message); - const hashed = this.calculateMD5Digest(encoded); - return this.calculateResult(hashed); + + if (typeof window !== "undefined") { + this.calculateMD5DigestWeb(encoded).then((hashBuffer) => { + const hashed = hashBuffer; + const result = this.calculateResult(hashed); + callback(result); + }); + } else { + const hashed = this.calculateMD5Digest(encoded); + const result = this.calculateResult(hashed); + callback(result); + } } public getIntsFromRanges(originalValue: string, ...suprema: number[]): number[] { - let big_Int = this.toInt(originalValue); - const result: number[] = []; - for (const x of suprema) { - const s = BigInt(x); - result.push(Number(big_Int % s)); - big_Int = big_Int / s; - } + let result: bigint | number[] = []; + this.toInt(originalValue, (res) => { + let bigNumber = res; + const arr: number[] = []; + for (const x of suprema) { + const s = BigInt(x); + arr.push(Number(bigNumber % s)); + bigNumber = bigNumber / s; + } + result = arr; + }); return result; } } diff --git a/src/uianonymizer.ts b/src/uianonymizer.ts index 256f6fb..da2fd3c 100644 --- a/src/uianonymizer.ts +++ b/src/uianonymizer.ts @@ -33,6 +33,10 @@ export class UIAnonymizer { }; newUI(origUI: string) { - return `2.${BigInt(10 ** 39) + this.randomizer.toInt(origUI)}`; + let number4String = BigInt(0); + this.randomizer.toInt(origUI, (res) => { + number4String = res; + }); + return `2.${BigInt(10 ** 39) + number4String}`; } } diff --git a/tsconfig.json b/tsconfig.json index c4e68df..358c07b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,7 @@ "incremental": true, "inlineSources": false, "isolatedModules": true, - "lib": ["ES2021"], + "lib": ["ES2021", "dom"], "module": "ES2015", "moduleResolution": "node", "noEmit": true,