@cripty2001/utils 0.0.237 → 0.0.238
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 -0
- package/dist/index.js +20 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -135,3 +135,6 @@ 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 builddigest(data: string): string;
|
|
139
|
+
export declare function checkDigest(data: string, digest: string): boolean;
|
|
140
|
+
export declare function enforceDigest(data: string, digest: string): void;
|
package/dist/index.js
CHANGED
|
@@ -24,6 +24,9 @@ exports.timediff2HumanReadable = timediff2HumanReadable;
|
|
|
24
24
|
exports.fn2promise = fn2promise;
|
|
25
25
|
exports.safeApply = safeApply;
|
|
26
26
|
exports.timesafeEqual = timesafeEqual;
|
|
27
|
+
exports.builddigest = builddigest;
|
|
28
|
+
exports.checkDigest = checkDigest;
|
|
29
|
+
exports.enforceDigest = enforceDigest;
|
|
27
30
|
const whispr_1 = require("@cripty2001/whispr");
|
|
28
31
|
const value_1 = require("@sinclair/typebox/value");
|
|
29
32
|
const crypto_1 = require("crypto");
|
|
@@ -374,3 +377,20 @@ function timesafeEqual(a, b) {
|
|
|
374
377
|
return false;
|
|
375
378
|
return (0, crypto_1.timingSafeEqual)(Buffer.from(a), Buffer.from(b));
|
|
376
379
|
}
|
|
380
|
+
const DIGEST_PREFIX = 'sha256.base64url.';
|
|
381
|
+
function builddigest(data) {
|
|
382
|
+
const hash = (0, crypto_1.createHash)('sha256')
|
|
383
|
+
.update(new TextEncoder().encode(data))
|
|
384
|
+
.digest('base64url');
|
|
385
|
+
return `${DIGEST_PREFIX}${hash}`;
|
|
386
|
+
}
|
|
387
|
+
function checkDigest(data, digest) {
|
|
388
|
+
if (!digest.startsWith(DIGEST_PREFIX))
|
|
389
|
+
throw new Error(`The digest was not generated with digest() function.`);
|
|
390
|
+
const dataDigest = builddigest(data);
|
|
391
|
+
return timesafeEqual(dataDigest, digest);
|
|
392
|
+
}
|
|
393
|
+
function enforceDigest(data, digest) {
|
|
394
|
+
if (!checkDigest(data, digest))
|
|
395
|
+
throw new Error(`The digest does not match the data.`);
|
|
396
|
+
}
|
package/package.json
CHANGED