Skip to content

Instantly share code, notes, and snippets.

@Rukomoynikov
Last active July 13, 2018 11:59
Show Gist options
  • Save Rukomoynikov/ff58a5e3f5360e2df89bf277edad0470 to your computer and use it in GitHub Desktop.
Save Rukomoynikov/ff58a5e3f5360e2df89bf277edad0470 to your computer and use it in GitHub Desktop.
sign uc
#### Подписание данных (исходные данные, XML содержится в параметре response.parameters.notSignedRequest):
response.parameters.notSignedRequest = utf8_decode(response.parameters.notSignedRequest)
dataToSign = JSON.stringify(response.parameters.notSignedRequest).replace(/^"/, '').replace(/"$/, '')
signedOnce = window.SignCreate({ dataToSign: dataToSign, contentEncoding: 0x00 })
signedOnce = signedOnce.replace(/[\s]/gi, '')
console.log('Первая подпись корректна') if Verify(signedOnce, dataToSign, 0x00)
dataToSign = signedOnce
signedTwice = window.SignCreate({ dataToSign: dataToSign, contentEncoding: 0x01 })
signedTwice = signedTwice.replace(/[\s]/gi, '')
console.log('Вторая подпись корректна') if Verify(signedTwice, dataToSign, 0x01)
#### Функция отвечающая за подписание данных:
CADESCOM_CADES_BES = 1
CAPICOM_CURRENT_USER_STORE = 2
CAPICOM_MY_STORE = "My"
CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED = 2
CAPICOM_CERTIFICATE_FIND_TIME_VALID = 9
window.SignCreate = (options) ->
{
certSubjectName = 'CN=Оператор Тестовый Сертификат, C=RU',
dataToSign,
detached = true,
contentEncoding = 0x00
} = options
oStore = cadesplugin.CreateObject("CAdESCOM.Store")
oStore.Open(CAPICOM_CURRENT_USER_STORE, CAPICOM_MY_STORE, CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED)
oCertificates = oStore.Certificates.Find(CAPICOM_CERTIFICATE_FIND_TIME_VALID)
if oCertificates.Count == 0
alert("Certificate not found: " + certSubjectName)
return
oCertificate = oCertificates.Item(1)
oSigner = cadesplugin.CreateObject("CAdESCOM.CPSigner")
oSigner.Certificate = oCertificate
oSignedData = cadesplugin.CreateObject("CAdESCOM.CadesSignedData")
oSignedData.ContentEncoding = contentEncoding
oSignedData.Content = Base64.encode(dataToSign)
sSignedMessage = ""
try
sSignedMessage = oSignedData.SignCades(oSigner, CADESCOM_CADES_BES)
catch err
alert("Failed to create signature. Error: " + cadesplugin.getLastError(err))
return
oStore.Close()
return sSignedMessage
window.Verify = (sSignedMessage, dataToVerify, contentEncoding = 0x00) ->
oSignedData = cadesplugin.CreateObject("CAdESCOM.CadesSignedData")
try
oSignedData.ContentEncoding = contentEncoding
oSignedData.Content = dataToVerify
oSignedData.VerifyCades(sSignedMessage, CADESCOM_CADES_BES)
catch err
alert("Failed to verify signature. Error: " + cadesplugin.getLastError(err))
return false
return true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment