cose-kit
Version:
**DEPRECATED:** Use [@auth0/cose](https://www.npmjs.com/package/@auth0/cose).
52 lines (51 loc) • 1.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.uint64be = exports.toUTF8 = exports.fromUTF8 = exports.areEqual = exports.concat = exports.decoder = exports.encoder = void 0;
const MAX_INT32 = 2 ** 32;
exports.encoder = new TextEncoder();
exports.decoder = new TextDecoder();
function concat(...buffers) {
const size = buffers.reduce((acc, { length }) => acc + length, 0);
const buf = new Uint8Array(size);
let i = 0;
buffers.forEach((buffer) => {
buf.set(buffer, i);
i += buffer.length;
});
return buf;
}
exports.concat = concat;
function areEqual(buf1, buf2) {
if (buf1 === buf2) {
return true;
}
if (buf1.byteLength !== buf2.byteLength) {
return false;
}
for (let i = 0; i < buf1.byteLength; i++) {
if (buf1[i] !== buf2[i]) {
return false;
}
}
return true;
}
exports.areEqual = areEqual;
const fromUTF8 = (input) => exports.encoder.encode(input);
exports.fromUTF8 = fromUTF8;
const toUTF8 = (input) => exports.decoder.decode(input);
exports.toUTF8 = toUTF8;
function writeUInt32BE(buf, value, offset) {
if (value < 0 || value >= MAX_INT32) {
throw new RangeError(`value must be >= 0 and <= ${MAX_INT32 - 1}. Received ${value}`);
}
buf.set([value >>> 24, value >>> 16, value >>> 8, value & 0xff], offset);
}
function uint64be(value) {
const high = Math.floor(value / MAX_INT32);
const low = value % MAX_INT32;
const buf = new Uint8Array(8);
writeUInt32BE(buf, high, 0);
writeUInt32BE(buf, low, 4);
return buf;
}
exports.uint64be = uint64be;