@cripty2001/utils 0.0.238 → 0.0.240
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 +4 -3
- package/dist/index.js +9 -4
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -135,6 +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, digest: string): boolean;
|
|
140
|
-
export declare function enforceDigest(data: string, digest: string): void;
|
|
138
|
+
export declare function buildDigest(data: string | Buffer): string;
|
|
139
|
+
export declare function checkDigest(data: string | Buffer, digest: string): boolean;
|
|
140
|
+
export declare function enforceDigest(data: string | Buffer, digest: string): void;
|
|
141
|
+
export declare function isDigest(digest: string): boolean;
|
package/dist/index.js
CHANGED
|
@@ -24,9 +24,10 @@ 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
|
+
exports.isDigest = isDigest;
|
|
30
31
|
const whispr_1 = require("@cripty2001/whispr");
|
|
31
32
|
const value_1 = require("@sinclair/typebox/value");
|
|
32
33
|
const crypto_1 = require("crypto");
|
|
@@ -378,19 +379,23 @@ function timesafeEqual(a, b) {
|
|
|
378
379
|
return (0, crypto_1.timingSafeEqual)(Buffer.from(a), Buffer.from(b));
|
|
379
380
|
}
|
|
380
381
|
const DIGEST_PREFIX = 'sha256.base64url.';
|
|
381
|
-
function
|
|
382
|
+
function buildDigest(data) {
|
|
383
|
+
const encoded = typeof data === 'string' ? new TextEncoder().encode(data) : data;
|
|
382
384
|
const hash = (0, crypto_1.createHash)('sha256')
|
|
383
|
-
.update(
|
|
385
|
+
.update(encoded)
|
|
384
386
|
.digest('base64url');
|
|
385
387
|
return `${DIGEST_PREFIX}${hash}`;
|
|
386
388
|
}
|
|
387
389
|
function checkDigest(data, digest) {
|
|
388
390
|
if (!digest.startsWith(DIGEST_PREFIX))
|
|
389
391
|
throw new Error(`The digest was not generated with digest() function.`);
|
|
390
|
-
const dataDigest =
|
|
392
|
+
const dataDigest = buildDigest(data);
|
|
391
393
|
return timesafeEqual(dataDigest, digest);
|
|
392
394
|
}
|
|
393
395
|
function enforceDigest(data, digest) {
|
|
394
396
|
if (!checkDigest(data, digest))
|
|
395
397
|
throw new Error(`The digest does not match the data.`);
|
|
396
398
|
}
|
|
399
|
+
function isDigest(digest) {
|
|
400
|
+
return digest.startsWith(DIGEST_PREFIX) && digest.length === DIGEST_PREFIX.length + 43;
|
|
401
|
+
}
|
package/package.json
CHANGED