@it-enterprise/digital-signature 1.4.7 → 1.5.0
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/dist/euscp.worker.js +8 -0
- package/dist/it-digital-signature.mjs +364 -201
- package/dist/it-digital-signature.mjs.map +1 -1
- package/dist/it-digital-signature.umd.js +1 -1
- package/dist/it-digital-signature.umd.js.map +1 -1
- package/package.json +2 -3
- package/readme.md +1 -1
- package/src/DigitalSignature.js +68 -22
- package/src/GlSign.js +1 -1
- package/src/Utils.js +334 -57
- package/src/umcaservice.js +8 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@it-enterprise/digital-signature",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "IT-Enterprise package for digital signature",
|
|
5
5
|
"private": false,
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -38,9 +38,8 @@
|
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build-worker": "powershell bin/worker.ps1",
|
|
41
|
-
"prebuild": "npm run build-worker",
|
|
42
41
|
"dev": "vite",
|
|
43
42
|
"build": "vite build",
|
|
44
|
-
"prepublishOnly": "npm run build"
|
|
43
|
+
"prepublishOnly": "npm run build && npm run build-worker"
|
|
45
44
|
}
|
|
46
45
|
}
|
package/readme.md
CHANGED
|
@@ -128,7 +128,7 @@ const ds = new DigitalSignature(
|
|
|
128
128
|
worker)
|
|
129
129
|
);
|
|
130
130
|
// Возвращает текущий тип ключа (файловый, аппаратный, облачный)
|
|
131
|
-
const currentKeyType = await ds.
|
|
131
|
+
const currentKeyType = await ds.initialize();
|
|
132
132
|
```
|
|
133
133
|
Сменить тип библиотеки
|
|
134
134
|
```javascript
|
package/src/DigitalSignature.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { EndUser, EndUserConstants, EndUserProxySettings, EndUserError, KSPSettings, EndUserCertificate, EndUserKeyMedia, EndUserSignContainerInfo,
|
|
1
|
+
import { EndUser, EndUserConstants, EndUserProxySettings, EndUserError, KSPSettings, EndUserCertificate, EndUserKeyMedia, EndUserSignContainerInfo, EndUserTaxReport } from "../euscp/euscp";
|
|
2
2
|
import { DigitalSignatureKeyType, PrivateKeyInfo, FilePrivateKeyInfo, HardwarePrivateKeyInfo, KspPrivateKeyInfo, EndUserKSP, KSPKeyMediasResponse, KSPUserAuthData, KSPPrivateKeyAuthData, DigitalSignatureKeyTypeWindowsCertStore, ProtectTaxReportParams } from "./Models";
|
|
3
|
-
import { arrayToBase64, base64ToArray,
|
|
3
|
+
import { arrayToBase64, base64ToArray, dateWithoutTimezone, downloadAndSignFiles, downloadData, format, isDefaultValue, readFile } from "./Utils";
|
|
4
4
|
import UmCAService from "./umcaservice";
|
|
5
5
|
import Resourses from "./Resourses.json";
|
|
6
6
|
import GlSign from "./GlSign";
|
|
@@ -37,6 +37,9 @@ export default class DigitalSignature {
|
|
|
37
37
|
|
|
38
38
|
/** @type {PrivateKeyInfo} */
|
|
39
39
|
this._readedKey = null;
|
|
40
|
+
|
|
41
|
+
this._isInitializing = false;
|
|
42
|
+
this._initializationPromise = Promise.resolve(DigitalSignatureKeyType.File);
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
/**
|
|
@@ -89,8 +92,37 @@ export default class DigitalSignature {
|
|
|
89
92
|
* Инициализировать библиотеку
|
|
90
93
|
* @returns {Promise<number>} Текущий тип библиотеки
|
|
91
94
|
*/
|
|
92
|
-
|
|
93
|
-
|
|
95
|
+
initialise() {
|
|
96
|
+
// обратная совместимость
|
|
97
|
+
return this.initialize();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Инициализировать библиотеку
|
|
102
|
+
* @returns {Promise<number>} Текущий тип библиотеки
|
|
103
|
+
*/
|
|
104
|
+
initialize() {
|
|
105
|
+
// защита от одновременного вызова инициализации из нескольких источников
|
|
106
|
+
if (this._isInitializing) {
|
|
107
|
+
return this._initializationPromise;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
this._isInitializing = true;
|
|
111
|
+
this._initializationPromise = new Promise(async (resolve, reject) => {
|
|
112
|
+
try {
|
|
113
|
+
const result = await this._initialize();
|
|
114
|
+
resolve(result);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
reject(error);
|
|
117
|
+
} finally {
|
|
118
|
+
this._isInitializing = false;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
return this._initializationPromise;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async _initialize() {
|
|
125
|
+
if (!this._glSign) {
|
|
94
126
|
this._glSign = await Promise.resolve(this._settingsProvider.getGlSign());
|
|
95
127
|
}
|
|
96
128
|
|
|
@@ -125,6 +157,11 @@ export default class DigitalSignature {
|
|
|
125
157
|
|
|
126
158
|
const euSign = this._euSign;
|
|
127
159
|
|
|
160
|
+
const isInitialised = await euSign.IsInitialized();
|
|
161
|
+
if (isInitialised) {
|
|
162
|
+
return this.getLibraryType();
|
|
163
|
+
}
|
|
164
|
+
|
|
128
165
|
if (euSign === this._euSignKeyMedia) {
|
|
129
166
|
const library = await euSign.GetLibraryInfo();
|
|
130
167
|
|
|
@@ -178,11 +215,6 @@ export default class DigitalSignature {
|
|
|
178
215
|
}
|
|
179
216
|
}
|
|
180
217
|
|
|
181
|
-
const isInitialised = await euSign.IsInitialized();
|
|
182
|
-
if (isInitialised) {
|
|
183
|
-
return this.getLibraryType();
|
|
184
|
-
}
|
|
185
|
-
|
|
186
218
|
if (!this._euSettings) {
|
|
187
219
|
let certificates;
|
|
188
220
|
try {
|
|
@@ -431,18 +463,30 @@ export default class DigitalSignature {
|
|
|
431
463
|
}
|
|
432
464
|
|
|
433
465
|
try {
|
|
434
|
-
|
|
466
|
+
let ownerInfo;
|
|
467
|
+
try {
|
|
468
|
+
ownerInfo = await this._euSign.ReadPrivateKeyBinary(privateKey, password, certs, this._selectedIssuerCN);
|
|
469
|
+
} catch (error) {
|
|
470
|
+
// Если в контейнере ключа записаны старые сертификаты, попробовать загрузить новые сертификаты по протоколу CMP
|
|
471
|
+
if (error && error.code === EndUserError.EU_ERROR_CERT_BAD_BY_OCSP) {
|
|
472
|
+
const { certs } = await this._searchPrivateKeyCertificatesWithCMP_JS(privateKey, password, this._selectedIssuerCN);
|
|
473
|
+
ownerInfo = await this._euSign.ReadPrivateKeyBinary(privateKey, password, certs, this._selectedIssuerCN);
|
|
474
|
+
} else {
|
|
475
|
+
throw error;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
const ownCertifcates = await this._euSign.GetOwnCertificates();
|
|
435
479
|
this._readedKey = new FilePrivateKeyInfo(
|
|
436
480
|
DigitalSignatureKeyType.File,
|
|
437
481
|
ownerInfo,
|
|
438
|
-
|
|
482
|
+
ownCertifcates,
|
|
439
483
|
privateKey,
|
|
440
484
|
password
|
|
441
485
|
);
|
|
442
486
|
this._readedKey._glSign = this._glSign;
|
|
443
487
|
return this._readedKey;
|
|
444
488
|
} catch (error) {
|
|
445
|
-
if (error && error.code === EndUserError.EU_ERROR_CERT_NOT_FOUND) {
|
|
489
|
+
if (error && (error.code === EndUserError.EU_ERROR_CERT_NOT_FOUND)) {
|
|
446
490
|
if (!this._selectedIssuerCN) {
|
|
447
491
|
throw {
|
|
448
492
|
code: EndUserError.EU_ERROR_CERT_NOT_FOUND,
|
|
@@ -488,6 +532,10 @@ export default class DigitalSignature {
|
|
|
488
532
|
return file.name.endsWith(".jks");
|
|
489
533
|
}
|
|
490
534
|
|
|
535
|
+
async _searchPrivateKeyCertificatesWithCMP_JS(privateKey, password, caCN) {
|
|
536
|
+
return await this._euSignFile.m_library.m_worker.postMessage("CallSearchPrivateKeyCertificatesWithCMP", [privateKey, password, caCN]);
|
|
537
|
+
}
|
|
538
|
+
|
|
491
539
|
/**
|
|
492
540
|
* Считать ключ с DepositSign
|
|
493
541
|
* @param {string} userId - Идентификатор пользователя
|
|
@@ -781,7 +829,7 @@ export default class DigitalSignature {
|
|
|
781
829
|
};
|
|
782
830
|
}
|
|
783
831
|
|
|
784
|
-
const hash = await this._euSign.HashData(this._readedKey.getHashAlgo(), data,
|
|
832
|
+
const hash = await this._euSign.HashData(this._readedKey.getHashAlgo(), data, true);
|
|
785
833
|
const signedData = await this._signHashWindowsCertificateStore(hash, signType, asByteArray);
|
|
786
834
|
|
|
787
835
|
return this._euSign.m_library.m_worker.postMessage("AppendDataToSignedData", [signedData, data, !asByteArray]);
|
|
@@ -912,14 +960,13 @@ export default class DigitalSignature {
|
|
|
912
960
|
} else if (signContainerInfo.type === EndUserConstants.EndUserSignContainerType.XAdES) {
|
|
913
961
|
return await this._euSign.XAdESVerifyData(data, sign.val || sign, signIndex);
|
|
914
962
|
} else if (signContainerInfo.type === EndUserConstants.EndUserSignContainerType.CAdES) {
|
|
915
|
-
if (
|
|
916
|
-
if (signContainerInfo.subType === EndUserConstants.EndUserCAdESType.Detached) {
|
|
917
|
-
throw {
|
|
918
|
-
code: EndUserError.EU_ERROR_BAD_PARAMETER,
|
|
919
|
-
message: this._resourses.BadParameter + " data"
|
|
920
|
-
};
|
|
921
|
-
}
|
|
963
|
+
if (signContainerInfo.subType === EndUserConstants.EndUserCAdESType.Enveloped) {
|
|
922
964
|
return await this._euSign.VerifyDataInternal(sign.val || sign, signIndex);
|
|
965
|
+
} else if (!data) {
|
|
966
|
+
throw {
|
|
967
|
+
code: EndUserError.EU_ERROR_BAD_PARAMETER,
|
|
968
|
+
message: this._resourses.BadParameter + " data"
|
|
969
|
+
};
|
|
923
970
|
}
|
|
924
971
|
}
|
|
925
972
|
return await this._euSign.VerifyData(data.val || data, sign.val || sign, signIndex);
|
|
@@ -1169,7 +1216,7 @@ export default class DigitalSignature {
|
|
|
1169
1216
|
const that = this;
|
|
1170
1217
|
async function signFunc(signValue) {
|
|
1171
1218
|
if (typeof signValue != "string") {
|
|
1172
|
-
signValue =
|
|
1219
|
+
signValue = arrayToBase64(signValue);
|
|
1173
1220
|
}
|
|
1174
1221
|
|
|
1175
1222
|
let signedData;
|
|
@@ -1603,6 +1650,5 @@ export default class DigitalSignature {
|
|
|
1603
1650
|
message: this._resourses.DiiaHealthCheckError
|
|
1604
1651
|
};
|
|
1605
1652
|
}
|
|
1606
|
-
|
|
1607
1653
|
}
|
|
1608
1654
|
}
|
package/src/GlSign.js
CHANGED
package/src/Utils.js
CHANGED
|
@@ -87,11 +87,13 @@ export function makeUrl(url) {
|
|
|
87
87
|
return origin + url;
|
|
88
88
|
}
|
|
89
89
|
if (origin && pathname) {
|
|
90
|
-
return origin + pathname.
|
|
90
|
+
return origin + pathname.substring(0, pathname.lastIndexOf("/")) + "/" + url;
|
|
91
91
|
}
|
|
92
92
|
return url;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
//--------------------------------------------------------------------------------
|
|
96
|
+
|
|
95
97
|
export function format(format) {
|
|
96
98
|
const args = Array.prototype.slice.call(arguments, 1);
|
|
97
99
|
return format.replace(/{(\d+)}/g, function (match, number) {
|
|
@@ -99,6 +101,8 @@ export function format(format) {
|
|
|
99
101
|
});
|
|
100
102
|
}
|
|
101
103
|
|
|
104
|
+
//--------------------------------------------------------------------------------
|
|
105
|
+
|
|
102
106
|
export function readFile(file) {
|
|
103
107
|
return new Promise((resolve, reject) => {
|
|
104
108
|
const reader = new FileReader();
|
|
@@ -113,83 +117,70 @@ export function readFile(file) {
|
|
|
113
117
|
});
|
|
114
118
|
}
|
|
115
119
|
|
|
116
|
-
|
|
117
|
-
let base64Str = "";
|
|
118
|
-
const m_map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
119
|
-
|
|
120
|
-
const bytes = new Uint8Array(array);
|
|
121
|
-
const bytesLength = bytes.length;
|
|
120
|
+
//--------------------------------------------------------------------------------
|
|
122
121
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
if (bytesLength % 3 === 2) {
|
|
132
|
-
base64Str = base64Str.substring(0, base64Str.length - 1) + "=";
|
|
133
|
-
} else if (bytesLength % 3 === 1) {
|
|
134
|
-
base64Str = base64Str.substring(0, base64Str.length - 2) + "==";
|
|
122
|
+
/**
|
|
123
|
+
* Convert Uint8Array to Base64 string
|
|
124
|
+
* @param {Uint8Array} buffer
|
|
125
|
+
* @returns {string}
|
|
126
|
+
*/
|
|
127
|
+
export function arrayToBase64(buffer) {
|
|
128
|
+
if (typeof buffer.toBase64 === "function") {
|
|
129
|
+
return buffer.toBase64();
|
|
135
130
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return base64Str;
|
|
131
|
+
return new EndUserBase64Coder().encode(buffer);
|
|
139
132
|
}
|
|
140
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Convert Base64 string to Uint8Array
|
|
136
|
+
* @param {string} str
|
|
137
|
+
* @returns {Uint8Array}
|
|
138
|
+
*/
|
|
141
139
|
export function base64ToArray(str) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
const array = new Uint8Array(new ArrayBuffer(rawLength));
|
|
145
|
-
|
|
146
|
-
for (let i = 0; i < rawLength; i++) {
|
|
147
|
-
array[i] = raw.charCodeAt(i);
|
|
140
|
+
if (typeof Uint8Array.fromBase64 === "function") {
|
|
141
|
+
return Uint8Array.fromBase64(str);
|
|
148
142
|
}
|
|
149
|
-
return
|
|
143
|
+
return new EndUserBase64Coder().decode(str);
|
|
150
144
|
}
|
|
151
145
|
|
|
146
|
+
/**
|
|
147
|
+
* Converts string to Uint8Array using UTF-16 encoding
|
|
148
|
+
* @param {string} s
|
|
149
|
+
* @returns {Uint8Array}
|
|
150
|
+
*/
|
|
152
151
|
export function stringToArray(s) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const arr = [];
|
|
156
|
-
for (let i = 0; i < utf8.length; i++) {
|
|
157
|
-
arr.push(utf8.charCodeAt(i));
|
|
158
|
-
}
|
|
159
|
-
return arr;
|
|
152
|
+
return new Uint8Array(new EndUserUTF16LECoder().encode(s));
|
|
160
153
|
}
|
|
161
154
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/* eslint-disable no-magic-numbers */
|
|
172
|
-
for (let i = 0; i < length; i++) {
|
|
173
|
-
let chr = arr[i];
|
|
174
|
-
if (chr > 0xFF) {
|
|
175
|
-
chr &= 0xFF;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
ret.push(String.fromCharCode(chr));
|
|
179
|
-
}
|
|
180
|
-
/* eslint-enable no-magic-numbers */
|
|
181
|
-
|
|
182
|
-
return decodeURIComponent(escape(ret.join("")));
|
|
155
|
+
/**
|
|
156
|
+
* Converts Uint8Array to string using UTF-16 encoding
|
|
157
|
+
* @param {Uint8Array} buffer
|
|
158
|
+
* @returns {string}
|
|
159
|
+
*/
|
|
160
|
+
export function arrayToString(buffer) {
|
|
161
|
+
return new EndUserUTF16LECoder().decode(buffer);
|
|
183
162
|
}
|
|
184
163
|
|
|
164
|
+
/**
|
|
165
|
+
* Converts regular string to Base64 string using UTF-16 encoding
|
|
166
|
+
* @param {string} str
|
|
167
|
+
* @returns {string}
|
|
168
|
+
*/
|
|
185
169
|
export function convertToBase64(str) {
|
|
186
170
|
return arrayToBase64(stringToArray(str));
|
|
187
171
|
}
|
|
188
172
|
|
|
173
|
+
/**
|
|
174
|
+
* Converts Base64 string to regular string using UTF-16 encoding
|
|
175
|
+
* @param {string} str
|
|
176
|
+
* @returns {string}
|
|
177
|
+
*/
|
|
189
178
|
export function convertFromBase64(str) {
|
|
190
179
|
return arrayToString(base64ToArray(str));
|
|
191
180
|
}
|
|
192
181
|
|
|
182
|
+
//--------------------------------------------------------------------------------
|
|
183
|
+
|
|
193
184
|
/**
|
|
194
185
|
* Получить алгоритм хеширования для алгоритма подписи
|
|
195
186
|
* @param {EndUserConstants.EndUserSignAlgo} signAlgo - Алгоритм подписи
|
|
@@ -240,6 +231,8 @@ export function getSupportedSignAlgos(certificates, glsign = null) {
|
|
|
240
231
|
return signAlgos;
|
|
241
232
|
}
|
|
242
233
|
|
|
234
|
+
//--------------------------------------------------------------------------------
|
|
235
|
+
|
|
243
236
|
export function isUrlValid(url) {
|
|
244
237
|
try {
|
|
245
238
|
return !!new URL(url);
|
|
@@ -294,11 +287,15 @@ export function isDefaultValue(value) {
|
|
|
294
287
|
return value === false || value === null || typeof value === "undefined";
|
|
295
288
|
}
|
|
296
289
|
|
|
290
|
+
//--------------------------------------------------------------------------------
|
|
291
|
+
|
|
297
292
|
// отсчёт месяцев в JS начинается с нуля -_-
|
|
298
293
|
// реальная дата - 01.07.2026
|
|
299
294
|
/* eslint-disable no-magic-numbers */
|
|
300
295
|
export const IS_DSTU_7564_DAY = () => Date.now() >= new Date(2026, 6, 1);
|
|
301
296
|
|
|
297
|
+
//--------------------------------------------------------------------------------
|
|
298
|
+
|
|
302
299
|
export function dateWithoutTimezone(date) {
|
|
303
300
|
const tzoffset = date.getTimezoneOffset() * 60000; //offset in milliseconds
|
|
304
301
|
const withoutTimezone = new Date(date.valueOf() - tzoffset)
|
|
@@ -306,3 +303,283 @@ export function dateWithoutTimezone(date) {
|
|
|
306
303
|
.slice(0, -1);
|
|
307
304
|
return withoutTimezone;
|
|
308
305
|
};
|
|
306
|
+
|
|
307
|
+
//--------------------------------------------------------------------------------
|
|
308
|
+
|
|
309
|
+
export class EndUserBase64Coder {
|
|
310
|
+
constructor() {
|
|
311
|
+
this.m_map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
312
|
+
this.m_codes = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
|
313
|
+
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103,
|
|
314
|
+
104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118,
|
|
315
|
+
119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47];
|
|
316
|
+
}
|
|
317
|
+
_getTextDecoder() {
|
|
318
|
+
try {
|
|
319
|
+
if (typeof TextDecoder == 'undefined')
|
|
320
|
+
return null;
|
|
321
|
+
|
|
322
|
+
const decoder = new TextDecoder("utf-8");
|
|
323
|
+
if (decoder.encoding != "utf-8")
|
|
324
|
+
return null;
|
|
325
|
+
|
|
326
|
+
return decoder;
|
|
327
|
+
} catch (e) {
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
_encodePart(bytes, begin, end) {
|
|
332
|
+
const result = [];
|
|
333
|
+
let tmp, str;
|
|
334
|
+
|
|
335
|
+
for (let i = begin; i < end; i += 3) {
|
|
336
|
+
tmp = (bytes[i] << 16) |
|
|
337
|
+
(bytes[i + 1] << 8) |
|
|
338
|
+
(bytes[i + 2]);
|
|
339
|
+
str = this.m_map[(tmp >> 18) & 0x3F] +
|
|
340
|
+
this.m_map[(tmp >> 12) & 0x3F] +
|
|
341
|
+
this.m_map[(tmp >> 6) & 0x3F] +
|
|
342
|
+
this.m_map[tmp & 0x3F];
|
|
343
|
+
|
|
344
|
+
result.push(str);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return result.join('');
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
encode(array) {
|
|
351
|
+
let bytes;
|
|
352
|
+
let i, j;
|
|
353
|
+
let tmp;
|
|
354
|
+
let length;
|
|
355
|
+
let str = '';
|
|
356
|
+
let result;
|
|
357
|
+
|
|
358
|
+
bytes = (typeof Uint8Array == 'undefined') ?
|
|
359
|
+
((typeof EndUserUint8Array == 'undefined') ?
|
|
360
|
+
array.slice(0) : new EndUserUint8Array(array)) :
|
|
361
|
+
new Uint8Array(array);
|
|
362
|
+
length = bytes.length;
|
|
363
|
+
|
|
364
|
+
if (length == 0)
|
|
365
|
+
return "";
|
|
366
|
+
|
|
367
|
+
const decoder = this._getTextDecoder();
|
|
368
|
+
if (decoder != null) {
|
|
369
|
+
result = new Uint8Array(parseInt(((length + 2) / 3)) * 4);
|
|
370
|
+
|
|
371
|
+
for (i = 0, j = 0; i < length - (length % 3); i += 3, j += 4) {
|
|
372
|
+
tmp = (bytes[i] << 16) |
|
|
373
|
+
(bytes[i + 1] << 8) |
|
|
374
|
+
(bytes[i + 2]);
|
|
375
|
+
result[j] = this.m_codes[(tmp >> 18) & 0x3F];
|
|
376
|
+
result[j + 1] = this.m_codes[(tmp >> 12) & 0x3F];
|
|
377
|
+
result[j + 2] = this.m_codes[(tmp >> 6) & 0x3F];
|
|
378
|
+
result[j + 3] = this.m_codes[tmp & 0x3F];
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
switch (length % 3) {
|
|
382
|
+
case 1:
|
|
383
|
+
tmp = (bytes[i] << 16);
|
|
384
|
+
result[j] = this.m_codes[(tmp >> 18) & 0x3F];
|
|
385
|
+
result[j + 1] = this.m_codes[(tmp >> 12) & 0x3F];
|
|
386
|
+
result[j + 2] = 61;
|
|
387
|
+
result[j + 3] = 61;
|
|
388
|
+
break;
|
|
389
|
+
case 2:
|
|
390
|
+
tmp = (bytes[i] << 16) |
|
|
391
|
+
(bytes[i + 1] << 8);
|
|
392
|
+
result[j] = this.m_codes[(tmp >> 18) & 0x3F];
|
|
393
|
+
result[j + 1] = this.m_codes[(tmp >> 12) & 0x3F];
|
|
394
|
+
result[j + 2] = this.m_codes[(tmp >> 6) & 0x3F];
|
|
395
|
+
result[j + 3] = 61;
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
398
|
+
str = decoder.decode(result);
|
|
399
|
+
} else {
|
|
400
|
+
let part = null;
|
|
401
|
+
const partLength = 5 * 1024 * 3;
|
|
402
|
+
const baseLength = length - (length % 3);
|
|
403
|
+
const partsCount = parseInt(baseLength / partLength);
|
|
404
|
+
let lastPart;
|
|
405
|
+
|
|
406
|
+
result = [];
|
|
407
|
+
|
|
408
|
+
for (i = 0; i < partsCount; i++) {
|
|
409
|
+
part = this._encodePart(bytes, i * partLength,
|
|
410
|
+
i * partLength + partLength);
|
|
411
|
+
result.push(part);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if ((partsCount * partLength) < baseLength) {
|
|
415
|
+
part = this._encodePart(bytes, i * partLength, baseLength);
|
|
416
|
+
result.push(part);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
switch (length % 3) {
|
|
420
|
+
case 1:
|
|
421
|
+
tmp = (bytes[length - 1] << 16);
|
|
422
|
+
lastPart = this.m_map[(tmp >> 18) & 0x3F];
|
|
423
|
+
lastPart += this.m_map[(tmp >> 12) & 0x3F];
|
|
424
|
+
lastPart += '=';
|
|
425
|
+
lastPart += '=';
|
|
426
|
+
result.push(lastPart);
|
|
427
|
+
break;
|
|
428
|
+
case 2:
|
|
429
|
+
tmp = (bytes[length - 2] << 16) |
|
|
430
|
+
(bytes[length - 1] << 8);
|
|
431
|
+
lastPart = this.m_map[(tmp >> 18) & 0x3F];
|
|
432
|
+
lastPart += this.m_map[(tmp >> 12) & 0x3F];
|
|
433
|
+
lastPart += this.m_map[(tmp >> 6) & 0x3F];
|
|
434
|
+
lastPart += '=';
|
|
435
|
+
result.push(lastPart);
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
str = result.join('');
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
return str;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
decode(base64Str) {
|
|
446
|
+
let bytes;
|
|
447
|
+
let bytesLength;
|
|
448
|
+
let i, j, p;
|
|
449
|
+
let base64StrLength;
|
|
450
|
+
let equals;
|
|
451
|
+
let encoded1, encoded2, encoded3, encoded4;
|
|
452
|
+
|
|
453
|
+
if ((base64Str == '') || ((base64Str.length & 0x03) > 0)) {
|
|
454
|
+
return (typeof Uint8Array == 'undefined') ?
|
|
455
|
+
((typeof EndUserUint8Array == 'undefined') ?
|
|
456
|
+
[] : new EndUserUint8Array(0)) :
|
|
457
|
+
(new Uint8Array(0));
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
base64StrLength = base64Str.length;
|
|
461
|
+
equals = (base64Str[base64StrLength - 1] == '=') ?
|
|
462
|
+
((base64Str[base64StrLength - 2] == '=') ? 2 : 1) : 0;
|
|
463
|
+
bytesLength = (base64StrLength / 4) * 3 - equals;
|
|
464
|
+
|
|
465
|
+
bytes = (typeof Uint8Array == 'undefined') ?
|
|
466
|
+
((typeof EndUserUint8Array == 'undefined') ?
|
|
467
|
+
[] : new EndUserUint8Array(bytesLength)) :
|
|
468
|
+
new Uint8Array(bytesLength);
|
|
469
|
+
|
|
470
|
+
for (i = 0, j = 0, p = 0; i <
|
|
471
|
+
Math.floor((base64StrLength - equals) / 4); i++) {
|
|
472
|
+
encoded1 = this.m_map.indexOf(base64Str[j++]);
|
|
473
|
+
encoded2 = this.m_map.indexOf(base64Str[j++]);
|
|
474
|
+
encoded3 = this.m_map.indexOf(base64Str[j++]);
|
|
475
|
+
encoded4 = this.m_map.indexOf(base64Str[j++]);
|
|
476
|
+
|
|
477
|
+
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
|
|
478
|
+
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
|
|
479
|
+
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
switch (equals) {
|
|
483
|
+
case 2:
|
|
484
|
+
encoded1 = this.m_map.indexOf(base64Str[j]);
|
|
485
|
+
encoded2 = this.m_map.indexOf(base64Str[j + 1]);
|
|
486
|
+
|
|
487
|
+
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
|
|
488
|
+
break;
|
|
489
|
+
|
|
490
|
+
case 1:
|
|
491
|
+
encoded1 = this.m_map.indexOf(base64Str[j]);
|
|
492
|
+
encoded2 = this.m_map.indexOf(base64Str[j + 1]);
|
|
493
|
+
encoded3 = this.m_map.indexOf(base64Str[j + 2]);
|
|
494
|
+
|
|
495
|
+
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
|
|
496
|
+
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return bytes;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
//--------------------------------------------------------------------------------
|
|
505
|
+
|
|
506
|
+
export class EndUserUTF8Coder {
|
|
507
|
+
encode(str) {
|
|
508
|
+
var out = [], p = 0;
|
|
509
|
+
for (var i = 0; i < str.length; i++) {
|
|
510
|
+
var c = str.charCodeAt(i);
|
|
511
|
+
if (c < 128) {
|
|
512
|
+
out[p++] = c;
|
|
513
|
+
} else if (c < 2048) {
|
|
514
|
+
out[p++] = (c >> 6) | 192;
|
|
515
|
+
out[p++] = (c & 63) | 128;
|
|
516
|
+
} else {
|
|
517
|
+
out[p++] = (c >> 12) | 224;
|
|
518
|
+
out[p++] = ((c >> 6) & 63) | 128;
|
|
519
|
+
out[p++] = (c & 63) | 128;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return out;
|
|
523
|
+
}
|
|
524
|
+
decode(bytes) {
|
|
525
|
+
var out = [], pos = 0, c = 0;
|
|
526
|
+
var c1, c2, c3;
|
|
527
|
+
while (pos < bytes.length) {
|
|
528
|
+
c1 = bytes[pos++];
|
|
529
|
+
if (c1 < 128) {
|
|
530
|
+
out[c++] = String.fromCharCode(c1);
|
|
531
|
+
} else if (c1 > 191 && c1 < 224) {
|
|
532
|
+
c2 = bytes[pos++];
|
|
533
|
+
out[c++] = String.fromCharCode((c1 & 31) << 6 | c2 & 63);
|
|
534
|
+
} else {
|
|
535
|
+
c2 = bytes[pos++];
|
|
536
|
+
c3 = bytes[pos++];
|
|
537
|
+
out[c++] = String.fromCharCode(
|
|
538
|
+
(c1 & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
return out.join('');
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
//--------------------------------------------------------------------------------
|
|
546
|
+
|
|
547
|
+
export class EndUserUTF16LECoder {
|
|
548
|
+
encode(str) {
|
|
549
|
+
var out = [];
|
|
550
|
+
var c;
|
|
551
|
+
|
|
552
|
+
for (var i = 0; i < str.length; i++) {
|
|
553
|
+
c = str.charCodeAt(i);
|
|
554
|
+
out.push(c & 0xFF);
|
|
555
|
+
out.push((c & 0xFF00) >> 8);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
return out;
|
|
559
|
+
}
|
|
560
|
+
decode(bytes) {
|
|
561
|
+
var i = 0;
|
|
562
|
+
var ret = '';
|
|
563
|
+
var length;
|
|
564
|
+
|
|
565
|
+
if ((bytes.length % 2) != 0)
|
|
566
|
+
return null;
|
|
567
|
+
|
|
568
|
+
length = bytes.length;
|
|
569
|
+
if (length > 0 &&
|
|
570
|
+
bytes[length - 2] == 0 &&
|
|
571
|
+
bytes[length - 1] == 0) {
|
|
572
|
+
length -= 2;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
while (i < length) {
|
|
576
|
+
ret += String.fromCharCode(bytes[i] | (bytes[i + 1] << 8));
|
|
577
|
+
i += 2;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
return ret;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
//--------------------------------------------------------------------------------
|
|
585
|
+
|