Skip to content

Commit 1473682

Browse files
committed
refactor: move createHash onto environment object
1 parent 4e2e6ab commit 1473682

4 files changed

Lines changed: 12 additions & 7 deletions

File tree

src/crypto/hash.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { createHash } from "../environment/index.js";
1+
import { environment } from "../environment/index.js";
22
import { fixBufferLength } from "../util.js";
33

44
export function hash(algorithm: string, buffers: Buffer[], length?: number): Buffer {
5-
const digest = createHash(algorithm);
5+
const digest = environment.createHash(algorithm);
66

77
for (const buffer of buffers) {
88
digest.update(buffer);

src/environment/browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import browserifyAES from "browserify-aes/browser.js";
22
import { inflate as pakoInflate } from "pako";
33
import type { Environment } from "./types.js";
4-
5-
export { default as createHash } from "create-hash";
4+
import { default as createHash } from "create-hash";
65

76
export const environment: Environment = {
87
inflate: (data) => Buffer.from(pakoInflate(data)),
98
createDecipheriv: browserifyAES.createDecipheriv,
9+
createHash,
1010
};

src/environment/node.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
import { createDecipheriv, createHash } from "node:crypto";
12
import { inflateSync } from "node:zlib";
23
import type { Environment } from "./types.js";
3-
import { createDecipheriv } from "crypto";
4-
5-
export { createHash } from "crypto";
64

75
export const environment: Environment = {
86
inflate: (data) => inflateSync(data),
97
createDecipheriv,
8+
createHash,
109
};

src/environment/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
export type Environment = {
22
inflate: (data: Buffer) => Buffer;
33
createDecipheriv: (algorithm: string, key: Buffer, iv: Buffer) => Decipher;
4+
createHash: (algorithm: string) => Hash;
45
};
56

67
export type Decipher = {
78
setAutoPadding: (padding: boolean) => void;
89
update: (data: Buffer) => Buffer;
910
};
11+
12+
export type Hash = {
13+
update: (data: Buffer) => void;
14+
digest: () => Buffer;
15+
};

0 commit comments

Comments
 (0)