@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.
Files changed (3) hide show
  1. package/index.d.ts +60 -53
  2. package/index.js +77 -75
  3. 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) => Promise<string>;
5
- v256: (d: InputData) => Promise<string>;
6
- v384: (d: InputData) => Promise<string>;
7
- v512: (d: InputData) => Promise<string>;
8
- v512_224: (d: InputData) => Promise<string>;
9
- v512_256: (d: InputData) => Promise<string>;
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: HashFamily;
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) => Promise<string>;
21
+ blake3: (d: InputData, key?: InputData) => string;
17
22
  shake: {
18
- v128: (d: InputData, len: number) => Promise<string>;
19
- v256: (d: InputData, len: number) => Promise<string>;
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) => Promise<string>;
23
- v64: (d: InputData, seed?: bigint) => Promise<string>;
24
- v3_64: (d: InputData) => Promise<string>;
25
- v3_64_seed: (d: InputData, seed: bigint) => Promise<string>;
26
- v3_128: (d: InputData) => Promise<string>;
27
- v3_128_seed: (d: InputData, seed: bigint) => Promise<string>;
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) => Promise<string>;
31
- sha512: (key: InputData, d: InputData) => Promise<string>;
35
+ sha256: (key: InputData, d: InputData) => string;
36
+ sha512: (key: InputData, d: InputData) => string;
32
37
  };
33
- md5: (d: InputData) => Promise<string>;
34
- stream: (readable: ReadableStream, hasher: any) => Promise<string>;
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): Promise<void>;
39
- finalize(): Promise<string>;
40
- free(): Promise<void>;
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): Promise<void>;
46
- finalize(): Promise<string>;
47
- free(): Promise<void>;
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): Promise<void>;
53
- finalize(): Promise<string>;
54
- free(): Promise<void>;
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): Promise<void>;
60
- finalize(): Promise<string>;
61
- free(): Promise<void>;
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): Promise<void>;
67
- finalize(): Promise<string>;
68
- free(): Promise<void>;
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): Promise<void>;
74
- finalize64(): Promise<string>;
75
- finalize128(): Promise<string>;
76
- finalize(): Promise<string>;
77
- free(): Promise<void>;
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): Promise<void>;
83
- finalize(): Promise<string>;
84
- free(): Promise<void>;
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): Promise<void>;
90
- finalize(): Promise<string>;
91
- free(): Promise<void>;
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): Promise<void>;
97
- finalize(): Promise<string>;
98
- free(): Promise<void>;
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): Promise<void>;
104
- finalize(): Promise<string>;
105
- free(): Promise<void>;
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 init, * as wasm from './pkg/rasmx_hash.js';
1
+ import initWasm, * as wasm from './pkg/rasmx_hash.js';
2
2
 
3
3
  let ready = false;
4
- async function ensure() {
5
- if (!ready) {
6
- await init();
7
- ready = true;
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: async (d) => { await ensure(); return wasm.sha2_224(toBuf(d)); },
23
- v256: async (d) => { await ensure(); return wasm.sha2_256(toBuf(d)); },
24
- v384: async (d) => { await ensure(); return wasm.sha2_384(toBuf(d)); },
25
- v512: async (d) => { await ensure(); return wasm.sha2_512(toBuf(d)); },
26
- v512_224: async (d) => { await ensure(); return wasm.sha2_512_224(toBuf(d)); },
27
- v512_256: async (d) => { await ensure(); return wasm.sha2_512_256(toBuf(d)); }
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: async (d) => { await ensure(); return wasm.sha3_224(toBuf(d)); },
31
- v256: async (d) => { await ensure(); return wasm.sha3_256(toBuf(d)); },
32
- v384: async (d) => { await ensure(); return wasm.sha3_384(toBuf(d)); },
33
- v512: async (d) => { await ensure(); return wasm.sha3_512(toBuf(d)); }
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: async (d) => { await ensure(); return wasm.keccak_224(toBuf(d)); },
37
- v256: async (d) => { await ensure(); return wasm.keccak_256(toBuf(d)); },
38
- v384: async (d) => { await ensure(); return wasm.keccak_384(toBuf(d)); },
39
- v512: async (d) => { await ensure(); return wasm.keccak_512(toBuf(d)); }
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: async (d, len) => { await ensure(); return wasm.shake128(toBuf(d), len); },
43
- v256: async (d, len) => { await ensure(); return wasm.shake256(toBuf(d), len); }
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: async (d, key = null) => {
46
- await ensure();
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: async (d, seed = 0) => { await ensure(); return wasm.xxh32_hex(toBuf(d), seed); },
51
- v64: async (d, seed = 0n) => { await ensure(); return wasm.xxh64_hex(toBuf(d), BigInt(seed)); },
52
- v3_64: async (d) => { await ensure(); return wasm.xxh3_64_hex(toBuf(d)); },
53
- v3_64_seed: async (d, seed) => { await ensure(); return wasm.xxh3_64_seed_hex(toBuf(d), BigInt(seed)); },
54
- v3_128: async (d) => { await ensure(); return wasm.xxh3_128_hex(toBuf(d)); },
55
- v3_128_seed: async (d, seed) => { await ensure(); return wasm.xxh3_128_seed_hex(toBuf(d), BigInt(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: async (key, d) => { await ensure(); return wasm.hmac_sha256(toBuf(key), toBuf(d)); },
59
- sha512: async (key, d) => { await ensure(); return wasm.hmac_sha512(toBuf(key), toBuf(d)); }
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: async (d) => { await ensure(); return wasm.md5_hex(toBuf(d)); },
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
- while (true) {
65
- const { done, value } = await reader.read();
66
- if (done) break;
67
- await hasher.update(value);
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() { this.p = ensure(); this.i = null; }
77
- async free() {
78
- await this.p;
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(); this.p = this.p.then(() => this.i = new wasm.IncrementalSha256()); }
88
- async update(d) { await this.p; this.i.update(toBuf(d)); }
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(); this.p = this.p.then(() => this.i = new wasm.IncrementalSha512()); }
94
- async update(d) { await this.p; this.i.update(toBuf(d)); }
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(); this.p = this.p.then(() => this.i = new wasm.IncrementalSha3_256()); }
100
- async update(d) { await this.p; this.i.update(toBuf(d)); }
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(); this.p = this.p.then(() => this.i = new wasm.IncrementalKeccak256()); }
106
- async update(d) { await this.p; this.i.update(toBuf(d)); }
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(); this.p = this.p.then(() => this.i = new wasm.IncrementalMd5()); }
112
- async update(d) { await this.p; this.i.update(toBuf(d)); }
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(); this.p = this.p.then(() => this.i = new wasm.IncrementalBlake3()); }
118
- async update(d) { await this.p; this.i.update(toBuf(d)); }
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(); this.p = this.p.then(() => this.i = new wasm.IncrementalXxh3()); }
124
- async update(d) { await this.p; this.i.update(toBuf(d)); }
125
- async finalize64() { await this.p; return this.i.finalize_64(); }
126
- async finalize128() { await this.p; return this.i.finalize_128(); }
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(); this.p = this.p.then(() => this.i = new wasm.IncrementalXxh32(seed)); }
132
- async update(d) { await this.p; this.i.update(toBuf(d)); }
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(); this.p = this.p.then(() => this.i = new wasm.IncrementalXxh64(BigInt(seed))); }
138
- async update(d) { await this.p; this.i.update(toBuf(d)); }
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rasmx/hash",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Blazing fast WASM hashing library powered by Rust. Support for SHA2, SHA3, Keccak, XXHash, and MD5 with streaming support.",
5
5
  "publishConfig": {
6
6
  "access": "public"