UNPKG

cose-kit

Version:

**DEPRECATED:** Use [@auth0/cose](https://www.npmjs.com/package/@auth0/cose).

66 lines (65 loc) 2.74 kB
import { AlgorithmNames, Headers, ProtectedHeaders, UnprotectedHeaders } from '../headers.js'; import sign from '#runtime/sign.js'; import { SignatureBase } from './SignatureBase.js'; import { encoder, addExtension } from '../cbor.js'; import { decode } from "./decode.js"; export class Sign1 extends SignatureBase { constructor(protectedHeaders, unprotectedHeaders, payload, signature) { super(protectedHeaders, unprotectedHeaders, signature); this.payload = payload; } getContentForEncoding() { return [ this.encodedProtectedHeaders, this.unprotectedHeaders, this.payload, this.signature, ]; } static Signature1(protectedHeaders, applicationHeaders, payload) { return encoder.encode([ 'Signature1', protectedHeaders, applicationHeaders, payload, ]); } async verify(key, options) { var _a, _b; const toBeSigned = Sign1.Signature1(this.encodedProtectedHeaders || new Uint8Array(), (_a = options === null || options === void 0 ? void 0 : options.externalAAD) !== null && _a !== void 0 ? _a : new Uint8Array(), (_b = options === null || options === void 0 ? void 0 : options.detachedPayload) !== null && _b !== void 0 ? _b : this.payload); await this.internalVerify(toBeSigned, key, options); } async verifyX509(roots) { const { publicKey } = await this.verifyX509Chain(roots); return this.verify(publicKey); } static async sign(protectedHeaders, unprotectedHeaders, payload, key) { const wProtectedHeaders = ProtectedHeaders.wrap(protectedHeaders); if (!wProtectedHeaders.has(Headers.Algorithm)) { throw new Error('The alg header must be set.'); } const alg = AlgorithmNames.get(wProtectedHeaders.get(Headers.Algorithm)); const encodedProtectedHeaders = encoder.encode(wProtectedHeaders.esMap); const unprotectedHeadersMap = UnprotectedHeaders.wrap(unprotectedHeaders).esMap; const toBeSigned = Sign1.Signature1(encodedProtectedHeaders, new Uint8Array(), payload); if (!alg) { throw new Error('The alg header must be set.'); } const signature = await sign(alg, key, toBeSigned); return new Sign1(encodedProtectedHeaders, unprotectedHeadersMap, payload, signature); } static decode(cose) { return decode(cose, Sign1); } } Sign1.tag = 18; addExtension({ Class: Sign1, tag: Sign1.tag, encode(instance, encodeFn) { return encodeFn(instance.getContentForEncoding()); }, decode: (data) => { return new Sign1(data[0], data[1], data[2], data[3]); } });