@fuzdev/fuz_util 0.53.0 → 0.53.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/README.md +4 -3
- package/dist/hash.d.ts +24 -2
- package/dist/hash.d.ts.map +1 -1
- package/dist/hash.js +36 -6
- package/dist/hex.d.ts +9 -1
- package/dist/hex.d.ts.map +1 -1
- package/dist/hex.js +18 -1
- package/package.json +4 -4
- package/src/lib/hash.ts +44 -6
- package/src/lib/hex.ts +18 -1
package/README.md
CHANGED
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
design:
|
|
10
10
|
|
|
11
11
|
- kitchen-sink utilities library - sorry, I wish it weren't so, JS made me do it
|
|
12
|
-
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
- all dependencies are optional ([`zod`](https://github.com/colinhacks/zod),
|
|
13
|
+
[`svelte`](https://github.com/sveltejs/svelte),
|
|
14
|
+
[`@fuzdev/blake3_wasm`](https://github.com/fuzdev/blake3),
|
|
15
|
+
[`esm-env`](https://github.com/benmccann/esm-env), `@types/node`, `@types/estree`)
|
|
15
16
|
- mix of JS module environments - browser-only, Node-only, universal
|
|
16
17
|
- mostly small pure functions
|
|
17
18
|
- all TypeScript, for styles and Svelte and SvelteKit
|
package/dist/hash.d.ts
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Hash utilities
|
|
2
|
+
* Hash utilities using Web Crypto API and DJB2.
|
|
3
3
|
*
|
|
4
|
-
* Provides `hash_sha256` (Web Crypto, async)
|
|
4
|
+
* Provides `hash_sha256`, `hash_sha384`, `hash_sha512`, `hash_sha1` (Web Crypto, async)
|
|
5
|
+
* and `hash_insecure` (DJB2, fast non-cryptographic).
|
|
5
6
|
* For BLAKE3, see `hash_blake3` in `hash_blake3.ts`.
|
|
6
7
|
*
|
|
7
8
|
* @module
|
|
8
9
|
*/
|
|
10
|
+
/**
|
|
11
|
+
* Computes a SHA-1 hash using Web Crypto API.
|
|
12
|
+
*
|
|
13
|
+
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
14
|
+
* @returns 40-character hexadecimal hash string.
|
|
15
|
+
*/
|
|
16
|
+
export declare const hash_sha1: (data: BufferSource | string) => Promise<string>;
|
|
9
17
|
/**
|
|
10
18
|
* Computes a SHA-256 hash using Web Crypto API.
|
|
11
19
|
*
|
|
@@ -13,6 +21,20 @@
|
|
|
13
21
|
* @returns 64-character hexadecimal hash string.
|
|
14
22
|
*/
|
|
15
23
|
export declare const hash_sha256: (data: BufferSource | string) => Promise<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Computes a SHA-384 hash using Web Crypto API.
|
|
26
|
+
*
|
|
27
|
+
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
28
|
+
* @returns 96-character hexadecimal hash string.
|
|
29
|
+
*/
|
|
30
|
+
export declare const hash_sha384: (data: BufferSource | string) => Promise<string>;
|
|
31
|
+
/**
|
|
32
|
+
* Computes a SHA-512 hash using Web Crypto API.
|
|
33
|
+
*
|
|
34
|
+
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
35
|
+
* @returns 128-character hexadecimal hash string.
|
|
36
|
+
*/
|
|
37
|
+
export declare const hash_sha512: (data: BufferSource | string) => Promise<string>;
|
|
16
38
|
/**
|
|
17
39
|
* Computes a fast non-cryptographic hash using DJB2 algorithm.
|
|
18
40
|
* Use for content comparison and cache keys, not security.
|
package/dist/hash.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/hash.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/hash.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAmBH;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,YAAY,GAAG,MAAM,KAAG,OAAO,CAAC,MAAM,CACxC,CAAC;AAE/B;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,MAAM,YAAY,GAAG,MAAM,KAAG,OAAO,CAAC,MAAM,CACxC,CAAC;AAEjC;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,MAAM,YAAY,GAAG,MAAM,KAAG,OAAO,CAAC,MAAM,CACxC,CAAC;AAEjC;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,MAAM,YAAY,GAAG,MAAM,KAAG,OAAO,CAAC,MAAM,CACxC,CAAC;AAEjC;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,YAAY,GAAG,MAAM,KAAG,MAkB3D,CAAC"}
|
package/dist/hash.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Hash utilities
|
|
2
|
+
* Hash utilities using Web Crypto API and DJB2.
|
|
3
3
|
*
|
|
4
|
-
* Provides `hash_sha256` (Web Crypto, async)
|
|
4
|
+
* Provides `hash_sha256`, `hash_sha384`, `hash_sha512`, `hash_sha1` (Web Crypto, async)
|
|
5
|
+
* and `hash_insecure` (DJB2, fast non-cryptographic).
|
|
5
6
|
* For BLAKE3, see `hash_blake3` in `hash_blake3.ts`.
|
|
6
7
|
*
|
|
7
8
|
* @module
|
|
@@ -9,16 +10,45 @@
|
|
|
9
10
|
import { to_hex } from './hex.js';
|
|
10
11
|
const encoder = new TextEncoder();
|
|
11
12
|
/**
|
|
12
|
-
* Computes a
|
|
13
|
+
* Computes a hash using Web Crypto API.
|
|
13
14
|
*
|
|
15
|
+
* @param algorithm - Web Crypto algorithm name (e.g. `'SHA-256'`).
|
|
14
16
|
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
15
|
-
* @returns
|
|
17
|
+
* @returns hexadecimal hash string.
|
|
16
18
|
*/
|
|
17
|
-
|
|
19
|
+
const hash_webcrypto = async (algorithm, data) => {
|
|
18
20
|
const buffer = typeof data === 'string' ? encoder.encode(data) : data;
|
|
19
|
-
const digested = await crypto.subtle.digest(
|
|
21
|
+
const digested = await crypto.subtle.digest(algorithm, buffer);
|
|
20
22
|
return to_hex(new Uint8Array(digested));
|
|
21
23
|
};
|
|
24
|
+
/**
|
|
25
|
+
* Computes a SHA-1 hash using Web Crypto API.
|
|
26
|
+
*
|
|
27
|
+
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
28
|
+
* @returns 40-character hexadecimal hash string.
|
|
29
|
+
*/
|
|
30
|
+
export const hash_sha1 = (data) => hash_webcrypto('SHA-1', data);
|
|
31
|
+
/**
|
|
32
|
+
* Computes a SHA-256 hash using Web Crypto API.
|
|
33
|
+
*
|
|
34
|
+
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
35
|
+
* @returns 64-character hexadecimal hash string.
|
|
36
|
+
*/
|
|
37
|
+
export const hash_sha256 = (data) => hash_webcrypto('SHA-256', data);
|
|
38
|
+
/**
|
|
39
|
+
* Computes a SHA-384 hash using Web Crypto API.
|
|
40
|
+
*
|
|
41
|
+
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
42
|
+
* @returns 96-character hexadecimal hash string.
|
|
43
|
+
*/
|
|
44
|
+
export const hash_sha384 = (data) => hash_webcrypto('SHA-384', data);
|
|
45
|
+
/**
|
|
46
|
+
* Computes a SHA-512 hash using Web Crypto API.
|
|
47
|
+
*
|
|
48
|
+
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
49
|
+
* @returns 128-character hexadecimal hash string.
|
|
50
|
+
*/
|
|
51
|
+
export const hash_sha512 = (data) => hash_webcrypto('SHA-512', data);
|
|
22
52
|
/**
|
|
23
53
|
* Computes a fast non-cryptographic hash using DJB2 algorithm.
|
|
24
54
|
* Use for content comparison and cache keys, not security.
|
package/dist/hex.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Hex encoding helpers.
|
|
2
|
+
* Hex encoding and decoding helpers.
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
@@ -10,4 +10,12 @@
|
|
|
10
10
|
* @returns Hex string with two characters per byte.
|
|
11
11
|
*/
|
|
12
12
|
export declare const to_hex: (bytes: Uint8Array) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Decodes a hex string to a `Uint8Array`.
|
|
15
|
+
* Whitespace is stripped before parsing. Returns `null` for invalid hex.
|
|
16
|
+
*
|
|
17
|
+
* @param hex - Hex string to decode (case-insensitive, whitespace allowed).
|
|
18
|
+
* @returns Decoded bytes, or `null` if the input is not valid hex.
|
|
19
|
+
*/
|
|
20
|
+
export declare const from_hex: (hex: string) => Uint8Array | null;
|
|
13
21
|
//# sourceMappingURL=hex.d.ts.map
|
package/dist/hex.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hex.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/hex.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;GAKG;AACH,eAAO,MAAM,MAAM,GAAI,OAAO,UAAU,KAAG,MAO1C,CAAC"}
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/hex.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;GAKG;AACH,eAAO,MAAM,MAAM,GAAI,OAAO,UAAU,KAAG,MAO1C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,KAAK,MAAM,KAAG,UAAU,GAAG,IAQnD,CAAC"}
|
package/dist/hex.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Hex encoding helpers.
|
|
2
|
+
* Hex encoding and decoding helpers.
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
@@ -17,6 +17,23 @@ export const to_hex = (bytes) => {
|
|
|
17
17
|
}
|
|
18
18
|
return hex;
|
|
19
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Decodes a hex string to a `Uint8Array`.
|
|
22
|
+
* Whitespace is stripped before parsing. Returns `null` for invalid hex.
|
|
23
|
+
*
|
|
24
|
+
* @param hex - Hex string to decode (case-insensitive, whitespace allowed).
|
|
25
|
+
* @returns Decoded bytes, or `null` if the input is not valid hex.
|
|
26
|
+
*/
|
|
27
|
+
export const from_hex = (hex) => {
|
|
28
|
+
const clean = hex.replace(/\s/g, '');
|
|
29
|
+
if (clean.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(clean))
|
|
30
|
+
return null;
|
|
31
|
+
const bytes = new Uint8Array(clean.length / 2);
|
|
32
|
+
for (let i = 0; i < clean.length; i += 2) {
|
|
33
|
+
bytes[i / 2] = parseInt(clean.substring(i, i + 2), 16);
|
|
34
|
+
}
|
|
35
|
+
return bytes;
|
|
36
|
+
};
|
|
20
37
|
// Lazily computed lookup table for byte to hex conversion
|
|
21
38
|
let byte_to_hex;
|
|
22
39
|
const get_byte_to_hex = () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fuzdev/fuz_util",
|
|
3
|
-
"version": "0.53.
|
|
3
|
+
"version": "0.53.1",
|
|
4
4
|
"description": "utility belt for JS",
|
|
5
5
|
"glyph": "🦕",
|
|
6
6
|
"logo": "logo.svg",
|
|
@@ -77,11 +77,11 @@
|
|
|
77
77
|
"@changesets/changelog-git": "^0.2.1",
|
|
78
78
|
"@fuzdev/blake3_wasm": "^0.1.0",
|
|
79
79
|
"@fuzdev/fuz_code": "^0.45.1",
|
|
80
|
-
"@fuzdev/fuz_css": "^0.
|
|
80
|
+
"@fuzdev/fuz_css": "^0.55.0",
|
|
81
81
|
"@fuzdev/fuz_ui": "^0.185.2",
|
|
82
|
-
"@fuzdev/gro": "^0.
|
|
82
|
+
"@fuzdev/gro": "^0.197.0",
|
|
83
83
|
"@jridgewell/trace-mapping": "^0.3.31",
|
|
84
|
-
"@ryanatkn/eslint-config": "^0.
|
|
84
|
+
"@ryanatkn/eslint-config": "^0.10.0",
|
|
85
85
|
"@sveltejs/adapter-static": "^3.0.10",
|
|
86
86
|
"@sveltejs/kit": "^2.50.1",
|
|
87
87
|
"@sveltejs/package": "^2.5.7",
|
package/src/lib/hash.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Hash utilities
|
|
2
|
+
* Hash utilities using Web Crypto API and DJB2.
|
|
3
3
|
*
|
|
4
|
-
* Provides `hash_sha256` (Web Crypto, async)
|
|
4
|
+
* Provides `hash_sha256`, `hash_sha384`, `hash_sha512`, `hash_sha1` (Web Crypto, async)
|
|
5
|
+
* and `hash_insecure` (DJB2, fast non-cryptographic).
|
|
5
6
|
* For BLAKE3, see `hash_blake3` in `hash_blake3.ts`.
|
|
6
7
|
*
|
|
7
8
|
* @module
|
|
@@ -12,17 +13,54 @@ import {to_hex} from './hex.js';
|
|
|
12
13
|
const encoder = new TextEncoder();
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
|
-
* Computes a
|
|
16
|
+
* Computes a hash using Web Crypto API.
|
|
16
17
|
*
|
|
18
|
+
* @param algorithm - Web Crypto algorithm name (e.g. `'SHA-256'`).
|
|
17
19
|
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
18
|
-
* @returns
|
|
20
|
+
* @returns hexadecimal hash string.
|
|
19
21
|
*/
|
|
20
|
-
|
|
22
|
+
const hash_webcrypto = async (algorithm: string, data: BufferSource | string): Promise<string> => {
|
|
21
23
|
const buffer = typeof data === 'string' ? encoder.encode(data) : data;
|
|
22
|
-
const digested = await crypto.subtle.digest(
|
|
24
|
+
const digested = await crypto.subtle.digest(algorithm, buffer);
|
|
23
25
|
return to_hex(new Uint8Array(digested));
|
|
24
26
|
};
|
|
25
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Computes a SHA-1 hash using Web Crypto API.
|
|
30
|
+
*
|
|
31
|
+
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
32
|
+
* @returns 40-character hexadecimal hash string.
|
|
33
|
+
*/
|
|
34
|
+
export const hash_sha1 = (data: BufferSource | string): Promise<string> =>
|
|
35
|
+
hash_webcrypto('SHA-1', data);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Computes a SHA-256 hash using Web Crypto API.
|
|
39
|
+
*
|
|
40
|
+
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
41
|
+
* @returns 64-character hexadecimal hash string.
|
|
42
|
+
*/
|
|
43
|
+
export const hash_sha256 = (data: BufferSource | string): Promise<string> =>
|
|
44
|
+
hash_webcrypto('SHA-256', data);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Computes a SHA-384 hash using Web Crypto API.
|
|
48
|
+
*
|
|
49
|
+
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
50
|
+
* @returns 96-character hexadecimal hash string.
|
|
51
|
+
*/
|
|
52
|
+
export const hash_sha384 = (data: BufferSource | string): Promise<string> =>
|
|
53
|
+
hash_webcrypto('SHA-384', data);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Computes a SHA-512 hash using Web Crypto API.
|
|
57
|
+
*
|
|
58
|
+
* @param data - String or binary data to hash. Strings are UTF-8 encoded.
|
|
59
|
+
* @returns 128-character hexadecimal hash string.
|
|
60
|
+
*/
|
|
61
|
+
export const hash_sha512 = (data: BufferSource | string): Promise<string> =>
|
|
62
|
+
hash_webcrypto('SHA-512', data);
|
|
63
|
+
|
|
26
64
|
/**
|
|
27
65
|
* Computes a fast non-cryptographic hash using DJB2 algorithm.
|
|
28
66
|
* Use for content comparison and cache keys, not security.
|
package/src/lib/hex.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Hex encoding helpers.
|
|
2
|
+
* Hex encoding and decoding helpers.
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
@@ -19,6 +19,23 @@ export const to_hex = (bytes: Uint8Array): string => {
|
|
|
19
19
|
return hex;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Decodes a hex string to a `Uint8Array`.
|
|
24
|
+
* Whitespace is stripped before parsing. Returns `null` for invalid hex.
|
|
25
|
+
*
|
|
26
|
+
* @param hex - Hex string to decode (case-insensitive, whitespace allowed).
|
|
27
|
+
* @returns Decoded bytes, or `null` if the input is not valid hex.
|
|
28
|
+
*/
|
|
29
|
+
export const from_hex = (hex: string): Uint8Array | null => {
|
|
30
|
+
const clean = hex.replace(/\s/g, '');
|
|
31
|
+
if (clean.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(clean)) return null;
|
|
32
|
+
const bytes = new Uint8Array(clean.length / 2);
|
|
33
|
+
for (let i = 0; i < clean.length; i += 2) {
|
|
34
|
+
bytes[i / 2] = parseInt(clean.substring(i, i + 2), 16);
|
|
35
|
+
}
|
|
36
|
+
return bytes;
|
|
37
|
+
};
|
|
38
|
+
|
|
22
39
|
// Lazily computed lookup table for byte to hex conversion
|
|
23
40
|
let byte_to_hex: Array<string> | undefined;
|
|
24
41
|
const get_byte_to_hex = (): Array<string> => {
|