@rasmx/hash 1.0.0 → 1.0.1
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 +77 -75
- 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,122 +23,120 @@ 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) => {
|
|
68
|
+
check();
|
|
63
69
|
const reader = readable.getReader();
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
70
|
+
try {
|
|
71
|
+
while (true) {
|
|
72
|
+
const { done, value } = await reader.read();
|
|
73
|
+
if (done) break;
|
|
74
|
+
hasher.update(value);
|
|
75
|
+
}
|
|
76
|
+
return hasher.finalize();
|
|
77
|
+
} finally {
|
|
78
|
+
if (hasher.free) hasher.free();
|
|
68
79
|
}
|
|
69
|
-
const res = await hasher.finalize();
|
|
70
|
-
if (hasher.free) await hasher.free();
|
|
71
|
-
return res;
|
|
72
80
|
}
|
|
73
81
|
};
|
|
74
82
|
|
|
75
83
|
class BaseIncremental {
|
|
76
|
-
constructor() {
|
|
77
|
-
|
|
78
|
-
|
|
84
|
+
constructor(wasmClassInstance) {
|
|
85
|
+
check();
|
|
86
|
+
this.i = wasmClassInstance;
|
|
87
|
+
}
|
|
88
|
+
update(d) { this.i.update(toBuf(d)); }
|
|
89
|
+
free() {
|
|
79
90
|
if (this.i) {
|
|
80
|
-
this.i.free();
|
|
81
|
-
this.i = null;
|
|
82
|
-
}
|
|
91
|
+
this.i.free();
|
|
92
|
+
this.i = null;
|
|
93
|
+
}
|
|
83
94
|
}
|
|
84
95
|
}
|
|
85
96
|
|
|
86
97
|
export class IncrementalSha256 extends BaseIncremental {
|
|
87
|
-
constructor() { super(
|
|
88
|
-
|
|
89
|
-
async finalize() { await this.p; return this.i.finalize(); }
|
|
98
|
+
constructor() { super(new wasm.IncrementalSha256()); }
|
|
99
|
+
finalize() { return this.i.finalize(); }
|
|
90
100
|
}
|
|
91
101
|
|
|
92
102
|
export class IncrementalSha512 extends BaseIncremental {
|
|
93
|
-
constructor() { super(
|
|
94
|
-
|
|
95
|
-
async finalize() { await this.p; return this.i.finalize(); }
|
|
103
|
+
constructor() { super(new wasm.IncrementalSha512()); }
|
|
104
|
+
finalize() { return this.i.finalize(); }
|
|
96
105
|
}
|
|
97
106
|
|
|
98
107
|
export class IncrementalSha3_256 extends BaseIncremental {
|
|
99
|
-
constructor() { super(
|
|
100
|
-
|
|
101
|
-
async finalize() { await this.p; return this.i.finalize(); }
|
|
108
|
+
constructor() { super(new wasm.IncrementalSha3_256()); }
|
|
109
|
+
finalize() { return this.i.finalize(); }
|
|
102
110
|
}
|
|
103
111
|
|
|
104
112
|
export class IncrementalKeccak256 extends BaseIncremental {
|
|
105
|
-
constructor() { super(
|
|
106
|
-
|
|
107
|
-
async finalize() { await this.p; return this.i.finalize(); }
|
|
113
|
+
constructor() { super(new wasm.IncrementalKeccak256()); }
|
|
114
|
+
finalize() { return this.i.finalize(); }
|
|
108
115
|
}
|
|
109
116
|
|
|
110
117
|
export class IncrementalMd5 extends BaseIncremental {
|
|
111
|
-
constructor() { super(
|
|
112
|
-
|
|
113
|
-
async finalize() { await this.p; return this.i.finalize(); }
|
|
118
|
+
constructor() { super(new wasm.IncrementalMd5()); }
|
|
119
|
+
finalize() { return this.i.finalize(); }
|
|
114
120
|
}
|
|
115
121
|
|
|
116
122
|
export class IncrementalBlake3 extends BaseIncremental {
|
|
117
|
-
constructor() { super(
|
|
118
|
-
|
|
119
|
-
async finalize() { await this.p; return this.i.finalize(); }
|
|
123
|
+
constructor() { super(new wasm.IncrementalBlake3()); }
|
|
124
|
+
finalize() { return this.i.finalize(); }
|
|
120
125
|
}
|
|
121
126
|
|
|
122
127
|
export class IncrementalXxh3 extends BaseIncremental {
|
|
123
|
-
constructor() { super(
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
async finalize() { return this.finalize64(); }
|
|
128
|
+
constructor() { super(new wasm.IncrementalXxh3()); }
|
|
129
|
+
finalize64() { return this.i.finalize_64(); }
|
|
130
|
+
finalize128() { return this.i.finalize_128(); }
|
|
131
|
+
finalize() { return this.finalize64(); }
|
|
128
132
|
}
|
|
129
133
|
|
|
130
134
|
export class IncrementalXxh32 extends BaseIncremental {
|
|
131
|
-
constructor(seed = 0) { super(
|
|
132
|
-
|
|
133
|
-
async finalize() { await this.p; return this.i.finalize(); }
|
|
135
|
+
constructor(seed = 0) { super(new wasm.IncrementalXxh32(seed)); }
|
|
136
|
+
finalize() { return this.i.finalize(); }
|
|
134
137
|
}
|
|
135
138
|
|
|
136
139
|
export class IncrementalXxh64 extends BaseIncremental {
|
|
137
|
-
constructor(seed = 0n) { super(
|
|
138
|
-
|
|
139
|
-
async finalize() { await this.p; return this.i.finalize(); }
|
|
140
|
+
constructor(seed = 0n) { super(new wasm.IncrementalXxh64(BigInt(seed))); }
|
|
141
|
+
finalize() { return this.i.finalize(); }
|
|
140
142
|
}
|
package/package.json
CHANGED