@ldclabs/cose-ts
Version:
Implemented Keys, Algorithms (RFC9053), COSE (RFC9052) and CWT (RFC8392) in TypeScript.
47 lines • 1.57 kB
JavaScript
// (c) 2023-present, LDC Labs. All rights reserved.
// See the file LICENSE for licensing terms.
import { decode, encode } from 'cborg';
export { bytesToHex, hexToBytes, utf8ToBytes, randomBytes, toBytes, concatBytes } from '@noble/hashes/utils';
export function decodeCBOR(data) {
return decode(data, {
useMaps: true,
rejectDuplicateMapKeys: true
});
}
export function encodeCBOR(data) {
return encode(data, {});
}
export function bytesToBase64Url(bytes) {
return btoa(String.fromCodePoint(...bytes))
.replaceAll('+', '-')
.replaceAll('/', '_')
.replaceAll('=', '');
}
export function base64ToBytes(str) {
return Uint8Array.from(atob(str.replaceAll('-', '+').replaceAll('_', '/')), (m) => m.codePointAt(0));
}
// compareBytes compares two Uint8Array and returns: -1 if a < b, 0 if a == b, 1 if a > b
export function compareBytes(a, b) {
if (a instanceof Uint8Array && b instanceof Uint8Array) {
if (a === b) {
return 0;
}
for (let i = 0; i < a.length; i++) {
if (a[i] === b[i]) {
continue;
}
return a[i] < b[i] ? -1 : 1;
}
if (b.length > a.length) {
return -1;
}
return 0;
}
throw new Error('cose-ts: compareBytes: invalid arguments');
}
export function assertEqual(actual, expected, message = 'not equal') {
if (actual !== expected) {
throw new Error(`cose-ts: ${message}, expected ${expected}, got ${actual}`);
}
}
//# sourceMappingURL=utils.js.map