@cripty2001/utils 0.0.239 → 0.0.241
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/index.d.ts +3 -3
- package/dist/index.js +15 -10
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -135,7 +135,7 @@ export declare function fn2promise<T>(fn: () => T | Promise<T>): Promise<T>;
|
|
|
135
135
|
export type SafeApplyed<T> = (updater: (draft: T) => T | void) => T;
|
|
136
136
|
export declare function safeApply<T>(updater: (draft: T) => T | void, incoming: T): T;
|
|
137
137
|
export declare function timesafeEqual(a: string, b: string): boolean;
|
|
138
|
-
export declare function
|
|
139
|
-
export declare function checkDigest(data: string
|
|
140
|
-
export declare function enforceDigest(data: string
|
|
138
|
+
export declare function buildDigest(data: string | Uint8Array<ArrayBuffer>): Promise<string>;
|
|
139
|
+
export declare function checkDigest(data: string | Uint8Array<ArrayBuffer>, digest: string): Promise<boolean>;
|
|
140
|
+
export declare function enforceDigest(data: string | Uint8Array<ArrayBuffer>, digest: string): Promise<void>;
|
|
141
141
|
export declare function isDigest(digest: string): boolean;
|
package/dist/index.js
CHANGED
|
@@ -24,7 +24,7 @@ exports.timediff2HumanReadable = timediff2HumanReadable;
|
|
|
24
24
|
exports.fn2promise = fn2promise;
|
|
25
25
|
exports.safeApply = safeApply;
|
|
26
26
|
exports.timesafeEqual = timesafeEqual;
|
|
27
|
-
exports.
|
|
27
|
+
exports.buildDigest = buildDigest;
|
|
28
28
|
exports.checkDigest = checkDigest;
|
|
29
29
|
exports.enforceDigest = enforceDigest;
|
|
30
30
|
exports.isDigest = isDigest;
|
|
@@ -379,20 +379,25 @@ function timesafeEqual(a, b) {
|
|
|
379
379
|
return (0, crypto_1.timingSafeEqual)(Buffer.from(a), Buffer.from(b));
|
|
380
380
|
}
|
|
381
381
|
const DIGEST_PREFIX = 'sha256.base64url.';
|
|
382
|
-
function
|
|
383
|
-
const
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
382
|
+
async function buildDigest(data) {
|
|
383
|
+
const encoded = typeof data === "string" ?
|
|
384
|
+
new TextEncoder().encode(data) :
|
|
385
|
+
data;
|
|
386
|
+
const hash = await globalThis.crypto.subtle.digest("SHA-256", encoded);
|
|
387
|
+
const b64url = new Uint8Array(hash).toBase64({
|
|
388
|
+
alphabet: "base64url",
|
|
389
|
+
omitPadding: true,
|
|
390
|
+
});
|
|
391
|
+
return `${DIGEST_PREFIX}${b64url}`;
|
|
387
392
|
}
|
|
388
|
-
function checkDigest(data, digest) {
|
|
393
|
+
async function checkDigest(data, digest) {
|
|
389
394
|
if (!digest.startsWith(DIGEST_PREFIX))
|
|
390
395
|
throw new Error(`The digest was not generated with digest() function.`);
|
|
391
|
-
const dataDigest =
|
|
396
|
+
const dataDigest = await buildDigest(data);
|
|
392
397
|
return timesafeEqual(dataDigest, digest);
|
|
393
398
|
}
|
|
394
|
-
function enforceDigest(data, digest) {
|
|
395
|
-
if (!checkDigest(data, digest))
|
|
399
|
+
async function enforceDigest(data, digest) {
|
|
400
|
+
if (!(await checkDigest(data, digest)))
|
|
396
401
|
throw new Error(`The digest does not match the data.`);
|
|
397
402
|
}
|
|
398
403
|
function isDigest(digest) {
|
package/package.json
CHANGED