Skip to content

Instantly share code, notes, and snippets.

@Slavenin
Last active January 13, 2020 13:33
Show Gist options
  • Save Slavenin/994c10b043b68f996d6deedfb562414b to your computer and use it in GitHub Desktop.
Save Slavenin/994c10b043b68f996d6deedfb562414b to your computer and use it in GitHub Desktop.
function SignCadesBES_Async_File(certListBoxId, dataToSign, onSign, onError) {
cadesplugin.async_spawn(function* (arg) {
var e = document.getElementById(arg[ 0 ]);
var selectedCertID = e.selectedIndex;
if (selectedCertID == -1) {
alert("Select certificate");
return;
}
var certificate = global_selectbox_container[ selectedCertID ];
var Signature;
try {
//FillCertInfo_Async(certificate);
var errormes = "";
try {
var oSigner = yield cadesplugin.CreateObjectAsync("CAdESCOM.CPSigner");
} catch (err) {
errormes = "Failed to create CAdESCOM.CPSigner: " + err.number;
throw errormes;
}
if (oSigner) {
yield oSigner.propset_Certificate(certificate);
}
else {
errormes = "Failed to create CAdESCOM.CPSigner";
throw errormes;
}
var oSignedData = yield cadesplugin.CreateObjectAsync("CAdESCOM.CadesSignedData");
var CADES_BES = 1;
if (dataToSign) {
// Данные на подпись ввели
yield oSignedData.propset_ContentEncoding(cadesplugin.CADESCOM_BASE64_TO_BINARY); //CADESCOM_BASE64_TO_BINARY
yield oSignedData.propset_Content(dataToSign);
yield oSigner.propset_Options(1); //CAPICOM_CERTIFICATE_INCLUDE_WHOLE_CHAIN
try {
Signature = yield oSignedData.SignCades(oSigner, CADES_BES, true);
}
catch (err) {
errormes = "Не удалось создать подпись из-за ошибки: " + cadesplugin.getLastError(err);
throw errormes;
}
}
var pubKeyPr = yield certificate.PublicKey();
var key = yield pubKeyPr.EncodedKey;
var keyV = yield key.Value();
onSign(Signature, keyV);
}
catch (err) {
onError(err)
}
}, certListBoxId); //cadesplugin.async_spawn
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment