mirror of
https://github.com/crypto-pro-web/crypto-pro-js.git
synced 2024-11-23 16:44:59 +03:00
Добавил пример с подписью файла
This commit is contained in:
parent
26bf41091e
commit
00e128423d
@ -7,48 +7,91 @@
|
|||||||
var $createSignature = document.forms.createSignature,
|
var $createSignature = document.forms.createSignature,
|
||||||
$certificate = document.getElementById('certificate'),
|
$certificate = document.getElementById('certificate'),
|
||||||
$message = document.getElementById('message'),
|
$message = document.getElementById('message'),
|
||||||
|
$messageFile = document.getElementById('messageFile'),
|
||||||
|
$messageFileError = document.getElementById('messageFileError'),
|
||||||
$hash = document.getElementById('hash'),
|
$hash = document.getElementById('hash'),
|
||||||
$hashError = document.getElementById('hashError'),
|
$hashError = document.getElementById('hashError'),
|
||||||
$signature = document.getElementById('signature'),
|
$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) {
|
$createSignature.addEventListener('submit', function (event) {
|
||||||
var thumbprint = $certificate.value,
|
var messageFile = $messageFile && $messageFile.files.length && $messageFile.files[0],
|
||||||
message = $message.value;
|
messagePromise = Promise.resolve($message.value);
|
||||||
|
|
||||||
|
if (messageFile) {
|
||||||
|
messagePromise = readFile(messageFile);
|
||||||
|
}
|
||||||
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
$hash.placeholder = 'Вычисляется...';
|
messagePromise.then(function (message) {
|
||||||
$hash.value = '';
|
$hash.placeholder = 'Вычисляется...';
|
||||||
|
$hash.value = '';
|
||||||
|
|
||||||
window.cryptoPro.createHash(message).then(function (hash) {
|
window.cryptoPro.createHash(message).then(createSignature.bind(null, message), function (hashError) {
|
||||||
var detachedSignature = document.querySelector('input[name="signatureType"]:checked').value,
|
$hash.placeholder = 'Не вычислен';
|
||||||
signaturePromise;
|
$hashError.textContent = hashError.message;
|
||||||
|
|
||||||
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;
|
|
||||||
});
|
});
|
||||||
}, function (error) {
|
}, function (fileError) {
|
||||||
$hash.placeholder = 'Не вычислен';
|
$messageFileError.textContent = fileError;
|
||||||
|
})
|
||||||
$hashError.textContent = error.message;
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
@ -13,6 +13,14 @@
|
|||||||
<textarea id="message" cols="80" rows="5" placeholder="Введите сообщение" autofocus required>Привет мир!</textarea>
|
<textarea id="message" cols="80" rows="5" placeholder="Введите сообщение" autofocus required>Привет мир!</textarea>
|
||||||
<br><br>
|
<br><br>
|
||||||
|
|
||||||
|
<!--[if gte IE 10]><!-->
|
||||||
|
<label for="messageFile">Или файл для подписи:</label>
|
||||||
|
<br/>
|
||||||
|
<input id="messageFile" type="file">
|
||||||
|
<pre id="messageFileError"></pre>
|
||||||
|
<!--<![endif]-->
|
||||||
|
<hr>
|
||||||
|
|
||||||
<label for="certificate">Сертификат: *</label>
|
<label for="certificate">Сертификат: *</label>
|
||||||
<br>
|
<br>
|
||||||
<select id="certificate" tabindex="-1" required>
|
<select id="certificate" tabindex="-1" required>
|
||||||
|
Loading…
Reference in New Issue
Block a user