@it-enterprise/digital-signature 1.4.2 → 1.4.4

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.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "digital signature",
5
5
  "private": false,
6
6
  "main": "src/index.js",
@@ -658,11 +658,18 @@ export default class DigitalSignature {
658
658
  * @returns {Promise<boolean>} считан ли ключ
659
659
  */
660
660
  async isPrivateKeyReaded() {
661
+ if (!this._readedKey) {
662
+ return false;
663
+ }
664
+ if (this._readedKey.keyType === DigitalSignatureKeyType.WindowsCertStore) {
665
+ return true;
666
+ }
667
+ if (this._readedKey.keyType === DigitalSignatureKeyType.UmCAService) {
668
+ return true;
669
+ }
661
670
  try {
662
- return !!this._readedKey && (this._readedKey.keyType == DigitalSignatureKeyType.WindowsCertStore
663
- || this._readedKey.keyType == DigitalSignatureKeyType.UmCAService
664
- || await this._euSign.IsPrivateKeyReaded());
665
- } catch {
671
+ return await this._euSign.IsPrivateKeyReaded();
672
+ } catch (error) {
666
673
  return false;
667
674
  }
668
675
  }
@@ -776,36 +783,7 @@ export default class DigitalSignature {
776
783
  message: this._resourses.KSPSignFormatError
777
784
  };
778
785
  }
779
-
780
- const formatData = (data, toBase64) => {
781
- return toBase64 && typeof data != "string" ? arrayToBase64(data)
782
- : !toBase64 && typeof data == "string" ? base64ToArray(data)
783
- : data;
784
- };
785
- let returnArray = true;
786
- if (!Array.isArray(data)) {
787
- returnArray = false;
788
- data = [data];
789
- }
790
-
791
- for (let i = 0; i < data.length; i++) {
792
- const d = data[i];
793
- if (d.val) {
794
- d.val = formatData(d.val, true);
795
- } else {
796
- data[i] = formatData(d, true);
797
- }
798
- }
799
- const result = await this.UmCAService.signData(this._readedKey.keyMedia, data, signType);
800
- for (let i = 0; i < result.length; i++) {
801
- const d = result[i];
802
- if (d.val) {
803
- d.val = formatData(d.val, !asByteArray);
804
- } else {
805
- result[i] = formatData(d, !asByteArray);
806
- }
807
- }
808
- return returnArray ? result : result[0];
786
+ return await this.UmCAService.signData(this._readedKey.keyMedia, data, signType, asByteArray);
809
787
  }
810
788
 
811
789
  const signAlgo = this._readedKey.getSignAlgo();
@@ -1,4 +1,5 @@
1
- import { EndUserConstants, EndUserError} from "../euscp/euscp";
1
+ import { EndUserConstants, EndUserError } from "../euscp/euscp";
2
+ import { arrayToBase64, base64ToArray } from "./Utils";
2
3
 
3
4
  export default class UmCAService {
4
5
  constructor(port) {
@@ -47,7 +48,7 @@ export default class UmCAService {
47
48
  });
48
49
  }
49
50
 
50
- async signData(key, toSign, signType) {
51
+ async signData(key, toSign, signType, asByteArray) {
51
52
  let sigType;
52
53
 
53
54
  if (signType.type === EndUserConstants.EU_SIGN_CONTAINER_TYPE_PADES) {
@@ -67,6 +68,17 @@ export default class UmCAService {
67
68
  if (!Array.isArray(toSign)) {
68
69
  returnArray = false;
69
70
  toSign = [toSign];
71
+ } else {
72
+ toSign = [...toSign];
73
+ }
74
+
75
+ for (let i = 0; i < toSign.length; i++) {
76
+ const d = toSign[i];
77
+ if (d.val) {
78
+ toSign[i] = { name: d.name, val: formatData(d.val, true) };
79
+ } else {
80
+ toSign[i] = formatData(d, true);
81
+ }
70
82
  }
71
83
 
72
84
  const results = [];
@@ -99,6 +111,20 @@ export default class UmCAService {
99
111
  const sign = await responce.json();
100
112
  results.push(data.name ? { name: data.name, val: sign } : sign);
101
113
  }
114
+ for (let i = 0; i < results.length; i++) {
115
+ const r = results[i];
116
+ if (r.val) {
117
+ results[i] = { name: r.name, val: formatData(r.val, !asByteArray) };
118
+ } else {
119
+ results[i] = formatData(r, !asByteArray);
120
+ }
121
+ }
102
122
  return returnArray ? results : results[0];
103
123
  }
104
124
  }
125
+
126
+ const formatData = (data, toBase64) => {
127
+ return toBase64 && typeof data != "string" ? arrayToBase64(data)
128
+ : !toBase64 && typeof data == "string" ? base64ToArray(data)
129
+ : data;
130
+ };