This repository was archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetached_sign.js
56 lines (46 loc) · 1.53 KB
/
detached_sign.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import base64Lib from 'base-64';
import {
cadesplugin,
ALGORITHMS,
CADESCOM_BASE64_TO_BINARY,
} from './constants';
import {
hexToBase64,
getTargetCertificate,
injectToSignatureTemplate,
extractAlgorithmOfCertificate,
} from './utils';
/**
* @function
* @name detachedSign
* @description Method calculate value of signature (Async)
* @param {string} thumbprint - hash of certificate
* @param {string} base64 - SignedInfo of signature template encoded to base64
* @return {promise} signature value and certificate value
*/
const detachedSign = async (
thumbprint,
base64,
signatureTemplateAsBase64,
) => {
const hashedData = await cadesplugin.CreateObjectAsync('CAdESCOM.HashedData');
const certificate = await getTargetCertificate(thumbprint);
const algorithm = await extractAlgorithmOfCertificate(certificate);
await hashedData.propset_Algorithm(ALGORITHMS[algorithm]);
await hashedData.propset_DataEncoding(CADESCOM_BASE64_TO_BINARY);
await hashedData.Hash(base64);
const calculatedHashedData = await hashedData;
const x509certificate = await certificate.Export(0);
const rawSignature = await cadesplugin.CreateObjectAsync('CAdESCOM.RawSignature');
const signatureHex = await rawSignature.SignHash(
calculatedHashedData,
certificate,
);
const transformedSignatureTemlate = injectToSignatureTemplate(
base64Lib.decode(signatureTemplateAsBase64),
hexToBase64(signatureHex),
x509certificate,
);
return base64Lib.encode(transformedSignatureTemlate);
};
export default detachedSign;