@tstdl/base 0.90.27 → 0.90.28
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/package.json +1 -1
- package/utils/cryptography.d.ts +4 -3
- package/utils/cryptography.js +12 -11
package/package.json
CHANGED
package/utils/cryptography.d.ts
CHANGED
|
@@ -115,9 +115,10 @@ export declare function generatePbkdf2Key(extractable?: boolean): Promise<Crypto
|
|
|
115
115
|
export declare function deriveBytes(algorithm: DeriveAlgorithm, baseKey: CryptoKey, length: number): Promise<Uint8Array>;
|
|
116
116
|
/**
|
|
117
117
|
* derive multiply byte arrays from key
|
|
118
|
-
* @param count how many Uint8Arrays to dervice
|
|
119
|
-
* @param length length of each Uint8Array in bytes
|
|
120
118
|
* @param algorithm algorithm to derive with
|
|
121
119
|
* @param baseKey key to derive from
|
|
120
|
+
* @param length length of each Uint8Array in bytes, if single number is provided, it is used for every array
|
|
121
|
+
* @param count how many Uint8Arrays to derive
|
|
122
122
|
*/
|
|
123
|
-
export declare function deriveBytesMultiple<
|
|
123
|
+
export declare function deriveBytesMultiple<const Lengths extends readonly number[]>(algorithm: DeriveAlgorithm, baseKey: CryptoKey, lengths: Lengths): Promise<ReadonlyTuple<Uint8Array, Lengths['length']>>;
|
|
124
|
+
export declare function deriveBytesMultiple<const C extends number>(algorithm: DeriveAlgorithm, baseKey: CryptoKey, length: C, count: number): Promise<ReadonlyTuple<Uint8Array, C>>;
|
package/utils/cryptography.js
CHANGED
|
@@ -2,7 +2,7 @@ import { createArray } from './array/array.js';
|
|
|
2
2
|
import { encodeBase64, encodeBase64Url } from './base64.js';
|
|
3
3
|
import { decodeText, encodeHex, encodeUtf8 } from './encoding.js';
|
|
4
4
|
import { getRandomBytes } from './random.js';
|
|
5
|
-
import { isDefined, isString } from './type-guards.js';
|
|
5
|
+
import { isArray, isDefined, isString } from './type-guards.js';
|
|
6
6
|
import { zBase32Encode } from './z-base32.js';
|
|
7
7
|
const subtle = globalThis.crypto.subtle;
|
|
8
8
|
/**
|
|
@@ -162,17 +162,18 @@ export async function deriveBytes(algorithm, baseKey, length) {
|
|
|
162
162
|
const bytes = await globalThis.crypto.subtle.deriveBits(algorithm, baseKey, length * 8);
|
|
163
163
|
return new Uint8Array(bytes);
|
|
164
164
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
* @param length length of each Uint8Array in bytes
|
|
169
|
-
* @param algorithm algorithm to derive with
|
|
170
|
-
* @param baseKey key to derive from
|
|
171
|
-
*/
|
|
172
|
-
export async function deriveBytesMultiple(algorithm, baseKey, count, length) {
|
|
173
|
-
const totalBits = count * length * 8;
|
|
165
|
+
export async function deriveBytesMultiple(algorithm, baseKey, lengthOrLengths, countOrNothing) {
|
|
166
|
+
const lengths = isArray(lengthOrLengths) ? lengthOrLengths : createArray(countOrNothing, () => lengthOrLengths);
|
|
167
|
+
const totalBits = lengths.reduce((sum, length) => sum + length, 0) * 8;
|
|
174
168
|
const bytes = await globalThis.crypto.subtle.deriveBits(algorithm, baseKey, totalBits);
|
|
175
|
-
|
|
169
|
+
const arrays = [];
|
|
170
|
+
for (let i = 0; i < bytes.byteLength;) {
|
|
171
|
+
const slice = bytes.slice(i, i + lengths[arrays.length]);
|
|
172
|
+
const array = new Uint8Array(slice);
|
|
173
|
+
arrays.push(array);
|
|
174
|
+
i += slice.byteLength;
|
|
175
|
+
}
|
|
176
|
+
return arrays;
|
|
176
177
|
}
|
|
177
178
|
function isBinaryKey(key) {
|
|
178
179
|
return isDefined(key.byteLength);
|