File size: 2,171 Bytes
edaa6b9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_utils_crypto = require("./crypto.js");
//#region src/utils/buffer.ts
/**
* @module
* Buffer utility.
*/
const equal = (a, b) => {
if (a === b) return true;
if (a.byteLength !== b.byteLength) return false;
const va = new DataView(a);
const vb = new DataView(b);
let i = va.byteLength;
while (i--) if (va.getUint8(i) !== vb.getUint8(i)) return false;
return true;
};
const constantTimeEqualString = (a, b) => {
const aLen = a.length;
const bLen = b.length;
const maxLen = Math.max(aLen, bLen);
let out = aLen ^ bLen;
for (let i = 0; i < maxLen; i++) {
const aChar = i < aLen ? a.charCodeAt(i) : 0;
const bChar = i < bLen ? b.charCodeAt(i) : 0;
out |= aChar ^ bChar;
}
return out === 0;
};
const timingSafeEqualString = async (a, b, hashFunction) => {
if (!hashFunction) hashFunction = require_utils_crypto.sha256;
const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)]);
if (sa == null || sb == null || typeof sa !== "string" || typeof sb !== "string") return false;
const hashEqual = constantTimeEqualString(sa, sb);
const originalEqual = constantTimeEqualString(a, b);
return hashEqual && originalEqual;
};
const timingSafeEqual = async (a, b, hashFunction) => {
if (typeof a === "string" && typeof b === "string") return timingSafeEqualString(a, b, hashFunction);
if (!hashFunction) hashFunction = require_utils_crypto.sha256;
const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)]);
if (!sa || !sb || typeof sa !== "string" || typeof sb !== "string") return false;
return timingSafeEqualString(sa, sb);
};
const bufferToString = (buffer) => {
if (buffer instanceof ArrayBuffer) return new TextDecoder("utf-8").decode(buffer);
return buffer;
};
const bufferToFormData = (arrayBuffer, contentType) => {
return new Response(arrayBuffer, { headers: { "Content-Type": contentType.replace(/^[^;]+/, (mediaType) => mediaType.toLowerCase()) } }).formData();
};
//#endregion
exports.bufferToFormData = bufferToFormData;
exports.bufferToString = bufferToString;
exports.equal = equal;
exports.timingSafeEqual = timingSafeEqual;
|