Skip to content

Commit

Permalink
change randomizer for web usage
Browse files Browse the repository at this point in the history
  • Loading branch information
lmdulz committed Feb 8, 2024
1 parent 167c053 commit 8457f55
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions src/randomizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as crypto from "crypto";
import { data } from "dcmjs";
// import * as crypto from "crypto";
import getRandomValues from "get-random-values";

// use only in node env
Expand All @@ -15,22 +14,16 @@ export class Randomizer {
}
}

/* for Browserenvironment
private generateRandomBytes(length: number): Uint8Array {
const randomValues = new Uint8Array(length);
return randomValues;
}
*/

private calculateMD5Digest(data: Uint8Array): Uint8Array {
const hash = crypto.createHash("md5");
hash.update(data);
return hash.digest();
}

// For Node work
// 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);
private async calculateSHADigestWeb(array: Uint8Array): Promise<Uint8Array> {
const hashBuffer = await window.crypto.subtle.digest("SHA-256", array);
return new Uint8Array(hashBuffer);
}

Expand Down Expand Up @@ -58,15 +51,15 @@ export class Randomizer {
const encoded = encoder.encode(message);

if (typeof window !== "undefined") {
this.calculateMD5DigestWeb(encoded).then((hashBuffer) => {
this.calculateSHADigestWeb(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);
// const hashed = this.calculateMD5Digest(encoded);
// const result = this.calculateResult(hashed);
// callback(result);
}
}

Expand Down

0 comments on commit 8457f55

Please sign in to comment.