@it-enterprise/digital-signature 1.2.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@it-enterprise/digital-signature",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "digital signature",
5
5
  "private": false,
6
6
  "main": "src/index.js",
package/readme.md CHANGED
@@ -66,7 +66,7 @@ import {
66
66
 
67
67
  # Инициализация
68
68
 
69
- Смена типа библиотеки
69
+ Инициализация библиотеки
70
70
  ```javascript
71
71
  const ds = new DigitalSignature(
72
72
  new Models.DefaultSettingProvider(
@@ -74,7 +74,7 @@ const ds = new DigitalSignature(
74
74
  userId, // id пользователя (для сохранения ключей и предпочитаемого типа ключа) (строка, или функция, возвращающая строку)
75
75
  location.pathname + "api/ds/") // Путь к API ЕЦП
76
76
  );
77
- Возвращает текущий тип ключа (файловый, аппаратный, облачный)
77
+ // Возвращает текущий тип ключа (файловый, аппаратный, облачный)
78
78
  const currentKeyType = await ds.initialise();
79
79
  ```
80
80
  Сменить тип библиотеки
@@ -138,7 +138,7 @@ export default class DigitalSignature {
138
138
  async setLibraryType(type) {
139
139
  switch (type) {
140
140
  case DigitalSignatureKeyType.Token:
141
- if (this._euSign === this._euSignKeyMedia) {
141
+ if (this._euSign === this._euSignKeyMedia && await this._euSign.IsInitialized()) {
142
142
  return;
143
143
  }
144
144
  this._euSign = this._euSignKeyMedia;
@@ -335,6 +335,29 @@ export default class DigitalSignature {
335
335
  return this._euSignKeyMedia.GetKeyMedias();
336
336
  }
337
337
 
338
+ /**
339
+ * Считать ключ по параметрам
340
+ * @param {PrivateKeyInfo} keyInfo - Параметры ключа
341
+ */
342
+ async readPrivateKeyByInfo(keyInfo) {
343
+ if (!keyInfo) {
344
+ throw {
345
+ code: EndUserError.EU_ERROR_BAD_PARAMETER,
346
+ message: this._resourses.BadParameter + " keyInfo"
347
+ };
348
+ }
349
+
350
+ await this.setLibraryType(keyInfo.keyType);
351
+ switch (keyInfo.keyType) {
352
+ case DigitalSignatureKeyType.File:
353
+ return await this.readFileKey(keyInfo.privateKey, keyInfo.password, keyInfo.certificates);
354
+ case DigitalSignatureKeyType.Token:
355
+ return await this.readHardwareKey(keyInfo.keyMedia, keyInfo.certificates);
356
+ case DigitalSignatureKeyType.KSP:
357
+ return await this.readPrivateKeyKSP(keyInfo.ksp, keyInfo.kspUserId, false);
358
+ }
359
+ }
360
+
338
361
  /**
339
362
  * Считать аппаратный ключ
340
363
  * @param {EndUserKeyMedia} keyMedia - Параметры аппаратного ключа
@@ -777,9 +800,9 @@ export default class DigitalSignature {
777
800
  */
778
801
  async verifyHash(hash, sign, signIndex) {
779
802
  if (!Number.isInteger(signIndex)) {
780
- signIndex = 0;
803
+ signIndex = -1;
781
804
  }
782
- return await this._euSign.VerifyHash(hash, sign, signIndex);
805
+ return await this._euSign.VerifyHash(hash.val || hash, sign.val || sign, signIndex);
783
806
  }
784
807
 
785
808
  /**
@@ -808,7 +831,7 @@ export default class DigitalSignature {
808
831
  const result = [];
809
832
  for (let i = 0; i < signs.length; i++) {
810
833
  const sign = signs[i];
811
- let signInfo = await this.verifyData(data[i], sign);
834
+ let signInfo = await this.verifyData(data[i].val || data[i], sign.val || sign);
812
835
  if (Array.isArray(signInfo)) {
813
836
  signInfo = signInfo[signInfo.length - 1];
814
837
  }
@@ -825,7 +848,7 @@ export default class DigitalSignature {
825
848
  }
826
849
  return result;
827
850
  } else {
828
- let signInfo = await this.verifyData(data, signs);
851
+ let signInfo = await this.verifyData(data.val || data, signs.val || signs);
829
852
  if (Array.isArray(signInfo)) {
830
853
  signInfo = signInfo[signInfo.length - 1];
831
854
  }
@@ -844,20 +867,11 @@ export default class DigitalSignature {
844
867
 
845
868
  /**
846
869
  * Выполнить подписание с проверкой подписи
847
- * @param {string | NamedData} fileUrl - ссылка на загрузку файла для подписания
848
- * @param {boolean} hash - подписывать хеш
870
+ * @param {string | NamedData | Array<string> | Array<NamedData>} fileUrl - ссылка на загрузку файла для подписания
871
+ * @param {EndUserSignContainerInfo?} signType - Тип подписи
849
872
  */
850
- async signFileEx(fileUrl, hash) {
851
- const isNamedData = typeof fileUrl === "object";
852
- let data = await downloadData(isNamedData ? fileUrl.val : fileUrl, "binary");
853
- if (isNamedData) {
854
- data = {name: fileUrl.name, val: data};
855
- }
856
- if (hash) {
857
- return await this.signHashEx(data);
858
- } else {
859
- return await this.signDataEx(data, false);
860
- }
873
+ async signFileEx(fileUrl, signType) {
874
+ return await downloadAndSignFiles(fileUrl, async (data) => await this.signDataEx(data, signType));
861
875
  }
862
876
 
863
877
  /**
@@ -865,19 +879,44 @@ export default class DigitalSignature {
865
879
  * @param {string | NamedData} hash - хеш для подписания
866
880
  */
867
881
  async signHashEx(hash) {
868
- const isNamedData = typeof hash === "object";
869
- const signature = await this.signHash(isNamedData ? hash.val : hash);
870
- const signatureInfo = await this.verifyHash(isNamedData ? hash.val : hash, isNamedData ? signature.val : signature);
871
- return {
872
- Success: true,
873
- Sign: isNamedData ? signature.val : signature,
874
- SignatureInfo: {
875
- Success: true,
876
- DateTimeStr: signatureInfo.timeInfo.time,
877
- Signer: signatureInfo.ownerInfo.subjCN,
878
- OwnerInfo: signatureInfo.ownerInfo
882
+ const signs = await this.signHash(hash);
883
+
884
+ if (Array.isArray(hash)) {
885
+ const result = [];
886
+ for (let i = 0; i < signs.length; i++) {
887
+ const sign = signs[i];
888
+ let signInfo = await this.verifyHash(hash[i].val || hash[i], sign.val || sign);
889
+ if (Array.isArray(signInfo)) {
890
+ signInfo = signInfo[signInfo.length - 1];
891
+ }
892
+ result[i] = {
893
+ Success: true,
894
+ Sign: sign.val || sign,
895
+ SignatureInfo: {
896
+ Success: true,
897
+ DateTimeStr: signInfo.timeInfo.time,
898
+ Signer: signInfo.ownerInfo.subjCN,
899
+ OwnerInfo: signInfo.ownerInfo
900
+ }
901
+ };
879
902
  }
880
- };
903
+ return result;
904
+ } else {
905
+ let signInfo = await this.verifyHash(hash.val || hash, signs.val || signs);
906
+ if (Array.isArray(signInfo)) {
907
+ signInfo = signInfo[signInfo.length - 1];
908
+ }
909
+ return {
910
+ Success: true,
911
+ Sign: signs.val || signs,
912
+ SignatureInfo: {
913
+ Success: true,
914
+ DateTimeStr: signInfo.timeInfo.time,
915
+ Signer: signInfo.ownerInfo.subjCN,
916
+ OwnerInfo: signInfo.ownerInfo
917
+ }
918
+ };
919
+ }
881
920
  }
882
921
 
883
922
  /**
@@ -938,7 +977,7 @@ export default class DigitalSignature {
938
977
  * @param {boolean} toLocalStorage - Будет ли ключ сохранён после закрытия вкладки
939
978
  */
940
979
  async storePrivateKeyInfo(privateKeyInfo, toLocalStorage) {
941
- if (privateKeyInfo.ksp && privateKeyInfo.ksp.needQrcode) {
980
+ if (privateKeyInfo.ksp && privateKeyInfo.ksp.needQRCode) {
942
981
  return;
943
982
  }
944
983
 
@@ -1015,8 +1054,8 @@ export default class DigitalSignature {
1015
1054
  */
1016
1055
  async removeStoredPrivateKeyInfo(keyId) {
1017
1056
  if (keyId === undefined) {
1018
- localStorage.removeItem(this._userId + this.PRIVATE_KEY_INFO);
1019
- sessionStorage.removeItem(this._userId + this.PRIVATE_KEY_INFO);
1057
+ localStorage.removeItem(this._userId + PRIVATE_KEY_INFO);
1058
+ sessionStorage.removeItem(this._userId + PRIVATE_KEY_INFO);
1020
1059
  }
1021
1060
  else {
1022
1061
  const localStoredKeys = await this.getPrivateKeyInfoFromStorage(true);
@@ -1028,7 +1067,7 @@ export default class DigitalSignature {
1028
1067
  if (keyIndex > -1) {
1029
1068
  keys.splice(keyIndex, 1);
1030
1069
  const data = await this._euSignFile.ProtectDataByPassword(JSON.stringify(keys), "", true);
1031
- storage[this._userId + this.PRIVATE_KEY_INFO] = data;
1070
+ storage[this._userId + PRIVATE_KEY_INFO] = data;
1032
1071
  }
1033
1072
  }
1034
1073
  }
package/src/Utils.js CHANGED
@@ -213,7 +213,7 @@ export async function downloadAndSignFiles(fileUrl, signFunc) {
213
213
  : { name: getFileNameFromUrl(url), val: await downloadData(url, "binary") }));
214
214
 
215
215
  const signs = await signFunc(fileUrl);
216
- return returnNamedData ? signs : signs.map(sign => sign.val);
216
+ return returnNamedData ? signs : signs.map(sign => sign.val || sign);
217
217
  } else {
218
218
  if (!isParamValid(fileUrl)) {
219
219
  throw {
@@ -229,6 +229,6 @@ export async function downloadAndSignFiles(fileUrl, signFunc) {
229
229
  : { name: getFileNameFromUrl(fileUrl), val: await downloadData(fileUrl, "binary") };
230
230
 
231
231
  const sign = await signFunc(fileUrl);
232
- return returnNamedData ? sign : sign.val;
232
+ return returnNamedData ? sign : (sign.val || sign);
233
233
  }
234
234
  }