UNPKG

cose-kit

Version:

This is an early prototype of a RFC8152 COSE library for node.js.

72 lines (71 loc) 2.11 kB
import { fromUTF8 } from "./lib/buffer_utils.js"; import { encoder } from "./cbor.js"; export const algs = new Map([ [-8, { name: 'EdDSA' }], [-7, { name: 'ES256', hash: 'SHA-256' }], [-35, { name: 'ES384', hash: 'SHA-384' }], [-36, { name: 'ES512', hash: 'SHA-512' }], [-37, { name: 'PS256', hash: 'SHA-256' }], [-38, { name: 'PS384', hash: 'SHA-384' }], [-39, { name: 'PS512', hash: 'SHA-512' }], [-257, { name: 'RS256', hash: 'SHA-256' }], [-258, { name: 'RS384', hash: 'SHA-384' }], [-259, { name: 'RS512', hash: 'SHA-512' }], ]); export const macAlgs = new Map([ [5, { name: 'HS256', hash: 'SHA-256', length: 256 }], [6, { name: 'HS384', hash: 'SHA-384', length: 384 }], [7, { name: 'HS512', hash: 'SHA-512', length: 512 }] ]); export const macAlgsToValue = new Map(Array.from(macAlgs.entries()).map(([k, v]) => [v.name, k])); export const algsToValue = new Map([ ['EdDSA', -8], ['ES256', -7], ['ES384', -35], ['ES512', -36], ['PS256', -37], ['PS384', -38], ['PS512', -39], ['RS256', -257], ['RS384', -258], ['RS512', -259], ]); export const headers = { partyUNonce: -22, static_key_id: -3, static_key: -2, ephemeral_key: -1, alg: 1, crit: 2, ctyp: 3, kid: 4, IV: 5, Partial_IV: 6, counter_signature: 7, x5bag: 32, x5chain: 33, x5t: 34, x5u: 35, }; export const encodeProtectedHeaders = (protectedHeaders) => { if (typeof protectedHeaders === 'undefined') { return new Uint8Array(); } return encoder.encode(new Map(Object.entries(protectedHeaders || {}).map(([k, v]) => { if (k === 'alg') { v = algsToValue.get(v); } else if (typeof v === 'string') { v = fromUTF8(v); } return [headers[k], v]; }))); }; export const mapUnprotectedHeaders = (unprotectedHeaders) => { return new Map(Object.entries(unprotectedHeaders || {}).map(([k, v]) => { if (typeof v === 'string') { v = fromUTF8(v); } return [headers[k], v]; })); };