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 @@

+ + +
+ +

+            
+            
+