-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
ec.ts
30 lines (28 loc) · 1010 Bytes
/
ec.ts
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
/**
* ec.js
*/
import * as params from './params';
import ec from 'js-crypto-ec';
import * as BufferMod from 'buffer';
import {SignatureType} from './typedef';
const BufferR = BufferMod.Buffer;
/**
* Sign encoded tbsCertificate with given signature algorithm under the JWK private key.
* @param {Uint8Array} encodedTbsCertificate - TBS Certificate encoded to Uint8Array.
* @param {JsonWebKey} privateJwk - Private key object in JWK format.
* @param {String} signatureAlgorithm - Signature algorithm name like 'ecdsa-with-sha256'.
* @returns {Promise<{data: BufferR, unused: Number}>} - ASN.1 encoded signature.
*/
export const getAsn1Signature = async (
encodedTbsCertificate: Uint8Array,
privateJwk: JsonWebKey,
signatureAlgorithm: SignatureType
): Promise<{ unused: 0, data: Buffer }> => {
const asn1sig = await ec.sign(
encodedTbsCertificate,
privateJwk,
params.signatureAlgorithms[signatureAlgorithm].hash,
'der'
);
return {unused: 0, data: BufferR.from(asn1sig)};
};