Актуализировал пример использования с Angular

This commit is contained in:
vgoma 2020-09-01 18:39:43 +03:00
parent d9797b31a5
commit 3d71049078
2 changed files with 155 additions and 38 deletions

View File

@ -1,34 +1,106 @@
<select name="certificate" <form
[(ngModel)]="thumbprint"> #signatureForm="ngForm"
<option value="null" disabled>Не выбран</option> novalidate
(ngSubmit)="createSignature(thumbprint)">
<fieldset>
<legend>Создание подписи</legend>
<label for="message">Подписываемое сообщение: *</label>
<br>
<textarea
id="message"
name="message"
cols="80"
rows="5"
placeholder="Введите сообщение"
autofocus
required
[(ngModel)]="message">Привет мир!</textarea>
<br><br>
<option *ngFor="let cert of certificateList" <label for="certificate">Сертификат: *</label>
[value]="cert.thumbprint"> <br>
{{cert.name}} <select
</option> id="certificate"
</select> name="certificate"
required
[(ngModel)]="thumbprint">
<option value="null" disabled>Не выбран</option>
<option *ngFor="let cert of certificateList" [value]="cert.thumbprint">
{{cert.name + ' (действителен до: ' + cert.validTo + ')'}}
</option>
</select>
<pre>{{certificateListError}}</pre>
<button type="button" <details
[disabled]="!thumbprint" *ngIf="thumbprint"
(click)="createSignature(thumbprint)"> (click)="showCertInfo(thumbprint)">
Создать подпись <summary>Информация о сертификате</summary>
</button> <pre *ngIf="certInfo">{{certInfo | json}}</pre>
<pre *ngIf="!certInfo">Запрашивается...</pre>
</details>
<pre>{{certificateInfoError}}</pre>
<button type="button" <label>Тип подписи: *</label>
[disabled]="!thumbprint" <br>
(click)="showCertInfo(thumbprint)"> <label>
Информация о сертификате <input
</button> type="radio"
name="detachedSignature"
[value]="false"
[(ngModel)]="detachedSignature">Совмещенная</label>
<br>
<label>
<input
type="radio"
name="detachedSignature"
[value]="true"
[(ngModel)]="detachedSignature">Отделенная</label>
<br><br>
<pre *ngIf="certInfo">{{certInfo | json}}</pre> <hr>
<button
type="submit"
[disabled]="!signatureForm.valid">Создать подпись</button>
</fieldset>
</form>
<br> <fieldset>
<legend>Результат</legend>
<label for="hash">Хэш (ГОСТ Р 34.11-2012 256 бит):</label><br>
<textarea
id="hash"
cols="80"
rows="5"
[(ngModel)]="hash"
[placeholder]="hashStatus"
></textarea>
<pre>{{hashError}}</pre>
<textarea name="signature" <label for="signature">Подпись (PKCS7):</label><br>
[(ngModel)]="signature" <textarea
cols="100" id="signature"
rows="30"></textarea> name="signature"
cols="80"
rows="30"
[placeholder]="signatureStatus"
[(ngModel)]="signature"
></textarea>
<pre>{{signatureError}}</pre>
<pre *ngIf="error">{{error}}</pre> <p>
Для
<a href="https://www.gosuslugi.ru/pgu/eds/"
target="_blank"
rel="nofollow noopener noreferrer"
title="Перейти к проверке подписи">проверки</a>
нужно создать файл со сгенерированной подписью в кодировке UTF-8 с расширением *.sgn
<br>
для отделенной подписи (или *.sig для совмещенной).
</p>
</fieldset>
<pre *ngIf="systemInfo">{{systemInfo | json}}</pre> <fieldset>
<legend>Информация о системе</legend>
<pre *ngIf="systemInfo">{{systemInfo | json}}</pre>
<pre>{{systemInfoError}}</pre>
</fieldset>

View File

@ -4,7 +4,9 @@ import {
getUserCertificates, getUserCertificates,
getSystemInfo, getSystemInfo,
isValidSystemSetup, isValidSystemSetup,
createSignature, createHash,
createDetachedSignature,
createAttachedSignature,
SystemInfo, SystemInfo,
Certificate Certificate
} from 'crypto-pro'; } from 'crypto-pro';
@ -15,14 +17,23 @@ import {
styleUrls: ['./crypto-pro.component.css'] styleUrls: ['./crypto-pro.component.css']
}) })
export class CryptoProComponent implements OnInit { export class CryptoProComponent implements OnInit {
public message = 'Привет мир!';
public certificateList: Certificate[] = []; public certificateList: Certificate[] = [];
public hash: string = null;
public hashStatus = 'Не вычислен';
public detachedSignature = true;
public thumbprint: string = null; public thumbprint: string = null;
public signature: string; public signature: string = null;
public signatureStatus = 'Не создана';
public systemInfo: SystemInfo & { public systemInfo: SystemInfo & {
isValidSystemSetup: boolean; isValidSystemSetup: boolean;
}; };
public error: string; public certificateListError: string = null;
public certInfo; public certificateInfoError: string = null;
public hashError: string = null;
public signatureError: string = null;
public systemInfoError: string = null;
public certInfo = null;
constructor() { } constructor() { }
@ -32,18 +43,48 @@ export class CryptoProComponent implements OnInit {
} }
public async createSignature(thumbprint) { public async createSignature(thumbprint) {
// Вычислинный hash по ГОСТ Р 34.11-94 для строки: "abc" this.hash = null;
const hash = 'b285056dbf18d7392d7677369524dd14747459ed8143997e163b2986f92fd42c'; this.hashError = null;
const hashBase64 = window.btoa(hash); this.signature = null;
this.signatureError = null;
this.hashStatus = 'Вычисляется...';
try { try {
this.signature = await createSignature(thumbprint, hashBase64); this.hash = await createHash(this.message);
} catch (error) { } catch (error) {
this.error = error.message; this.hashError = error.message;
return;
} }
this.hashStatus = 'Не вычислен';
this.signatureStatus = 'Создается...';
if (this.detachedSignature) {
try {
this.signature = await createDetachedSignature(thumbprint, this.hash);
} catch (error) {
this.signatureError = error.message;
}
this.signatureStatus = 'Не создана';
return;
}
try {
this.signature = await createAttachedSignature(thumbprint, this.message);
} catch (error) {
this.signatureError = error.message;
}
this.signatureStatus = 'Не создана';
} }
public async showCertInfo(thumbprint) { public async showCertInfo(thumbprint) {
this.certInfo = null;
this.certificateInfoError = null;
try { try {
const certificate = await getCertificate(thumbprint); const certificate = await getCertificate(thumbprint);
@ -74,26 +115,30 @@ export class CryptoProComponent implements OnInit {
]), ]),
}; };
} catch (error) { } catch (error) {
this.error = error.message; this.certificateInfoError = error.message;
} }
} }
private async displayCertificates() { private async displayCertificates() {
this.certificateListError = null;
try { try {
this.certificateList = await getUserCertificates(); this.certificateList = await getUserCertificates();
} catch (error) { } catch (error) {
this.error = error.message; this.certificateListError = error.message;
} }
} }
private async displaySystemInfo() { private async displaySystemInfo() {
this.systemInfoError = null;
try { try {
this.systemInfo = { this.systemInfo = {
...await getSystemInfo(), ...await getSystemInfo(),
isValidSystemSetup: await isValidSystemSetup() isValidSystemSetup: await isValidSystemSetup()
}; };
} catch (error) { } catch (error) {
this.error = error.message; this.systemInfoError = error.message;
} }
} }
} }