From 00e128423d7d98ec9634de8f38aafd0064c707b2 Mon Sep 17 00:00:00 2001 From: vgoma Date: Sat, 7 Nov 2020 21:56:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20=D1=81=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=B4=D0=BF=D0=B8=D1=81=D1=8C=D1=8E=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/script-tag/public/create-sign.js | 109 +++++++++++++++------- examples/script-tag/public/index.html | 8 ++ 2 files changed, 84 insertions(+), 33 deletions(-) diff --git a/examples/script-tag/public/create-sign.js b/examples/script-tag/public/create-sign.js index b8faedf..c5f9b2f 100644 --- a/examples/script-tag/public/create-sign.js +++ b/examples/script-tag/public/create-sign.js @@ -7,48 +7,91 @@ var $createSignature = document.forms.createSignature, $certificate = document.getElementById('certificate'), $message = document.getElementById('message'), + $messageFile = document.getElementById('messageFile'), + $messageFileError = document.getElementById('messageFileError'), $hash = document.getElementById('hash'), $hashError = document.getElementById('hashError'), $signature = document.getElementById('signature'), - $signatureError = document.getElementById('signatureError'); + $signatureError = document.getElementById('signatureError'), + + // https://support.cryptopro.ru/index.php?/Knowledgebase/Article/View/213/12/ogrnichenie-n-rzmer-podpisyvemogo-fjjl-v-bruzere + MAX_FILE_SIZE = 25000000; + + function readFile(messageFile) { + return new Promise(function (resolve, reject) { + var fileReader = new FileReader(); + + fileReader.onload = function () { + resolve(this.result); + }; + + if (messageFile.size > MAX_FILE_SIZE) { + reject('Файл для подписи не должен превышать ' + MAX_FILE_SIZE / 1000000 + 'МБ'); + + return; + } + + fileReader.readAsArrayBuffer(messageFile); + }); + } + + function createSignature(message, hash) { + var thumbprint = $certificate.value, + detachedSignature = document.querySelector('input[name="signatureType"]:checked').value, + signaturePromise; + + detachedSignature = Boolean(Number(detachedSignature)); + + $hash.value = hash; + + $signature.placeholder = 'Создается...'; + $signature.value = ''; + + if (detachedSignature) { + signaturePromise = window.cryptoPro.createDetachedSignature(thumbprint, hash); + } else { + signaturePromise = window.cryptoPro.createAttachedSignature(thumbprint, message); + } + + signaturePromise.then(function (signature) { + $signature.value = signature; + $signatureError.textContent = ''; + }, function (error) { + $signature.placeholder = 'Не создана'; + $signatureError.textContent = error.message; + }); + } + + $message.addEventListener('keydown', function() { + $messageFile.value = null; + }); + + if ($messageFile) { + $messageFile.addEventListener('change', function() { + $message.value = ''; + }); + } $createSignature.addEventListener('submit', function (event) { - var thumbprint = $certificate.value, - message = $message.value; + var messageFile = $messageFile && $messageFile.files.length && $messageFile.files[0], + messagePromise = Promise.resolve($message.value); + + if (messageFile) { + messagePromise = readFile(messageFile); + } event.preventDefault(); - $hash.placeholder = 'Вычисляется...'; - $hash.value = ''; + messagePromise.then(function (message) { + $hash.placeholder = 'Вычисляется...'; + $hash.value = ''; - window.cryptoPro.createHash(message).then(function (hash) { - var detachedSignature = document.querySelector('input[name="signatureType"]:checked').value, - signaturePromise; - - detachedSignature = Boolean(Number(detachedSignature)); - - $hash.value = hash; - - $signature.placeholder = 'Создается...'; - $signature.value = ''; - - if (detachedSignature) { - signaturePromise = window.cryptoPro.createDetachedSignature(thumbprint, hash); - } else { - signaturePromise = window.cryptoPro.createAttachedSignature(thumbprint, message); - } - - signaturePromise.then(function (signature) { - $signature.value = signature; - }, function (error) { - $signature.placeholder = 'Не создана'; - - $signatureError.textContent = error.message; + window.cryptoPro.createHash(message).then(createSignature.bind(null, message), function (hashError) { + $hash.placeholder = 'Не вычислен'; + $hashError.textContent = hashError.message; }); - }, function (error) { - $hash.placeholder = 'Не вычислен'; - - $hashError.textContent = error.message; - }); + }, function (fileError) { + $messageFileError.textContent = fileError; + }) }); })(); diff --git a/examples/script-tag/public/index.html b/examples/script-tag/public/index.html index 9066f6e..3b60fdc 100755 --- a/examples/script-tag/public/index.html +++ b/examples/script-tag/public/index.html @@ -13,6 +13,14 @@

+ + +
+ +

+            
+            
+