forked from vgoma/crypto-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createXMLSignature.ts
65 lines (54 loc) · 2.75 KB
/
createXMLSignature.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
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
57
58
59
60
61
62
63
64
65
import { _afterPluginsLoaded } from '../helpers/_afterPluginsLoaded';
import { _extractMeaningfulErrorMessage } from '../helpers/_extractMeaningfulErrorMessage';
import { __cadesAsyncToken__, __createCadesPluginObject__, _generateCadesFn } from '../helpers/_generateCadesFn';
import { _getCadesCert } from '../helpers/_getCadesCert';
/**
* Создает XML подпись для документа в формате XML
*
* @param thumbprint - отпечаток сертификата
* @param unencryptedMessage - подписываемое сообщение в формате XML
* @returns подпись
*/
export const createXMLSignature = _afterPluginsLoaded(
async (thumbprint: string, unencryptedMessage: string): Promise<string> => {
const { cadesplugin } = window;
const cadesCertificate = await _getCadesCert(thumbprint);
return eval(
_generateCadesFn(function createXMLSignature(): string {
let cadesSigner;
let cadesSignedXML;
try {
cadesSigner = __cadesAsyncToken__ + __createCadesPluginObject__('CAdESCOM.CPSigner');
cadesSignedXML = __cadesAsyncToken__ + __createCadesPluginObject__('CAdESCOM.SignedXML');
} catch (error) {
console.error(error);
throw new Error(_extractMeaningfulErrorMessage(error) || 'Ошибка при инициализации подписи');
}
try {
const signatureMethod = 'urn:ietf:params:xml:ns:cpxmlsec:algorithms:gostr34102012-gostr34112012-256';
const digestMethod = 'urn:ietf:params:xml:ns:cpxmlsec:algorithms:gostr34112012-256';
void (__cadesAsyncToken__ + cadesSigner.propset_Certificate(cadesCertificate));
void (__cadesAsyncToken__ + cadesSigner.propset_CheckCertificate(true));
void (__cadesAsyncToken__ + cadesSignedXML.propset_Content(unencryptedMessage));
void (
__cadesAsyncToken__ +
cadesSignedXML.propset_SignatureType(cadesplugin.CADESCOM_XML_SIGNATURE_TYPE_ENVELOPED)
);
void (__cadesAsyncToken__ + cadesSignedXML.propset_SignatureMethod(signatureMethod));
void (__cadesAsyncToken__ + cadesSignedXML.propset_DigestMethod(digestMethod));
} catch (error) {
console.error(error);
throw new Error(_extractMeaningfulErrorMessage(error) || 'Ошибка при указании данных для подписи');
}
let signature: string;
try {
signature = __cadesAsyncToken__ + cadesSignedXML.Sign(cadesSigner);
} catch (error) {
console.error(error);
throw new Error(_extractMeaningfulErrorMessage(error) || 'Ошибка при подписании данных');
}
return signature;
}),
);
},
);