@byline/admin 2.4.0 → 2.4.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/dist/abilities.js +5 -24
- package/dist/index.js +8 -30
- package/dist/lib/assert-admin-actor.js +13 -74
- package/dist/lib/create-command.js +6 -16
- package/dist/modules/admin-account/commands.js +35 -24
- package/dist/modules/admin-account/components/change-password.d.ts +8 -0
- package/dist/modules/admin-account/components/change-password.js +192 -0
- package/dist/modules/admin-account/components/change-password.module.js +8 -0
- package/dist/modules/admin-account/components/change-password_module.css +27 -0
- package/dist/modules/admin-account/components/container.d.ts +29 -0
- package/dist/modules/admin-account/components/container.js +298 -0
- package/dist/modules/admin-account/components/container.module.js +28 -0
- package/dist/modules/admin-account/components/container_module.css +106 -0
- package/dist/modules/admin-account/components/update.d.ts +8 -0
- package/dist/modules/admin-account/components/update.js +207 -0
- package/dist/modules/admin-account/components/update.module.js +8 -0
- package/dist/modules/admin-account/components/update_module.css +27 -0
- package/dist/modules/admin-account/errors.js +14 -45
- package/dist/modules/admin-account/index.js +4 -34
- package/dist/modules/admin-account/schemas.js +25 -59
- package/dist/modules/admin-account/service.js +56 -61
- package/dist/modules/admin-permissions/abilities.js +6 -24
- package/dist/modules/admin-permissions/commands.js +42 -28
- package/dist/modules/admin-permissions/components/inspector.d.ts +4 -0
- package/dist/modules/admin-permissions/components/inspector.js +284 -0
- package/dist/modules/admin-permissions/components/inspector.module.js +56 -0
- package/dist/modules/admin-permissions/components/inspector_module.css +238 -0
- package/dist/modules/admin-permissions/dto.js +3 -16
- package/dist/modules/admin-permissions/errors.js +14 -27
- package/dist/modules/admin-permissions/index.js +6 -26
- package/dist/modules/admin-permissions/repository.js +1 -8
- package/dist/modules/admin-permissions/schemas.js +33 -70
- package/dist/modules/admin-permissions/service.js +88 -92
- package/dist/modules/admin-roles/abilities.js +8 -30
- package/dist/modules/admin-roles/commands.js +89 -55
- package/dist/modules/admin-roles/components/create.d.ts +7 -0
- package/dist/modules/admin-roles/components/create.js +177 -0
- package/dist/modules/admin-roles/components/create.module.js +8 -0
- package/dist/modules/admin-roles/components/create_module.css +27 -0
- package/dist/modules/admin-roles/components/permissions.d.ts +10 -0
- package/dist/modules/admin-roles/components/permissions.js +303 -0
- package/dist/modules/admin-roles/components/permissions.module.js +44 -0
- package/dist/modules/admin-roles/components/permissions_module.css +192 -0
- package/dist/modules/admin-roles/components/update.d.ts +8 -0
- package/dist/modules/admin-roles/components/update.js +166 -0
- package/dist/modules/admin-roles/components/update.module.js +8 -0
- package/dist/modules/admin-roles/components/update_module.css +27 -0
- package/dist/modules/admin-roles/dto.js +3 -16
- package/dist/modules/admin-roles/errors.js +16 -40
- package/dist/modules/admin-roles/index.js +6 -26
- package/dist/modules/admin-roles/repository.js +1 -8
- package/dist/modules/admin-roles/schemas.js +41 -71
- package/dist/modules/admin-roles/service.js +79 -82
- package/dist/modules/admin-users/abilities.js +9 -38
- package/dist/modules/admin-users/commands.js +92 -50
- package/dist/modules/admin-users/components/create.d.ts +8 -0
- package/dist/modules/admin-users/components/create.js +268 -0
- package/dist/modules/admin-users/components/create.module.js +10 -0
- package/dist/modules/admin-users/components/create_module.css +45 -0
- package/dist/modules/admin-users/components/roles.d.ts +11 -0
- package/dist/modules/admin-users/components/roles.js +148 -0
- package/dist/modules/admin-users/components/roles.module.js +18 -0
- package/dist/modules/admin-users/components/roles_module.css +75 -0
- package/dist/modules/admin-users/components/set-password.d.ts +8 -0
- package/dist/modules/admin-users/components/set-password.js +170 -0
- package/dist/modules/admin-users/components/set-password.module.js +9 -0
- package/dist/modules/admin-users/components/set-password_module.css +31 -0
- package/dist/modules/admin-users/components/update.d.ts +8 -0
- package/dist/modules/admin-users/components/update.js +254 -0
- package/dist/modules/admin-users/components/update.module.js +9 -0
- package/dist/modules/admin-users/components/update_module.css +34 -0
- package/dist/modules/admin-users/dto.js +3 -18
- package/dist/modules/admin-users/errors.js +17 -43
- package/dist/modules/admin-users/index.js +7 -27
- package/dist/modules/admin-users/repository.js +1 -8
- package/dist/modules/admin-users/schemas.js +44 -75
- package/dist/modules/admin-users/seed-super-admin.js +9 -34
- package/dist/modules/admin-users/service.js +76 -91
- package/dist/modules/auth/components/sign-in-form.d.ts +12 -0
- package/dist/modules/auth/components/sign-in-form.js +115 -0
- package/dist/modules/auth/components/sign-in-form.module.js +12 -0
- package/dist/modules/auth/components/sign-in-form_module.css +41 -0
- package/dist/modules/auth/index.js +3 -24
- package/dist/modules/auth/jwt-session-provider.js +179 -149
- package/dist/modules/auth/password.js +11 -53
- package/dist/modules/auth/phc.js +21 -54
- package/dist/modules/auth/refresh-tokens-repository.js +1 -8
- package/dist/modules/auth/resolve-actor.js +6 -28
- package/dist/services/admin-services-context.d.ts +16 -0
- package/dist/services/admin-services-context.js +13 -0
- package/dist/services/admin-services-types.d.ts +129 -0
- package/dist/services/admin-services-types.js +1 -0
- package/dist/store.js +1 -8
- package/dist/vendor/noble-argon2/_blake.js +277 -45
- package/dist/vendor/noble-argon2/_md.js +81 -136
- package/dist/vendor/noble-argon2/_u64.js +65 -67
- package/dist/vendor/noble-argon2/argon2.js +181 -342
- package/dist/vendor/noble-argon2/blake2.js +252 -327
- package/dist/vendor/noble-argon2/utils.js +110 -490
- package/dist/vendor/noble-argon2/utils.js.LICENSE.txt +1 -0
- package/package.json +89 -10
- package/src/abilities.ts +32 -0
- package/src/declarations.d.ts +4 -0
- package/src/index.ts +39 -0
- package/src/lib/assert-admin-actor.ts +90 -0
- package/src/lib/create-command.ts +109 -0
- package/src/modules/admin-account/commands.ts +76 -0
- package/src/modules/admin-account/components/change-password.module.css +40 -0
- package/src/modules/admin-account/components/change-password.tsx +232 -0
- package/src/modules/admin-account/components/container.module.css +158 -0
- package/src/modules/admin-account/components/container.tsx +229 -0
- package/src/modules/admin-account/components/update.module.css +40 -0
- package/src/modules/admin-account/components/update.tsx +263 -0
- package/src/modules/admin-account/errors.ts +75 -0
- package/src/modules/admin-account/index.ts +60 -0
- package/src/modules/admin-account/schemas.ts +84 -0
- package/src/modules/admin-account/service.ts +92 -0
- package/src/modules/admin-permissions/abilities.ts +46 -0
- package/src/modules/admin-permissions/commands.ts +103 -0
- package/src/modules/admin-permissions/components/inspector.module.css +326 -0
- package/src/modules/admin-permissions/components/inspector.tsx +298 -0
- package/src/modules/admin-permissions/dto.ts +28 -0
- package/src/modules/admin-permissions/errors.ts +57 -0
- package/src/modules/admin-permissions/index.ts +72 -0
- package/src/modules/admin-permissions/repository.ts +49 -0
- package/src/modules/admin-permissions/schemas.ts +128 -0
- package/src/modules/admin-permissions/service.ts +137 -0
- package/src/modules/admin-roles/abilities.ts +62 -0
- package/src/modules/admin-roles/commands.ts +161 -0
- package/src/modules/admin-roles/components/create.module.css +40 -0
- package/src/modules/admin-roles/components/create.tsx +218 -0
- package/src/modules/admin-roles/components/permissions.module.css +279 -0
- package/src/modules/admin-roles/components/permissions.tsx +396 -0
- package/src/modules/admin-roles/components/update.module.css +40 -0
- package/src/modules/admin-roles/components/update.tsx +218 -0
- package/src/modules/admin-roles/dto.ts +30 -0
- package/src/modules/admin-roles/errors.ts +76 -0
- package/src/modules/admin-roles/index.ts +81 -0
- package/src/modules/admin-roles/repository.ts +96 -0
- package/src/modules/admin-roles/schemas.ts +139 -0
- package/src/modules/admin-roles/service.ts +136 -0
- package/src/modules/admin-users/abilities.ts +76 -0
- package/src/modules/admin-users/commands.ts +157 -0
- package/src/modules/admin-users/components/create.module.css +63 -0
- package/src/modules/admin-users/components/create.tsx +323 -0
- package/src/modules/admin-users/components/roles.module.css +119 -0
- package/src/modules/admin-users/components/roles.tsx +172 -0
- package/src/modules/admin-users/components/set-password.module.css +46 -0
- package/src/modules/admin-users/components/set-password.tsx +199 -0
- package/src/modules/admin-users/components/update.module.css +49 -0
- package/src/modules/admin-users/components/update.tsx +328 -0
- package/src/modules/admin-users/dto.ts +39 -0
- package/src/modules/admin-users/errors.ts +84 -0
- package/src/modules/admin-users/index.ts +91 -0
- package/src/modules/admin-users/repository.ts +161 -0
- package/src/modules/admin-users/schemas.ts +168 -0
- package/src/modules/admin-users/seed-super-admin.ts +102 -0
- package/src/modules/admin-users/service.ts +166 -0
- package/src/modules/auth/components/sign-in-form.module.css +62 -0
- package/src/modules/auth/components/sign-in-form.tsx +132 -0
- package/src/modules/auth/index.ts +31 -0
- package/src/modules/auth/jwt-session-provider.ts +301 -0
- package/src/modules/auth/password.ts +94 -0
- package/src/modules/auth/phc.ts +121 -0
- package/src/modules/auth/refresh-tokens-repository.ts +74 -0
- package/src/modules/auth/resolve-actor.ts +42 -0
- package/src/services/admin-services-context.tsx +52 -0
- package/src/services/admin-services-types.ts +177 -0
- package/src/store.ts +32 -0
- package/src/vendor/noble-argon2/LICENSE +21 -0
- package/src/vendor/noble-argon2/README.md +87 -0
- package/src/vendor/noble-argon2/_blake.ts +58 -0
- package/src/vendor/noble-argon2/_md.ts +223 -0
- package/src/vendor/noble-argon2/_u64.ts +118 -0
- package/src/vendor/noble-argon2/argon2.ts +668 -0
- package/src/vendor/noble-argon2/blake2.ts +583 -0
- package/src/vendor/noble-argon2/utils.ts +849 -0
|
@@ -0,0 +1,849 @@
|
|
|
1
|
+
// @ts-nocheck — vendored from noble-hashes; see ./README.md
|
|
2
|
+
/**
|
|
3
|
+
* Utilities for hex, bytes, CSPRNG.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
7
|
+
/**
|
|
8
|
+
* Bytes API type helpers for old + new TypeScript.
|
|
9
|
+
*
|
|
10
|
+
* TS 5.6 has `Uint8Array`, while TS 5.9+ made it generic `Uint8Array<ArrayBuffer>`.
|
|
11
|
+
* We can't use specific return type, because TS 5.6 will error.
|
|
12
|
+
* We can't use generic return type, because most TS 5.9 software will expect specific type.
|
|
13
|
+
*
|
|
14
|
+
* Maps typed-array input leaves to broad forms.
|
|
15
|
+
* These are compatibility adapters, not ownership guarantees.
|
|
16
|
+
*
|
|
17
|
+
* - `TArg` keeps byte inputs broad.
|
|
18
|
+
* - `TRet` marks byte outputs for TS 5.6 and TS 5.9+ compatibility.
|
|
19
|
+
*/
|
|
20
|
+
export type TypedArg<T> = T extends BigInt64Array
|
|
21
|
+
? BigInt64Array
|
|
22
|
+
: T extends BigUint64Array
|
|
23
|
+
? BigUint64Array
|
|
24
|
+
: T extends Float32Array
|
|
25
|
+
? Float32Array
|
|
26
|
+
: T extends Float64Array
|
|
27
|
+
? Float64Array
|
|
28
|
+
: T extends Int16Array
|
|
29
|
+
? Int16Array
|
|
30
|
+
: T extends Int32Array
|
|
31
|
+
? Int32Array
|
|
32
|
+
: T extends Int8Array
|
|
33
|
+
? Int8Array
|
|
34
|
+
: T extends Uint16Array
|
|
35
|
+
? Uint16Array
|
|
36
|
+
: T extends Uint32Array
|
|
37
|
+
? Uint32Array
|
|
38
|
+
: T extends Uint8ClampedArray
|
|
39
|
+
? Uint8ClampedArray
|
|
40
|
+
: T extends Uint8Array
|
|
41
|
+
? Uint8Array
|
|
42
|
+
: never;
|
|
43
|
+
/** Maps typed-array output leaves to narrow TS-compatible forms. */
|
|
44
|
+
export type TypedRet<T> = T extends BigInt64Array
|
|
45
|
+
? ReturnType<typeof BigInt64Array.of>
|
|
46
|
+
: T extends BigUint64Array
|
|
47
|
+
? ReturnType<typeof BigUint64Array.of>
|
|
48
|
+
: T extends Float32Array
|
|
49
|
+
? ReturnType<typeof Float32Array.of>
|
|
50
|
+
: T extends Float64Array
|
|
51
|
+
? ReturnType<typeof Float64Array.of>
|
|
52
|
+
: T extends Int16Array
|
|
53
|
+
? ReturnType<typeof Int16Array.of>
|
|
54
|
+
: T extends Int32Array
|
|
55
|
+
? ReturnType<typeof Int32Array.of>
|
|
56
|
+
: T extends Int8Array
|
|
57
|
+
? ReturnType<typeof Int8Array.of>
|
|
58
|
+
: T extends Uint16Array
|
|
59
|
+
? ReturnType<typeof Uint16Array.of>
|
|
60
|
+
: T extends Uint32Array
|
|
61
|
+
? ReturnType<typeof Uint32Array.of>
|
|
62
|
+
: T extends Uint8ClampedArray
|
|
63
|
+
? ReturnType<typeof Uint8ClampedArray.of>
|
|
64
|
+
: T extends Uint8Array
|
|
65
|
+
? ReturnType<typeof Uint8Array.of>
|
|
66
|
+
: never;
|
|
67
|
+
/** Recursively adapts byte-carrying API input types. See {@link TypedArg}. */
|
|
68
|
+
export type TArg<T> =
|
|
69
|
+
| T
|
|
70
|
+
| ([TypedArg<T>] extends [never]
|
|
71
|
+
? T extends (...args: infer A) => infer R
|
|
72
|
+
? ((...args: { [K in keyof A]: TRet<A[K]> }) => TArg<R>) & {
|
|
73
|
+
[K in keyof T]: T[K] extends (...args: any) => any ? T[K] : TArg<T[K]>;
|
|
74
|
+
}
|
|
75
|
+
: T extends [infer A, ...infer R]
|
|
76
|
+
? [TArg<A>, ...{ [K in keyof R]: TArg<R[K]> }]
|
|
77
|
+
: T extends readonly [infer A, ...infer R]
|
|
78
|
+
? readonly [TArg<A>, ...{ [K in keyof R]: TArg<R[K]> }]
|
|
79
|
+
: T extends (infer A)[]
|
|
80
|
+
? TArg<A>[]
|
|
81
|
+
: T extends readonly (infer A)[]
|
|
82
|
+
? readonly TArg<A>[]
|
|
83
|
+
: T extends Promise<infer A>
|
|
84
|
+
? Promise<TArg<A>>
|
|
85
|
+
: T extends object
|
|
86
|
+
? { [K in keyof T]: TArg<T[K]> }
|
|
87
|
+
: T
|
|
88
|
+
: TypedArg<T>);
|
|
89
|
+
/** Recursively adapts byte-carrying API output types. See {@link TypedArg}. */
|
|
90
|
+
export type TRet<T> = T extends unknown
|
|
91
|
+
? T &
|
|
92
|
+
([TypedRet<T>] extends [never]
|
|
93
|
+
? T extends (...args: infer A) => infer R
|
|
94
|
+
? ((...args: { [K in keyof A]: TArg<A[K]> }) => TRet<R>) & {
|
|
95
|
+
[K in keyof T]: T[K] extends (...args: any) => any ? T[K] : TRet<T[K]>;
|
|
96
|
+
}
|
|
97
|
+
: T extends [infer A, ...infer R]
|
|
98
|
+
? [TRet<A>, ...{ [K in keyof R]: TRet<R[K]> }]
|
|
99
|
+
: T extends readonly [infer A, ...infer R]
|
|
100
|
+
? readonly [TRet<A>, ...{ [K in keyof R]: TRet<R[K]> }]
|
|
101
|
+
: T extends (infer A)[]
|
|
102
|
+
? TRet<A>[]
|
|
103
|
+
: T extends readonly (infer A)[]
|
|
104
|
+
? readonly TRet<A>[]
|
|
105
|
+
: T extends Promise<infer A>
|
|
106
|
+
? Promise<TRet<A>>
|
|
107
|
+
: T extends object
|
|
108
|
+
? { [K in keyof T]: TRet<T[K]> }
|
|
109
|
+
: T
|
|
110
|
+
: TypedRet<T>)
|
|
111
|
+
: never;
|
|
112
|
+
/**
|
|
113
|
+
* Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
|
|
114
|
+
* @param a - value to test
|
|
115
|
+
* @returns `true` when the value is a Uint8Array-compatible view.
|
|
116
|
+
* @example
|
|
117
|
+
* Check whether a value is a Uint8Array-compatible view.
|
|
118
|
+
* ```ts
|
|
119
|
+
* isBytes(new Uint8Array([1, 2, 3]));
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export function isBytes(a: unknown): a is Uint8Array {
|
|
123
|
+
// Plain `instanceof Uint8Array` is too strict for some Buffer / proxy / cross-realm cases.
|
|
124
|
+
// The fallback still requires a real ArrayBuffer view, so plain
|
|
125
|
+
// JSON-deserialized `{ constructor: ... }` spoofing is rejected, and
|
|
126
|
+
// `BYTES_PER_ELEMENT === 1` keeps the fallback on byte-oriented views.
|
|
127
|
+
return (
|
|
128
|
+
a instanceof Uint8Array ||
|
|
129
|
+
(ArrayBuffer.isView(a) &&
|
|
130
|
+
a.constructor.name === 'Uint8Array' &&
|
|
131
|
+
'BYTES_PER_ELEMENT' in a &&
|
|
132
|
+
a.BYTES_PER_ELEMENT === 1)
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Asserts something is a non-negative integer.
|
|
138
|
+
* @param n - number to validate
|
|
139
|
+
* @param title - label included in thrown errors
|
|
140
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
141
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
142
|
+
* @example
|
|
143
|
+
* Validate a non-negative integer option.
|
|
144
|
+
* ```ts
|
|
145
|
+
* anumber(32, 'length');
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
export function anumber(n: number, title: string = ''): void {
|
|
149
|
+
if (typeof n !== 'number') {
|
|
150
|
+
const prefix = title && `"${title}" `;
|
|
151
|
+
throw new TypeError(`${prefix}expected number, got ${typeof n}`);
|
|
152
|
+
}
|
|
153
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
154
|
+
const prefix = title && `"${title}" `;
|
|
155
|
+
throw new RangeError(`${prefix}expected integer >= 0, got ${n}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Asserts something is Uint8Array.
|
|
161
|
+
* @param value - value to validate
|
|
162
|
+
* @param length - optional exact length constraint
|
|
163
|
+
* @param title - label included in thrown errors
|
|
164
|
+
* @returns The validated byte array.
|
|
165
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
166
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
167
|
+
* @example
|
|
168
|
+
* Validate that a value is a byte array.
|
|
169
|
+
* ```ts
|
|
170
|
+
* abytes(new Uint8Array([1, 2, 3]));
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export function abytes(
|
|
174
|
+
value: TArg<Uint8Array>,
|
|
175
|
+
length?: number,
|
|
176
|
+
title: string = ''
|
|
177
|
+
): TRet<Uint8Array> {
|
|
178
|
+
const bytes = isBytes(value);
|
|
179
|
+
const len = value?.length;
|
|
180
|
+
const needsLen = length !== undefined;
|
|
181
|
+
if (!bytes || (needsLen && len !== length)) {
|
|
182
|
+
const prefix = title && `"${title}" `;
|
|
183
|
+
const ofLen = needsLen ? ` of length ${length}` : '';
|
|
184
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
185
|
+
const message = prefix + 'expected Uint8Array' + ofLen + ', got ' + got;
|
|
186
|
+
if (!bytes) throw new TypeError(message);
|
|
187
|
+
throw new RangeError(message);
|
|
188
|
+
}
|
|
189
|
+
return value as TRet<Uint8Array>;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Copies bytes into a fresh Uint8Array.
|
|
194
|
+
* Buffer-style slices can alias the same backing store, so callers that need ownership should copy.
|
|
195
|
+
* @param bytes - source bytes to clone
|
|
196
|
+
* @returns Freshly allocated copy of `bytes`.
|
|
197
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
198
|
+
* @example
|
|
199
|
+
* Clone a byte array before mutating it.
|
|
200
|
+
* ```ts
|
|
201
|
+
* const copy = copyBytes(new Uint8Array([1, 2, 3]));
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
export function copyBytes(bytes: TArg<Uint8Array>): TRet<Uint8Array> {
|
|
205
|
+
// `Uint8Array.from(...)` would also accept arrays / other typed arrays. Keep this helper strict
|
|
206
|
+
// because callers use it at byte-validation boundaries before mutating the detached copy.
|
|
207
|
+
return Uint8Array.from(abytes(bytes)) as TRet<Uint8Array>;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Asserts something is a wrapped hash constructor.
|
|
212
|
+
* @param h - hash constructor to validate
|
|
213
|
+
* @throws On wrong argument types or invalid hash wrapper shape. {@link TypeError}
|
|
214
|
+
* @throws On invalid hash metadata ranges or values. {@link RangeError}
|
|
215
|
+
* @throws If the hash metadata allows empty outputs or block sizes. {@link Error}
|
|
216
|
+
* @example
|
|
217
|
+
* Validate a callable hash wrapper.
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { ahash } from '@noble/hashes/utils.js';
|
|
220
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
221
|
+
* ahash(sha256);
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
export function ahash(h: TArg<CHash>): void {
|
|
225
|
+
if (typeof h !== 'function' || typeof h.create !== 'function')
|
|
226
|
+
throw new TypeError('Hash must wrapped by utils.createHasher');
|
|
227
|
+
anumber(h.outputLen);
|
|
228
|
+
anumber(h.blockLen);
|
|
229
|
+
// HMAC and KDF callers treat these as real byte lengths; allowing zero lets fake wrappers pass
|
|
230
|
+
// validation and can produce empty outputs instead of failing fast.
|
|
231
|
+
if (h.outputLen < 1) throw new Error('"outputLen" must be >= 1');
|
|
232
|
+
if (h.blockLen < 1) throw new Error('"blockLen" must be >= 1');
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Asserts a hash instance has not been destroyed or finished.
|
|
237
|
+
* @param instance - hash instance to validate
|
|
238
|
+
* @param checkFinished - whether to reject finalized instances
|
|
239
|
+
* @throws If the hash instance has already been destroyed or finalized. {@link Error}
|
|
240
|
+
* @example
|
|
241
|
+
* Validate that a hash instance is still usable.
|
|
242
|
+
* ```ts
|
|
243
|
+
* import { aexists } from '@noble/hashes/utils.js';
|
|
244
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
245
|
+
* const hash = sha256.create();
|
|
246
|
+
* aexists(hash);
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
export function aexists(instance: any, checkFinished = true): void {
|
|
250
|
+
if (instance.destroyed) throw new Error('Hash instance has been destroyed');
|
|
251
|
+
if (checkFinished && instance.finished) throw new Error('Hash#digest() has already been called');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Asserts output is a sufficiently-sized byte array.
|
|
256
|
+
* @param out - destination buffer
|
|
257
|
+
* @param instance - hash instance providing output length
|
|
258
|
+
* Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
|
|
259
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
260
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
261
|
+
* @example
|
|
262
|
+
* Validate a caller-provided digest buffer.
|
|
263
|
+
* ```ts
|
|
264
|
+
* import { aoutput } from '@noble/hashes/utils.js';
|
|
265
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
266
|
+
* const hash = sha256.create();
|
|
267
|
+
* aoutput(new Uint8Array(hash.outputLen), hash);
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
export function aoutput(out: any, instance: any): void {
|
|
271
|
+
abytes(out, undefined, 'digestInto() output');
|
|
272
|
+
const min = instance.outputLen;
|
|
273
|
+
if (out.length < min) {
|
|
274
|
+
throw new RangeError('"digestInto() output" expected to be of length >=' + min);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Generic type encompassing 8/16/32-byte array views, but not 64-bit variants. */
|
|
279
|
+
// prettier-ignore
|
|
280
|
+
export type TypedArray = Int8Array | Uint8ClampedArray | Uint8Array |
|
|
281
|
+
Uint16Array | Int16Array | Uint32Array | Int32Array;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Casts a typed array view to Uint8Array.
|
|
285
|
+
* @param arr - source typed array
|
|
286
|
+
* @returns Uint8Array view over the same buffer.
|
|
287
|
+
* @example
|
|
288
|
+
* Reinterpret a typed array as bytes.
|
|
289
|
+
* ```ts
|
|
290
|
+
* u8(new Uint32Array([1, 2]));
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
export function u8(arr: TArg<TypedArray>): TRet<Uint8Array> {
|
|
294
|
+
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength) as TRet<Uint8Array>;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Casts a typed array view to Uint32Array.
|
|
299
|
+
* `arr.byteOffset` must already be 4-byte aligned or the platform
|
|
300
|
+
* Uint32Array constructor will throw.
|
|
301
|
+
* @param arr - source typed array
|
|
302
|
+
* @returns Uint32Array view over the same buffer.
|
|
303
|
+
* @example
|
|
304
|
+
* Reinterpret a byte array as 32-bit words.
|
|
305
|
+
* ```ts
|
|
306
|
+
* u32(new Uint8Array(8));
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
export function u32(arr: TArg<TypedArray>): TRet<Uint32Array> {
|
|
310
|
+
return new Uint32Array(
|
|
311
|
+
arr.buffer,
|
|
312
|
+
arr.byteOffset,
|
|
313
|
+
Math.floor(arr.byteLength / 4)
|
|
314
|
+
) as TRet<Uint32Array>;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Zeroizes typed arrays in place. Warning: JS provides no guarantees.
|
|
319
|
+
* @param arrays - arrays to overwrite with zeros
|
|
320
|
+
* @example
|
|
321
|
+
* Zeroize sensitive buffers in place.
|
|
322
|
+
* ```ts
|
|
323
|
+
* clean(new Uint8Array([1, 2, 3]));
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
export function clean(...arrays: TArg<TypedArray[]>): void {
|
|
327
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
328
|
+
arrays[i].fill(0);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Creates a DataView for byte-level manipulation.
|
|
334
|
+
* @param arr - source typed array
|
|
335
|
+
* @returns DataView over the same buffer region.
|
|
336
|
+
* @example
|
|
337
|
+
* Create a DataView over an existing buffer.
|
|
338
|
+
* ```ts
|
|
339
|
+
* createView(new Uint8Array(4));
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
export function createView(arr: TArg<TypedArray>): DataView {
|
|
343
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Rotate-right operation for uint32 values.
|
|
348
|
+
* @param word - source word
|
|
349
|
+
* @param shift - shift amount in bits
|
|
350
|
+
* @returns Rotated word.
|
|
351
|
+
* @example
|
|
352
|
+
* Rotate a 32-bit word to the right.
|
|
353
|
+
* ```ts
|
|
354
|
+
* rotr(0x12345678, 8);
|
|
355
|
+
* ```
|
|
356
|
+
*/
|
|
357
|
+
export function rotr(word: number, shift: number): number {
|
|
358
|
+
return (word << (32 - shift)) | (word >>> shift);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Rotate-left operation for uint32 values.
|
|
363
|
+
* @param word - source word
|
|
364
|
+
* @param shift - shift amount in bits
|
|
365
|
+
* @returns Rotated word.
|
|
366
|
+
* @example
|
|
367
|
+
* Rotate a 32-bit word to the left.
|
|
368
|
+
* ```ts
|
|
369
|
+
* rotl(0x12345678, 8);
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
372
|
+
export function rotl(word: number, shift: number): number {
|
|
373
|
+
return (word << shift) | ((word >>> (32 - shift)) >>> 0);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Whether the current platform is little-endian. */
|
|
377
|
+
export const isLE: boolean = /* @__PURE__ */ (() =>
|
|
378
|
+
new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Byte-swap operation for uint32 values.
|
|
382
|
+
* @param word - source word
|
|
383
|
+
* @returns Word with reversed byte order.
|
|
384
|
+
* @example
|
|
385
|
+
* Reverse the byte order of a 32-bit word.
|
|
386
|
+
* ```ts
|
|
387
|
+
* byteSwap(0x11223344);
|
|
388
|
+
* ```
|
|
389
|
+
*/
|
|
390
|
+
export function byteSwap(word: number): number {
|
|
391
|
+
return (
|
|
392
|
+
((word << 24) & 0xff000000) |
|
|
393
|
+
((word << 8) & 0xff0000) |
|
|
394
|
+
((word >>> 8) & 0xff00) |
|
|
395
|
+
((word >>> 24) & 0xff)
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Conditionally byte-swaps one 32-bit word on big-endian platforms.
|
|
400
|
+
* @param n - source word
|
|
401
|
+
* @returns Original or byte-swapped word depending on platform endianness.
|
|
402
|
+
* @example
|
|
403
|
+
* Normalize a 32-bit word for host endianness.
|
|
404
|
+
* ```ts
|
|
405
|
+
* swap8IfBE(0x11223344);
|
|
406
|
+
* ```
|
|
407
|
+
*/
|
|
408
|
+
export const swap8IfBE: (n: number) => number = isLE
|
|
409
|
+
? (n: number) => n
|
|
410
|
+
: (n: number) => byteSwap(n) >>> 0;
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Byte-swaps every word of a Uint32Array in place.
|
|
414
|
+
* @param arr - array to mutate
|
|
415
|
+
* @returns The same array after mutation; callers pass live state arrays here.
|
|
416
|
+
* @example
|
|
417
|
+
* Reverse the byte order of every word in place.
|
|
418
|
+
* ```ts
|
|
419
|
+
* byteSwap32(new Uint32Array([0x11223344]));
|
|
420
|
+
* ```
|
|
421
|
+
*/
|
|
422
|
+
export function byteSwap32(arr: TArg<Uint32Array>): TRet<Uint32Array> {
|
|
423
|
+
for (let i = 0; i < arr.length; i++) {
|
|
424
|
+
arr[i] = byteSwap(arr[i]);
|
|
425
|
+
}
|
|
426
|
+
return arr as TRet<Uint32Array>;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Conditionally byte-swaps a Uint32Array on big-endian platforms.
|
|
431
|
+
* @param u - array to normalize for host endianness
|
|
432
|
+
* @returns Original or byte-swapped array depending on platform endianness.
|
|
433
|
+
* On big-endian runtimes this mutates `u` in place via `byteSwap32(...)`.
|
|
434
|
+
* @example
|
|
435
|
+
* Normalize a word array for host endianness.
|
|
436
|
+
* ```ts
|
|
437
|
+
* swap32IfBE(new Uint32Array([0x11223344]));
|
|
438
|
+
* ```
|
|
439
|
+
*/
|
|
440
|
+
export const swap32IfBE: (u: TArg<Uint32Array>) => TRet<Uint32Array> = isLE
|
|
441
|
+
? (u: TArg<Uint32Array>) => u as TRet<Uint32Array>
|
|
442
|
+
: byteSwap32;
|
|
443
|
+
|
|
444
|
+
// Built-in hex conversion https://caniuse.com/mdn-javascript_builtins_uint8array_fromhex
|
|
445
|
+
const hasHexBuiltin: boolean = /* @__PURE__ */ (() =>
|
|
446
|
+
// @ts-ignore
|
|
447
|
+
typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
|
448
|
+
|
|
449
|
+
// Array where index 0xf0 (240) is mapped to string 'f0'
|
|
450
|
+
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) =>
|
|
451
|
+
i.toString(16).padStart(2, '0')
|
|
452
|
+
);
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Convert byte array to hex string.
|
|
456
|
+
* Uses the built-in function when available and assumes it matches the tested
|
|
457
|
+
* fallback semantics.
|
|
458
|
+
* @param bytes - bytes to encode
|
|
459
|
+
* @returns Lowercase hexadecimal string.
|
|
460
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
461
|
+
* @example
|
|
462
|
+
* Convert bytes to lowercase hexadecimal.
|
|
463
|
+
* ```ts
|
|
464
|
+
* bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])); // 'cafe0123'
|
|
465
|
+
* ```
|
|
466
|
+
*/
|
|
467
|
+
export function bytesToHex(bytes: TArg<Uint8Array>): string {
|
|
468
|
+
abytes(bytes);
|
|
469
|
+
// @ts-ignore
|
|
470
|
+
if (hasHexBuiltin) return bytes.toHex();
|
|
471
|
+
// pre-caching improves the speed 6x
|
|
472
|
+
let hex = '';
|
|
473
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
474
|
+
hex += hexes[bytes[i]];
|
|
475
|
+
}
|
|
476
|
+
return hex;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// We use optimized technique to convert hex string to byte array
|
|
480
|
+
const asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 } as const;
|
|
481
|
+
function asciiToBase16(ch: number): number | undefined {
|
|
482
|
+
if (ch >= asciis._0 && ch <= asciis._9) return ch - asciis._0; // '2' => 50-48
|
|
483
|
+
if (ch >= asciis.A && ch <= asciis.F) return ch - (asciis.A - 10); // 'B' => 66-(65-10)
|
|
484
|
+
if (ch >= asciis.a && ch <= asciis.f) return ch - (asciis.a - 10); // 'b' => 98-(97-10)
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Convert hex string to byte array. Uses built-in function, when available.
|
|
490
|
+
* @param hex - hexadecimal string to decode
|
|
491
|
+
* @returns Decoded bytes.
|
|
492
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
493
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
494
|
+
* @example
|
|
495
|
+
* Decode lowercase hexadecimal into bytes.
|
|
496
|
+
* ```ts
|
|
497
|
+
* hexToBytes('cafe0123'); // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
498
|
+
* ```
|
|
499
|
+
*/
|
|
500
|
+
export function hexToBytes(hex: string): TRet<Uint8Array> {
|
|
501
|
+
if (typeof hex !== 'string') throw new TypeError('hex string expected, got ' + typeof hex);
|
|
502
|
+
if (hasHexBuiltin) {
|
|
503
|
+
try {
|
|
504
|
+
return (Uint8Array as any).fromHex(hex);
|
|
505
|
+
} catch (error) {
|
|
506
|
+
if (error instanceof SyntaxError) throw new RangeError(error.message);
|
|
507
|
+
throw error;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
const hl = hex.length;
|
|
511
|
+
const al = hl / 2;
|
|
512
|
+
if (hl % 2) throw new RangeError('hex string expected, got unpadded hex of length ' + hl);
|
|
513
|
+
const array = new Uint8Array(al);
|
|
514
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
515
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
516
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
517
|
+
if (n1 === undefined || n2 === undefined) {
|
|
518
|
+
const char = hex[hi] + hex[hi + 1];
|
|
519
|
+
throw new RangeError(
|
|
520
|
+
'hex string expected, got non-hex character "' + char + '" at index ' + hi
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
array[ai] = n1 * 16 + n2; // multiply first octet, e.g. 'a3' => 10*16+3 => 160 + 3 => 163
|
|
524
|
+
}
|
|
525
|
+
return array;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* There is no setImmediate in browser and setTimeout is slow.
|
|
530
|
+
* This yields to the Promise/microtask scheduler queue, not to timers or the
|
|
531
|
+
* full macrotask event loop.
|
|
532
|
+
* @example
|
|
533
|
+
* Yield to the next scheduler tick.
|
|
534
|
+
* ```ts
|
|
535
|
+
* await nextTick();
|
|
536
|
+
* ```
|
|
537
|
+
*/
|
|
538
|
+
export const nextTick = async (): Promise<void> => {};
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Returns control to the Promise/microtask scheduler every `tick`
|
|
542
|
+
* milliseconds to avoid blocking long loops.
|
|
543
|
+
* @param iters - number of loop iterations to run
|
|
544
|
+
* @param tick - maximum time slice in milliseconds
|
|
545
|
+
* @param cb - callback executed on each iteration
|
|
546
|
+
* @example
|
|
547
|
+
* Run a loop that periodically yields back to the event loop.
|
|
548
|
+
* ```ts
|
|
549
|
+
* await asyncLoop(2, 0, () => {});
|
|
550
|
+
* ```
|
|
551
|
+
*/
|
|
552
|
+
export async function asyncLoop(
|
|
553
|
+
iters: number,
|
|
554
|
+
tick: number,
|
|
555
|
+
cb: (i: number) => void
|
|
556
|
+
): Promise<void> {
|
|
557
|
+
let ts = Date.now();
|
|
558
|
+
for (let i = 0; i < iters; i++) {
|
|
559
|
+
cb(i);
|
|
560
|
+
// Date.now() is not monotonic, so in case if clock goes backwards we return return control too
|
|
561
|
+
const diff = Date.now() - ts;
|
|
562
|
+
if (diff >= 0 && diff < tick) continue;
|
|
563
|
+
await nextTick();
|
|
564
|
+
ts += diff;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// Global symbols, but ts doesn't see them: https://github.com/microsoft/TypeScript/issues/31535
|
|
569
|
+
declare const TextEncoder: any;
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Converts string to bytes using UTF8 encoding.
|
|
573
|
+
* Built-in doesn't validate input to be string: we do the check.
|
|
574
|
+
* Non-ASCII details are delegated to the platform `TextEncoder`.
|
|
575
|
+
* @param str - string to encode
|
|
576
|
+
* @returns UTF-8 encoded bytes.
|
|
577
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
578
|
+
* @example
|
|
579
|
+
* Encode a string as UTF-8 bytes.
|
|
580
|
+
* ```ts
|
|
581
|
+
* utf8ToBytes('abc'); // Uint8Array.from([97, 98, 99])
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
export function utf8ToBytes(str: string): TRet<Uint8Array> {
|
|
585
|
+
if (typeof str !== 'string') throw new TypeError('string expected');
|
|
586
|
+
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/** KDFs can accept string or Uint8Array for user convenience. */
|
|
590
|
+
export type KDFInput = string | Uint8Array;
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Helper for KDFs: consumes Uint8Array or string.
|
|
594
|
+
* String inputs are UTF-8 encoded; byte-array inputs stay aliased to the caller buffer.
|
|
595
|
+
* @param data - user-provided KDF input
|
|
596
|
+
* @param errorTitle - label included in thrown errors
|
|
597
|
+
* @returns Byte representation of the input.
|
|
598
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
599
|
+
* @example
|
|
600
|
+
* Normalize KDF input to bytes.
|
|
601
|
+
* ```ts
|
|
602
|
+
* kdfInputToBytes('password');
|
|
603
|
+
* ```
|
|
604
|
+
*/
|
|
605
|
+
export function kdfInputToBytes(data: TArg<KDFInput>, errorTitle = ''): TRet<Uint8Array> {
|
|
606
|
+
if (typeof data === 'string') return utf8ToBytes(data);
|
|
607
|
+
return abytes(data, undefined, errorTitle);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Copies several Uint8Arrays into one.
|
|
612
|
+
* @param arrays - arrays to concatenate
|
|
613
|
+
* @returns Concatenated byte array.
|
|
614
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
615
|
+
* @example
|
|
616
|
+
* Concatenate multiple byte arrays.
|
|
617
|
+
* ```ts
|
|
618
|
+
* concatBytes(new Uint8Array([1]), new Uint8Array([2]));
|
|
619
|
+
* ```
|
|
620
|
+
*/
|
|
621
|
+
export function concatBytes(...arrays: TArg<Uint8Array[]>): TRet<Uint8Array> {
|
|
622
|
+
let sum = 0;
|
|
623
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
624
|
+
const a = arrays[i];
|
|
625
|
+
abytes(a);
|
|
626
|
+
sum += a.length;
|
|
627
|
+
}
|
|
628
|
+
const res = new Uint8Array(sum);
|
|
629
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
630
|
+
const a = arrays[i];
|
|
631
|
+
res.set(a, pad);
|
|
632
|
+
pad += a.length;
|
|
633
|
+
}
|
|
634
|
+
return res;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
type EmptyObj = {};
|
|
638
|
+
/**
|
|
639
|
+
* Merges default options and passed options.
|
|
640
|
+
* @param defaults - base option object
|
|
641
|
+
* @param opts - user overrides
|
|
642
|
+
* @returns Merged option object. The merge mutates `defaults` in place.
|
|
643
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
644
|
+
* @example
|
|
645
|
+
* Merge user overrides onto default options.
|
|
646
|
+
* ```ts
|
|
647
|
+
* checkOpts({ dkLen: 32 }, { asyncTick: 10 });
|
|
648
|
+
* ```
|
|
649
|
+
*/
|
|
650
|
+
export function checkOpts<T1 extends EmptyObj, T2 extends EmptyObj>(
|
|
651
|
+
defaults: T1,
|
|
652
|
+
opts?: T2
|
|
653
|
+
): T1 & T2 {
|
|
654
|
+
if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')
|
|
655
|
+
throw new TypeError('options must be object or undefined');
|
|
656
|
+
const merged = Object.assign(defaults, opts);
|
|
657
|
+
return merged as T1 & T2;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/** Common interface for all hash instances. */
|
|
661
|
+
export interface Hash<T> {
|
|
662
|
+
/** Bytes processed per compression block. */
|
|
663
|
+
blockLen: number;
|
|
664
|
+
/** Bytes produced by `digest()`. */
|
|
665
|
+
outputLen: number;
|
|
666
|
+
/** Whether the instance supports XOF-style variable-length output via `xof()` / `xofInto()`. */
|
|
667
|
+
canXOF: boolean;
|
|
668
|
+
/**
|
|
669
|
+
* Absorbs more message bytes into the running hash state.
|
|
670
|
+
* @param buf - message chunk to absorb
|
|
671
|
+
* @returns The same hash instance for chaining.
|
|
672
|
+
*/
|
|
673
|
+
update(buf: TArg<Uint8Array>): this;
|
|
674
|
+
/**
|
|
675
|
+
* Finalizes the hash into a caller-provided buffer.
|
|
676
|
+
* @param buf - destination buffer
|
|
677
|
+
* @returns Nothing. Implementations write into `buf` in place.
|
|
678
|
+
*/
|
|
679
|
+
digestInto(buf: TArg<Uint8Array>): void;
|
|
680
|
+
/**
|
|
681
|
+
* Finalizes the hash and returns a freshly allocated digest.
|
|
682
|
+
* @returns Digest bytes.
|
|
683
|
+
*/
|
|
684
|
+
digest(): TRet<Uint8Array>;
|
|
685
|
+
/** Wipes internal state and makes the instance unusable. */
|
|
686
|
+
destroy(): void;
|
|
687
|
+
/**
|
|
688
|
+
* Copies the current hash state into an existing or new instance.
|
|
689
|
+
* @param to - Optional destination instance to reuse.
|
|
690
|
+
* @returns Cloned hash state.
|
|
691
|
+
*/
|
|
692
|
+
_cloneInto(to?: T): T;
|
|
693
|
+
/**
|
|
694
|
+
* Creates an independent copy of the current hash state.
|
|
695
|
+
* @returns Cloned hash instance.
|
|
696
|
+
*/
|
|
697
|
+
clone(): T;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/** Pseudorandom generator interface. */
|
|
701
|
+
export interface PRG {
|
|
702
|
+
/**
|
|
703
|
+
* Mixes more entropy into the generator state.
|
|
704
|
+
* @param seed - fresh entropy bytes
|
|
705
|
+
* @returns Nothing. Implementations update internal state in place.
|
|
706
|
+
*/
|
|
707
|
+
addEntropy(seed: TArg<Uint8Array>): void;
|
|
708
|
+
/**
|
|
709
|
+
* Generates pseudorandom output bytes.
|
|
710
|
+
* @param length - number of bytes to generate
|
|
711
|
+
* @returns Generated pseudorandom bytes.
|
|
712
|
+
*/
|
|
713
|
+
randomBytes(length: number): TRet<Uint8Array>;
|
|
714
|
+
/** Wipes generator state and makes the instance unusable. */
|
|
715
|
+
clean(): void;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* XOF: streaming API to read digest in chunks.
|
|
720
|
+
* Same as 'squeeze' in keccak/k12 and 'seek' in blake3, but more generic name.
|
|
721
|
+
* When hash used in XOF mode it is up to user to call '.destroy' afterwards, since we cannot
|
|
722
|
+
* destroy state, next call can require more bytes.
|
|
723
|
+
*/
|
|
724
|
+
export type HashXOF<T extends Hash<T>> = Hash<T> & {
|
|
725
|
+
/**
|
|
726
|
+
* Reads more bytes from the XOF stream.
|
|
727
|
+
* @param bytes - number of bytes to read
|
|
728
|
+
* @returns Requested digest bytes.
|
|
729
|
+
*/
|
|
730
|
+
xof(bytes: number): TRet<Uint8Array>;
|
|
731
|
+
/**
|
|
732
|
+
* Reads more bytes from the XOF stream into a caller-provided buffer.
|
|
733
|
+
* @param buf - destination buffer
|
|
734
|
+
* @returns Filled output buffer.
|
|
735
|
+
*/
|
|
736
|
+
xofInto(buf: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
737
|
+
};
|
|
738
|
+
|
|
739
|
+
/** Hash constructor or factory type. */
|
|
740
|
+
export type HasherCons<T, Opts = undefined> = Opts extends undefined ? () => T : (opts?: Opts) => T;
|
|
741
|
+
/** Optional hash metadata. */
|
|
742
|
+
export type HashInfo = {
|
|
743
|
+
/** DER-encoded object identifier bytes for the hash algorithm. */
|
|
744
|
+
oid?: TRet<Uint8Array>;
|
|
745
|
+
};
|
|
746
|
+
/** Callable hash function type. */
|
|
747
|
+
export type CHash<T extends Hash<T> = Hash<any>, Opts = undefined> = {
|
|
748
|
+
/** Digest size in bytes. */
|
|
749
|
+
outputLen: number;
|
|
750
|
+
/** Input block size in bytes. */
|
|
751
|
+
blockLen: number;
|
|
752
|
+
/** Whether `.create()` returns a hash instance that can be used as an XOF stream. */
|
|
753
|
+
canXOF: boolean;
|
|
754
|
+
} & HashInfo &
|
|
755
|
+
(Opts extends undefined
|
|
756
|
+
? {
|
|
757
|
+
(msg: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
758
|
+
create(): T;
|
|
759
|
+
}
|
|
760
|
+
: {
|
|
761
|
+
(msg: TArg<Uint8Array>, opts?: TArg<Opts>): TRet<Uint8Array>;
|
|
762
|
+
create(opts?: Opts): T;
|
|
763
|
+
});
|
|
764
|
+
/** Callable extendable-output hash function type. */
|
|
765
|
+
export type CHashXOF<T extends HashXOF<T> = HashXOF<any>, Opts = undefined> = CHash<T, Opts>;
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* Creates a callable hash function from a stateful class constructor.
|
|
769
|
+
* @param hashCons - hash constructor or factory
|
|
770
|
+
* @param info - optional metadata such as DER OID
|
|
771
|
+
* @returns Frozen callable hash wrapper with `.create()`.
|
|
772
|
+
* Wrapper construction eagerly calls `hashCons(undefined)` once to read
|
|
773
|
+
* `outputLen` / `blockLen`, so constructor side effects happen at module
|
|
774
|
+
* init time.
|
|
775
|
+
* @example
|
|
776
|
+
* Wrap a stateful hash constructor into a callable helper.
|
|
777
|
+
* ```ts
|
|
778
|
+
* import { createHasher } from '@noble/hashes/utils.js';
|
|
779
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
780
|
+
* const wrapped = createHasher(sha256.create, { oid: sha256.oid });
|
|
781
|
+
* wrapped(new Uint8Array([1]));
|
|
782
|
+
* ```
|
|
783
|
+
*/
|
|
784
|
+
export function createHasher<T extends Hash<T>, Opts = undefined>(
|
|
785
|
+
hashCons: HasherCons<T, Opts>,
|
|
786
|
+
info: TArg<HashInfo> = {}
|
|
787
|
+
): TRet<CHash<T, Opts>> {
|
|
788
|
+
const hashC: any = (msg: TArg<Uint8Array>, opts?: TArg<Opts>) =>
|
|
789
|
+
hashCons(opts as Opts)
|
|
790
|
+
.update(msg)
|
|
791
|
+
.digest();
|
|
792
|
+
const tmp = hashCons(undefined);
|
|
793
|
+
hashC.outputLen = tmp.outputLen;
|
|
794
|
+
hashC.blockLen = tmp.blockLen;
|
|
795
|
+
hashC.canXOF = tmp.canXOF;
|
|
796
|
+
hashC.create = (opts?: Opts) => hashCons(opts);
|
|
797
|
+
Object.assign(hashC, info);
|
|
798
|
+
return Object.freeze(hashC) as TRet<CHash<T, Opts>>;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Cryptographically secure PRNG backed by `crypto.getRandomValues`.
|
|
803
|
+
* @param bytesLength - number of random bytes to generate
|
|
804
|
+
* @returns Random bytes.
|
|
805
|
+
* The platform `getRandomValues()` implementation still defines any
|
|
806
|
+
* single-call length cap, and this helper rejects oversize requests
|
|
807
|
+
* with a stable library `RangeError` instead of host-specific errors.
|
|
808
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
809
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
810
|
+
* @throws If the current runtime does not provide `crypto.getRandomValues`. {@link Error}
|
|
811
|
+
* @example
|
|
812
|
+
* Generate a fresh random key or nonce.
|
|
813
|
+
* ```ts
|
|
814
|
+
* const key = randomBytes(16);
|
|
815
|
+
* ```
|
|
816
|
+
*/
|
|
817
|
+
export function randomBytes(bytesLength = 32): TRet<Uint8Array> {
|
|
818
|
+
// Match the repo's other length-taking helpers instead of relying on Uint8Array coercion.
|
|
819
|
+
anumber(bytesLength, 'bytesLength');
|
|
820
|
+
const cr = typeof globalThis === 'object' ? (globalThis as any).crypto : null;
|
|
821
|
+
if (typeof cr?.getRandomValues !== 'function')
|
|
822
|
+
throw new Error('crypto.getRandomValues must be defined');
|
|
823
|
+
// Web Cryptography API Level 2 §10.1.1:
|
|
824
|
+
// if `byteLength > 65536`, throw `QuotaExceededError`.
|
|
825
|
+
// Keep the guard explicit so callers can see the quota in code
|
|
826
|
+
// instead of discovering it by reading the spec or host errors.
|
|
827
|
+
// This wrapper surfaces the same quota as a stable library RangeError.
|
|
828
|
+
if (bytesLength > 65536)
|
|
829
|
+
throw new RangeError(`"bytesLength" expected <= 65536, got ${bytesLength}`);
|
|
830
|
+
return cr.getRandomValues(new Uint8Array(bytesLength));
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
|
|
835
|
+
* @param suffix - final OID byte for the selected hash.
|
|
836
|
+
* The helper accepts any byte even though only the documented NIST hash
|
|
837
|
+
* suffixes are meaningful downstream.
|
|
838
|
+
* @returns Object containing the DER-encoded OID.
|
|
839
|
+
* @example
|
|
840
|
+
* Build OID metadata for a NIST hash.
|
|
841
|
+
* ```ts
|
|
842
|
+
* oidNist(0x01);
|
|
843
|
+
* ```
|
|
844
|
+
*/
|
|
845
|
+
export const oidNist = (suffix: number): TRet<Required<HashInfo>> => ({
|
|
846
|
+
// Current NIST hashAlgs suffixes used here fit in one DER subidentifier octet.
|
|
847
|
+
// Larger suffix values would need base-128 OID encoding and a different length byte.
|
|
848
|
+
oid: Uint8Array.from([0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, suffix]),
|
|
849
|
+
});
|