@rasmx/hash 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +60 -53
- package/index.js +126 -72
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -1,106 +1,113 @@
|
|
|
1
1
|
export type InputData = string | Uint8Array | ArrayBuffer | SharedArrayBuffer;
|
|
2
2
|
|
|
3
3
|
export interface HashFamily {
|
|
4
|
-
v224: (d: InputData) =>
|
|
5
|
-
v256: (d: InputData) =>
|
|
6
|
-
v384: (d: InputData) =>
|
|
7
|
-
v512: (d: InputData) =>
|
|
8
|
-
v512_224: (d: InputData) =>
|
|
9
|
-
v512_256: (d: InputData) =>
|
|
4
|
+
v224: (d: InputData) => string;
|
|
5
|
+
v256: (d: InputData) => string;
|
|
6
|
+
v384: (d: InputData) => string;
|
|
7
|
+
v512: (d: InputData) => string;
|
|
8
|
+
v512_224: (d: InputData) => string;
|
|
9
|
+
v512_256: (d: InputData) => string;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export const hash: {
|
|
13
13
|
sha2: HashFamily;
|
|
14
|
-
sha3:
|
|
14
|
+
sha3: {
|
|
15
|
+
v224: (d: InputData) => string;
|
|
16
|
+
v256: (d: InputData) => string;
|
|
17
|
+
v384: (d: InputData) => string;
|
|
18
|
+
v512: (d: InputData) => string;
|
|
19
|
+
};
|
|
15
20
|
keccak: HashFamily;
|
|
16
|
-
blake3: (d: InputData, key?: InputData) =>
|
|
21
|
+
blake3: (d: InputData, key?: InputData) => string;
|
|
17
22
|
shake: {
|
|
18
|
-
v128: (d: InputData, len: number) =>
|
|
19
|
-
v256: (d: InputData, len: number) =>
|
|
23
|
+
v128: (d: InputData, len: number) => string;
|
|
24
|
+
v256: (d: InputData, len: number) => string;
|
|
20
25
|
};
|
|
21
26
|
xxh: {
|
|
22
|
-
v32: (d: InputData, seed?: number) =>
|
|
23
|
-
v64: (d: InputData, seed?: bigint) =>
|
|
24
|
-
v3_64: (d: InputData) =>
|
|
25
|
-
v3_64_seed: (d: InputData, seed: bigint) =>
|
|
26
|
-
v3_128: (d: InputData) =>
|
|
27
|
-
v3_128_seed: (d: InputData, seed: bigint) =>
|
|
27
|
+
v32: (d: InputData, seed?: number) => string;
|
|
28
|
+
v64: (d: InputData, seed?: bigint) => string;
|
|
29
|
+
v3_64: (d: InputData) => string;
|
|
30
|
+
v3_64_seed: (d: InputData, seed: bigint) => string;
|
|
31
|
+
v3_128: (d: InputData) => string;
|
|
32
|
+
v3_128_seed: (d: InputData, seed: bigint) => string;
|
|
28
33
|
};
|
|
29
34
|
hmac: {
|
|
30
|
-
sha256: (key: InputData, d: InputData) =>
|
|
31
|
-
sha512: (key: InputData, d: InputData) =>
|
|
35
|
+
sha256: (key: InputData, d: InputData) => string;
|
|
36
|
+
sha512: (key: InputData, d: InputData) => string;
|
|
32
37
|
};
|
|
33
|
-
md5: (d: InputData) =>
|
|
34
|
-
stream: (readable: ReadableStream, hasher:
|
|
38
|
+
md5: (d: InputData) => string;
|
|
39
|
+
stream: (readable: ReadableStream, hasher: IHasher) => Promise<string>;
|
|
35
40
|
};
|
|
36
41
|
|
|
37
42
|
export interface IHasher {
|
|
38
|
-
update(d: InputData):
|
|
39
|
-
finalize():
|
|
40
|
-
free():
|
|
43
|
+
update(d: InputData): void;
|
|
44
|
+
finalize(): string;
|
|
45
|
+
free(): void;
|
|
41
46
|
}
|
|
42
47
|
|
|
43
48
|
export class IncrementalSha256 implements IHasher {
|
|
44
49
|
constructor();
|
|
45
|
-
update(d: InputData):
|
|
46
|
-
finalize():
|
|
47
|
-
free():
|
|
50
|
+
update(d: InputData): void;
|
|
51
|
+
finalize(): string;
|
|
52
|
+
free(): void;
|
|
48
53
|
}
|
|
49
54
|
|
|
50
55
|
export class IncrementalSha512 implements IHasher {
|
|
51
56
|
constructor();
|
|
52
|
-
update(d: InputData):
|
|
53
|
-
finalize():
|
|
54
|
-
free():
|
|
57
|
+
update(d: InputData): void;
|
|
58
|
+
finalize(): string;
|
|
59
|
+
free(): void;
|
|
55
60
|
}
|
|
56
61
|
|
|
57
62
|
export class IncrementalMd5 implements IHasher {
|
|
58
63
|
constructor();
|
|
59
|
-
update(d: InputData):
|
|
60
|
-
finalize():
|
|
61
|
-
free():
|
|
64
|
+
update(d: InputData): void;
|
|
65
|
+
finalize(): string;
|
|
66
|
+
free(): void;
|
|
62
67
|
}
|
|
63
68
|
|
|
64
69
|
export class IncrementalBlake3 implements IHasher {
|
|
65
70
|
constructor();
|
|
66
|
-
update(d: InputData):
|
|
67
|
-
finalize():
|
|
68
|
-
free():
|
|
71
|
+
update(d: InputData): void;
|
|
72
|
+
finalize(): string;
|
|
73
|
+
free(): void;
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
export class IncrementalXxh3 {
|
|
72
77
|
constructor();
|
|
73
|
-
update(d: InputData):
|
|
74
|
-
finalize64():
|
|
75
|
-
finalize128():
|
|
76
|
-
finalize():
|
|
77
|
-
free():
|
|
78
|
+
update(d: InputData): void;
|
|
79
|
+
finalize64(): string;
|
|
80
|
+
finalize128(): string;
|
|
81
|
+
finalize(): string;
|
|
82
|
+
free(): void;
|
|
78
83
|
}
|
|
79
84
|
|
|
80
85
|
export class IncrementalXxh32 implements IHasher {
|
|
81
86
|
constructor(seed?: number);
|
|
82
|
-
update(d: InputData):
|
|
83
|
-
finalize():
|
|
84
|
-
free():
|
|
87
|
+
update(d: InputData): void;
|
|
88
|
+
finalize(): string;
|
|
89
|
+
free(): void;
|
|
85
90
|
}
|
|
86
91
|
|
|
87
92
|
export class IncrementalXxh64 implements IHasher {
|
|
88
93
|
constructor(seed?: bigint);
|
|
89
|
-
update(d: InputData):
|
|
90
|
-
finalize():
|
|
91
|
-
free():
|
|
94
|
+
update(d: InputData): void;
|
|
95
|
+
finalize(): string;
|
|
96
|
+
free(): void;
|
|
92
97
|
}
|
|
93
98
|
|
|
94
99
|
export class IncrementalSha3_256 implements IHasher {
|
|
95
100
|
constructor();
|
|
96
|
-
update(d: InputData):
|
|
97
|
-
finalize():
|
|
98
|
-
free():
|
|
101
|
+
update(d: InputData): void;
|
|
102
|
+
finalize(): string;
|
|
103
|
+
free(): void;
|
|
99
104
|
}
|
|
100
105
|
|
|
101
106
|
export class IncrementalKeccak256 implements IHasher {
|
|
102
107
|
constructor();
|
|
103
|
-
update(d: InputData):
|
|
104
|
-
finalize():
|
|
105
|
-
free():
|
|
106
|
-
}
|
|
108
|
+
update(d: InputData): void;
|
|
109
|
+
finalize(): string;
|
|
110
|
+
free(): void;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export default function init(input?: string | URL | Request): Promise<void>;
|
package/index.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import initWasm, * as wasm from './pkg/rasmx_hash.js';
|
|
2
2
|
|
|
3
3
|
let ready = false;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
|
|
5
|
+
export default async function init(input) {
|
|
6
|
+
if (ready) return;
|
|
7
|
+
await initWasm(input);
|
|
8
|
+
ready = true;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
const check = () => {
|
|
12
|
+
if (!ready) throw new Error("@rasmx/hash: WASM module not initialized. Call 'await init()' once before use.");
|
|
13
|
+
};
|
|
14
|
+
|
|
11
15
|
function toBuf(data) {
|
|
12
16
|
if (data instanceof Uint8Array) return data;
|
|
13
17
|
if (typeof data === 'string') return new TextEncoder().encode(data);
|
|
@@ -19,63 +23,72 @@ function toBuf(data) {
|
|
|
19
23
|
|
|
20
24
|
export const hash = {
|
|
21
25
|
sha2: {
|
|
22
|
-
v224:
|
|
23
|
-
v256:
|
|
24
|
-
v384:
|
|
25
|
-
v512:
|
|
26
|
-
v512_224:
|
|
27
|
-
v512_256:
|
|
26
|
+
v224: (d) => { check(); return wasm.sha2_224(toBuf(d)); },
|
|
27
|
+
v256: (d) => { check(); return wasm.sha2_256(toBuf(d)); },
|
|
28
|
+
v384: (d) => { check(); return wasm.sha2_384(toBuf(d)); },
|
|
29
|
+
v512: (d) => { check(); return wasm.sha2_512(toBuf(d)); },
|
|
30
|
+
v512_224: (d) => { check(); return wasm.sha2_512_224(toBuf(d)); },
|
|
31
|
+
v512_256: (d) => { check(); return wasm.sha2_512_256(toBuf(d)); }
|
|
28
32
|
},
|
|
29
33
|
sha3: {
|
|
30
|
-
v224:
|
|
31
|
-
v256:
|
|
32
|
-
v384:
|
|
33
|
-
v512:
|
|
34
|
+
v224: (d) => { check(); return wasm.sha3_224(toBuf(d)); },
|
|
35
|
+
v256: (d) => { check(); return wasm.sha3_256(toBuf(d)); },
|
|
36
|
+
v384: (d) => { check(); return wasm.sha3_384(toBuf(d)); },
|
|
37
|
+
v512: (d) => { check(); return wasm.sha3_512(toBuf(d)); }
|
|
34
38
|
},
|
|
35
39
|
keccak: {
|
|
36
|
-
v224:
|
|
37
|
-
v256:
|
|
38
|
-
v384:
|
|
39
|
-
v512:
|
|
40
|
+
v224: (d) => { check(); return wasm.keccak_224(toBuf(d)); },
|
|
41
|
+
v256: (d) => { check(); return wasm.keccak_256(toBuf(d)); },
|
|
42
|
+
v384: (d) => { check(); return wasm.keccak_384(toBuf(d)); },
|
|
43
|
+
v512: (d) => { check(); return wasm.keccak_512(toBuf(d)); }
|
|
40
44
|
},
|
|
41
45
|
shake: {
|
|
42
|
-
v128:
|
|
43
|
-
v256:
|
|
46
|
+
v128: (d, len) => { check(); return wasm.shake128(toBuf(d), len); },
|
|
47
|
+
v256: (d, len) => { check(); return wasm.shake256(toBuf(d), len); }
|
|
44
48
|
},
|
|
45
|
-
blake3:
|
|
46
|
-
|
|
49
|
+
blake3: (d, key = null) => {
|
|
50
|
+
check();
|
|
47
51
|
return key ? wasm.blake3_keyed_hex(toBuf(d), toBuf(key)) : wasm.blake3_hex(toBuf(d));
|
|
48
52
|
},
|
|
49
53
|
xxh: {
|
|
50
|
-
v32:
|
|
51
|
-
v64:
|
|
52
|
-
v3_64:
|
|
53
|
-
v3_64_seed:
|
|
54
|
-
v3_128:
|
|
55
|
-
v3_128_seed:
|
|
54
|
+
v32: (d, seed = 0) => { check(); return wasm.xxh32_hex(toBuf(d), seed); },
|
|
55
|
+
v64: (d, seed = 0n) => { check(); return wasm.xxh64_hex(toBuf(d), BigInt(seed)); },
|
|
56
|
+
v3_64: (d) => { check(); return wasm.xxh3_64_hex(toBuf(d)); },
|
|
57
|
+
v3_64_seed: (d, seed) => { check(); return wasm.xxh3_64_seed_hex(toBuf(d), BigInt(seed)); },
|
|
58
|
+
v3_128: (d) => { check(); return wasm.xxh3_128_hex(toBuf(d)); },
|
|
59
|
+
v3_128_seed: (d, seed) => { check(); return wasm.xxh3_128_seed_hex(toBuf(d), BigInt(seed)); }
|
|
56
60
|
},
|
|
57
61
|
hmac: {
|
|
58
|
-
sha256:
|
|
59
|
-
sha512:
|
|
62
|
+
sha256: (key, d) => { check(); return wasm.hmac_sha256(toBuf(key), toBuf(d)); },
|
|
63
|
+
sha512: (key, d) => { check(); return wasm.hmac_sha512(toBuf(key), toBuf(d)); }
|
|
60
64
|
},
|
|
61
|
-
md5:
|
|
65
|
+
md5: (d) => { check(); return wasm.md5_hex(toBuf(d)); },
|
|
66
|
+
|
|
62
67
|
stream: async (readable, hasher) => {
|
|
63
68
|
const reader = readable.getReader();
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
69
|
+
try {
|
|
70
|
+
while (true) {
|
|
71
|
+
const { done, value } = await reader.read();
|
|
72
|
+
if (done) break;
|
|
73
|
+
hasher.update(value);
|
|
74
|
+
}
|
|
75
|
+
return hasher.finalize();
|
|
76
|
+
} finally {
|
|
77
|
+
if (hasher.free) hasher.free();
|
|
68
78
|
}
|
|
69
|
-
const res = await hasher.finalize();
|
|
70
|
-
if (hasher.free) await hasher.free();
|
|
71
|
-
return res;
|
|
72
79
|
}
|
|
73
80
|
};
|
|
74
81
|
|
|
75
82
|
class BaseIncremental {
|
|
76
|
-
constructor() {
|
|
77
|
-
|
|
78
|
-
|
|
83
|
+
constructor(wasmClassInstance) {
|
|
84
|
+
check();
|
|
85
|
+
this.i = wasmClassInstance;
|
|
86
|
+
}
|
|
87
|
+
update(d) {
|
|
88
|
+
if (!this.i) throw new Error("Hasher already finalized or freed");
|
|
89
|
+
this.i.update(toBuf(d));
|
|
90
|
+
}
|
|
91
|
+
free() {
|
|
79
92
|
if (this.i) {
|
|
80
93
|
this.i.free();
|
|
81
94
|
this.i = null;
|
|
@@ -84,57 +97,98 @@ class BaseIncremental {
|
|
|
84
97
|
}
|
|
85
98
|
|
|
86
99
|
export class IncrementalSha256 extends BaseIncremental {
|
|
87
|
-
constructor() { super(
|
|
88
|
-
|
|
89
|
-
|
|
100
|
+
constructor() { super(new wasm.IncrementalSha256()); }
|
|
101
|
+
finalize() {
|
|
102
|
+
if (!this.i) throw new Error("Hasher already finalized or freed");
|
|
103
|
+
const res = this.i.finalize();
|
|
104
|
+
this.i = null;
|
|
105
|
+
return res;
|
|
106
|
+
}
|
|
90
107
|
}
|
|
91
108
|
|
|
92
109
|
export class IncrementalSha512 extends BaseIncremental {
|
|
93
|
-
constructor() { super(
|
|
94
|
-
|
|
95
|
-
|
|
110
|
+
constructor() { super(new wasm.IncrementalSha512()); }
|
|
111
|
+
finalize() {
|
|
112
|
+
if (!this.i) throw new Error("Hasher already finalized or freed");
|
|
113
|
+
const res = this.i.finalize();
|
|
114
|
+
this.i = null;
|
|
115
|
+
return res;
|
|
116
|
+
}
|
|
96
117
|
}
|
|
97
118
|
|
|
98
119
|
export class IncrementalSha3_256 extends BaseIncremental {
|
|
99
|
-
constructor() { super(
|
|
100
|
-
|
|
101
|
-
|
|
120
|
+
constructor() { super(new wasm.IncrementalSha3_256()); }
|
|
121
|
+
finalize() {
|
|
122
|
+
if (!this.i) throw new Error("Hasher already finalized or freed");
|
|
123
|
+
const res = this.i.finalize();
|
|
124
|
+
this.i = null;
|
|
125
|
+
return res;
|
|
126
|
+
}
|
|
102
127
|
}
|
|
103
128
|
|
|
104
129
|
export class IncrementalKeccak256 extends BaseIncremental {
|
|
105
|
-
constructor() { super(
|
|
106
|
-
|
|
107
|
-
|
|
130
|
+
constructor() { super(new wasm.IncrementalKeccak256()); }
|
|
131
|
+
finalize() {
|
|
132
|
+
if (!this.i) throw new Error("Hasher already finalized or freed");
|
|
133
|
+
const res = this.i.finalize();
|
|
134
|
+
this.i = null;
|
|
135
|
+
return res;
|
|
136
|
+
}
|
|
108
137
|
}
|
|
109
138
|
|
|
110
139
|
export class IncrementalMd5 extends BaseIncremental {
|
|
111
|
-
constructor() { super(
|
|
112
|
-
|
|
113
|
-
|
|
140
|
+
constructor() { super(new wasm.IncrementalMd5()); }
|
|
141
|
+
finalize() {
|
|
142
|
+
if (!this.i) throw new Error("Hasher already finalized or freed");
|
|
143
|
+
const res = this.i.finalize();
|
|
144
|
+
this.i = null;
|
|
145
|
+
return res;
|
|
146
|
+
}
|
|
114
147
|
}
|
|
115
148
|
|
|
116
149
|
export class IncrementalBlake3 extends BaseIncremental {
|
|
117
|
-
constructor() { super(
|
|
118
|
-
|
|
119
|
-
|
|
150
|
+
constructor() { super(new wasm.IncrementalBlake3()); }
|
|
151
|
+
finalize() {
|
|
152
|
+
if (!this.i) throw new Error("Hasher already finalized or freed");
|
|
153
|
+
const res = this.i.finalize();
|
|
154
|
+
this.i = null;
|
|
155
|
+
return res;
|
|
156
|
+
}
|
|
120
157
|
}
|
|
121
158
|
|
|
122
159
|
export class IncrementalXxh3 extends BaseIncremental {
|
|
123
|
-
constructor() { super(
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
160
|
+
constructor() { super(new wasm.IncrementalXxh3()); }
|
|
161
|
+
finalize64() {
|
|
162
|
+
if (!this.i) throw new Error("Hasher already finalized or freed");
|
|
163
|
+
const res = this.i.finalize_64();
|
|
164
|
+
this.i = null;
|
|
165
|
+
return res;
|
|
166
|
+
}
|
|
167
|
+
finalize128() {
|
|
168
|
+
if (!this.i) throw new Error("Hasher already finalized or freed");
|
|
169
|
+
const res = this.i.finalize_128();
|
|
170
|
+
this.i = null;
|
|
171
|
+
return res;
|
|
172
|
+
}
|
|
173
|
+
finalize() { return this.finalize64(); }
|
|
128
174
|
}
|
|
129
175
|
|
|
130
176
|
export class IncrementalXxh32 extends BaseIncremental {
|
|
131
|
-
constructor(seed = 0) { super(
|
|
132
|
-
|
|
133
|
-
|
|
177
|
+
constructor(seed = 0) { super(new wasm.IncrementalXxh32(seed)); }
|
|
178
|
+
finalize() {
|
|
179
|
+
if (!this.i) throw new Error("Hasher already finalized or freed");
|
|
180
|
+
const res = this.i.finalize();
|
|
181
|
+
this.i = null;
|
|
182
|
+
return res;
|
|
183
|
+
}
|
|
134
184
|
}
|
|
135
185
|
|
|
136
186
|
export class IncrementalXxh64 extends BaseIncremental {
|
|
137
|
-
constructor(seed = 0n) { super(
|
|
138
|
-
|
|
139
|
-
|
|
187
|
+
constructor(seed = 0n) { super(new wasm.IncrementalXxh64(BigInt(seed))); }
|
|
188
|
+
finalize() {
|
|
189
|
+
if (!this.i) throw new Error("Hasher already finalized or freed");
|
|
190
|
+
const res = this.i.finalize();
|
|
191
|
+
this.i = null;
|
|
192
|
+
return res;
|
|
193
|
+
}
|
|
140
194
|
}
|
package/package.json
CHANGED