Skip to content

Commit

Permalink
use callbacks from crypto in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
lmdulz committed Jan 18, 2024
1 parent 799e297 commit 167c053
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
16 changes: 9 additions & 7 deletions src/anonymizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
41 changes: 30 additions & 11 deletions src/randomizer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as crypto from "crypto";
import { data } from "dcmjs";

Check warning on line 2 in src/randomizer.ts

View workflow job for this annotation

GitHub Actions / CI (16.x)

'data' is defined but never used. Allowed unused vars must match /^_/u
import getRandomValues from "get-random-values";

// use only in node env
Expand All @@ -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<Uint8Array> {
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) {
Expand All @@ -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;
}
}
6 changes: 5 additions & 1 deletion src/uianonymizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"incremental": true,
"inlineSources": false,
"isolatedModules": true,
"lib": ["ES2021"],
"lib": ["ES2021", "dom"],
"module": "ES2015",
"moduleResolution": "node",
"noEmit": true,
Expand Down

0 comments on commit 167c053

Please sign in to comment.