@ne1410s/crypto 0.2.42 → 0.2.44

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.
@@ -1,12 +1,12 @@
1
- /// <reference types="node" />
2
- import { IKeyPair_Jwk, ICsr_Params, ICsr_Result } from './interfaces';
3
- import { JsonWebKey } from 'node:crypto';
4
- export declare function randomString(): Promise<string>;
5
- export declare function gen(): Promise<IKeyPair_Jwk>;
6
- export declare function sign(text: string, privateJwk: JsonWebKey): Promise<string>;
7
- export declare function digest(text: string): Promise<string>;
8
- export declare function csr(params: ICsr_Params): Promise<ICsr_Result>;
9
- export declare function pfx(friendlyName: string, cert_b64: string, key_b64: string, password: string, hash?: string): Promise<ArrayBuffer>;
10
- export declare function pemToBase64Parts(pem: string): Array<string>;
11
- export declare function base64ToPem(base64: string, title: string): string;
12
- export declare function bufferToPem(pkcs10_buf: ArrayBuffer, title: string): string;
1
+ /// <reference types="node" />
2
+ import { IKeyPair_Jwk, ICsr_Params, ICsr_Result } from './interfaces';
3
+ import { JsonWebKey } from 'node:crypto';
4
+ export declare function randomString(): Promise<string>;
5
+ export declare function gen(): Promise<IKeyPair_Jwk>;
6
+ export declare function sign(text: string, privateJwk: JsonWebKey): Promise<string>;
7
+ export declare function digest(text: string): Promise<string>;
8
+ export declare function csr(params: ICsr_Params): Promise<ICsr_Result>;
9
+ export declare function pfx(friendlyName: string, cert_b64: string, key_b64: string, password: string, hash?: string): Promise<ArrayBuffer>;
10
+ export declare function pemToBase64Parts(pem: string): Array<string>;
11
+ export declare function base64ToPem(base64: string, title: string): string;
12
+ export declare function bufferToPem(pkcs10_buf: ArrayBuffer, title: string): string;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from './functions';
2
- export * from './interfaces';
1
+ export * from './functions';
2
+ export * from './interfaces';
@@ -1,20 +1,20 @@
1
- /// <reference types="node" />
2
- import { JsonWebKey } from 'node:crypto';
3
- export interface IKeyPair_Jwk {
4
- publicJwk: JsonWebKey;
5
- privateJwk: JsonWebKey;
6
- }
7
- export interface ICsr_Params {
8
- domains: Array<string>;
9
- country?: string;
10
- town?: string;
11
- county?: string;
12
- company?: string;
13
- department?: string;
14
- }
15
- export interface ICsr_Result extends IKeyPair_Jwk {
16
- der: string;
17
- pem: string;
18
- pkcs8_pem: string;
19
- pkcs8_b64: string;
20
- }
1
+ /// <reference types="node" />
2
+ import { JsonWebKey } from 'node:crypto';
3
+ export interface IKeyPair_Jwk {
4
+ publicJwk: JsonWebKey;
5
+ privateJwk: JsonWebKey;
6
+ }
7
+ export interface ICsr_Params {
8
+ domains: Array<string>;
9
+ country?: string;
10
+ town?: string;
11
+ county?: string;
12
+ company?: string;
13
+ department?: string;
14
+ }
15
+ export interface ICsr_Result extends IKeyPair_Jwk {
16
+ der: string;
17
+ pem: string;
18
+ pkcs8_pem: string;
19
+ pkcs8_b64: string;
20
+ }
@@ -76,276 +76,276 @@ function __generator(thisArg, body) {
76
76
  }
77
77
  }
78
78
 
79
- var crypto = require('node:crypto');
80
- var pkijs = require('pkijs');
81
- pkijs.setEngine('OpenSSL', crypto, new pkijs.CryptoEngine({ name: '', crypto: crypto, subtle: crypto.subtle }));
82
- var DEF_ALGO = {
83
- name: 'RSASSA-PKCS1-v1_5',
84
- modulusLength: 2048,
85
- publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
86
- hash: { name: 'SHA-256' },
87
- };
88
- function randomString() {
89
- return __awaiter(this, void 0, void 0, function () {
90
- var bytes;
91
- return __generator(this, function (_a) {
92
- bytes = new Uint8Array(24);
93
- crypto.getRandomValues(bytes);
94
- return [2 /*return*/, ne_text__namespace.bufferToBase64Url(bytes.buffer)];
95
- });
96
- });
97
- }
98
- function gen() {
99
- return __awaiter(this, void 0, void 0, function () {
100
- var keys;
101
- var _a;
102
- return __generator(this, function (_b) {
103
- switch (_b.label) {
104
- case 0: return [4 /*yield*/, crypto.subtle.generateKey(DEF_ALGO, true, ['sign'])];
105
- case 1:
106
- keys = _b.sent();
107
- _a = {};
108
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.publicKey)];
109
- case 2:
110
- _a.publicJwk = _b.sent();
111
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.privateKey)];
112
- case 3: return [2 /*return*/, (_a.privateJwk = _b.sent(),
113
- _a)];
114
- }
115
- });
116
- });
117
- }
118
- function sign(text, privateJwk) {
119
- return __awaiter(this, void 0, void 0, function () {
120
- var cKey, buffer, signed;
121
- return __generator(this, function (_a) {
122
- switch (_a.label) {
123
- case 0: return [4 /*yield*/, crypto.subtle.importKey('jwk', privateJwk, DEF_ALGO, true, ['sign'])];
124
- case 1:
125
- cKey = _a.sent(), buffer = ne_text__namespace.textToBuffer(text);
126
- return [4 /*yield*/, crypto.subtle.sign(DEF_ALGO.name, cKey, buffer)];
127
- case 2:
128
- signed = _a.sent();
129
- return [2 /*return*/, ne_text__namespace.bufferToBase64Url(signed)];
130
- }
131
- });
132
- });
133
- }
134
- function digest(text) {
135
- return __awaiter(this, void 0, void 0, function () {
136
- var buffer, digest;
137
- return __generator(this, function (_a) {
138
- switch (_a.label) {
139
- case 0:
140
- buffer = ne_text__namespace.textToBuffer(text);
141
- return [4 /*yield*/, crypto.subtle.digest('SHA-256', buffer)];
142
- case 1:
143
- digest = _a.sent();
144
- return [2 /*return*/, ne_text__namespace.bufferToBase64Url(digest)];
145
- }
146
- });
147
- });
148
- }
149
- //https://github.com/PeculiarVentures/PKI.js/blob/master/examples/PKCS10ComplexExample/es6.js
150
- function csr(params) {
151
- return __awaiter(this, void 0, void 0, function () {
152
- var pkcs10, keys, publicKey, toDigest, pubkeyhash_sha1, privateKey, pkcs10_schema, pkcs10_encoded, exportedPkcs8;
153
- var _a;
154
- return __generator(this, function (_b) {
155
- switch (_b.label) {
156
- case 0:
157
- pkcs10 = new pkijs.CertificationRequest();
158
- pkcs10.version = 0;
159
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
160
- type: '2.5.4.3',
161
- value: new asn1js__namespace.PrintableString({ value: params.domains[0] }),
162
- }));
163
- if (params.country) {
164
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
165
- type: '2.5.4.6',
166
- value: new asn1js__namespace.PrintableString({ value: params.country }),
167
- }));
168
- }
169
- if (params.town) {
170
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
171
- type: '2.5.4.7',
172
- value: new asn1js__namespace.PrintableString({ value: params.town }),
173
- }));
174
- }
175
- if (params.county) {
176
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
177
- type: '2.5.4.8',
178
- value: new asn1js__namespace.PrintableString({ value: params.county }),
179
- }));
180
- }
181
- if (params.company) {
182
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
183
- type: '2.5.4.10',
184
- value: new asn1js__namespace.PrintableString({ value: params.company }),
185
- }));
186
- }
187
- if (params.department) {
188
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
189
- type: '2.5.4.11',
190
- value: new asn1js__namespace.PrintableString({ value: params.department }),
191
- }));
192
- }
193
- return [4 /*yield*/, crypto.subtle.generateKey(DEF_ALGO, true, ['sign'])];
194
- case 1:
195
- keys = _b.sent();
196
- publicKey = keys.publicKey;
197
- return [4 /*yield*/, pkcs10.subjectPublicKeyInfo.importKey(publicKey)];
198
- case 2:
199
- _b.sent();
200
- toDigest = pkcs10.subjectPublicKeyInfo.subjectPublicKey.valueBlock.valueHex;
201
- return [4 /*yield*/, crypto.subtle.digest('SHA-1', toDigest)];
202
- case 3:
203
- pubkeyhash_sha1 = _b.sent();
204
- //pubkeyhash_sha256 = await crypto.subtle.digest('SHA-256', toDigest);
205
- pkcs10.attributes = [];
206
- pkcs10.attributes.push(new pkijs.Attribute({
207
- type: '1.2.840.113549.1.9.14',
208
- values: [
209
- new pkijs.Extensions({
210
- extensions: [
211
- new pkijs.Extension({
212
- extnID: '2.5.29.14',
213
- critical: false,
214
- extnValue: new asn1js__namespace.OctetString({ valueHex: pubkeyhash_sha1 }).toBER(false),
215
- }),
216
- new pkijs.Extension({
217
- extnID: '2.5.29.17',
218
- critical: false,
219
- extnValue: new pkijs.GeneralNames({
220
- names: params.domains.map(function (dom) {
221
- return new pkijs.GeneralName({
222
- type: 2,
223
- value: dom,
224
- });
225
- }),
226
- })
227
- .toSchema()
228
- .toBER(false),
229
- }),
230
- ],
231
- }).toSchema(),
232
- ],
233
- }));
234
- privateKey = keys.privateKey;
235
- return [4 /*yield*/, pkcs10.sign(privateKey, 'SHA-256')];
236
- case 4:
237
- _b.sent(), pkcs10_schema = pkcs10.toSchema(), pkcs10_encoded = pkcs10_schema.toBER(false);
238
- return [4 /*yield*/, crypto.subtle.exportKey('pkcs8', keys.privateKey)];
239
- case 5:
240
- exportedPkcs8 = _b.sent();
241
- _a = {
242
- pem: bufferToPem(pkcs10_encoded, 'CERTIFICATE REQUEST'),
243
- der: ne_text__namespace.bufferToBase64Url(pkcs10_encoded),
244
- pkcs8_pem: bufferToPem(exportedPkcs8, 'PRIVATE KEY'),
245
- pkcs8_b64: ne_text__namespace.bufferToBase64(exportedPkcs8)
246
- };
247
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.privateKey)];
248
- case 6:
249
- _a.privateJwk = _b.sent();
250
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.publicKey)];
251
- case 7: return [2 /*return*/, (_a.publicJwk = _b.sent(),
252
- _a)];
253
- }
254
- });
255
- });
256
- }
257
- //https://github.com/PeculiarVentures/PKI.js/blob/master/examples/PKCS12SimpleExample/es6.js
258
- function pfx(friendlyName, cert_b64, key_b64, password, hash) {
259
- if (hash === void 0) { hash = 'SHA-256'; }
260
- return __awaiter(this, void 0, void 0, function () {
261
- var asn1_cer, asn1_key, objc_cer, objc_key, pkcs12;
262
- return __generator(this, function (_a) {
263
- switch (_a.label) {
264
- case 0:
265
- asn1_cer = asn1js__namespace.fromBER(ne_text__namespace.textToBuffer(ne_text__namespace.base64ToText(cert_b64))), asn1_key = asn1js__namespace.fromBER(ne_text__namespace.textToBuffer(ne_text__namespace.base64ToText(key_b64)));
266
- objc_cer = new pkijs.Certificate({ schema: asn1_cer.result }), objc_key = new pkijs.PrivateKeyInfo({ schema: asn1_key.result });
267
- pkcs12 = new pkijs.PFX({
268
- parsedValue: {
269
- integrityMode: 0,
270
- authenticatedSafe: new pkijs.AuthenticatedSafe({
271
- parsedValue: {
272
- safeContents: [
273
- {
274
- privacyMode: 0,
275
- value: new pkijs.SafeContents({
276
- safeBags: [
277
- new pkijs.SafeBag({
278
- bagId: '1.2.840.113549.1.12.10.1.1',
279
- bagValue: objc_key,
280
- }),
281
- new pkijs.SafeBag({
282
- bagId: '1.2.840.113549.1.12.10.1.3',
283
- bagValue: new pkijs.CertBag({
284
- parsedValue: objc_cer,
285
- }),
286
- bagAttributes: [
287
- new pkijs.Attribute({
288
- type: '1.2.840.113549.1.9.20',
289
- values: [new asn1js__namespace.BmpString({ value: friendlyName })],
290
- }),
291
- ],
292
- }),
293
- ],
294
- }),
295
- },
296
- ],
297
- },
298
- }),
299
- },
300
- });
301
- return [4 /*yield*/, pkcs12.parsedValue.authenticatedSafe.makeInternalValues({
302
- safeContents: [
303
- {
304
- /* Empty as 'No Privacy' for SafeContents */
305
- },
306
- ],
307
- })];
308
- case 1:
309
- _a.sent();
310
- return [4 /*yield*/, pkcs12.makeInternalValues({
311
- password: ne_text__namespace.textToBuffer(password),
312
- iterations: 1000,
313
- pbkdf2HashAlgorithm: hash,
314
- hmacHashAlgorithm: hash,
315
- })];
316
- case 2:
317
- _a.sent();
318
- return [2 /*return*/, pkcs12.toSchema().toBER(false)];
319
- }
320
- });
321
- });
322
- }
323
- function pemToBase64Parts(pem) {
324
- return pem
325
- .split(/-----[\w\s]+-----/)
326
- .map(function (p) { return p.replace(/\s/g, ''); })
327
- .filter(function (p) { return p; });
328
- }
329
- function base64ToPem(base64, title) {
330
- var string_length = base64.length;
331
- var result_string = '';
332
- for (var i = 0, count = 0; i < string_length; i++, count++) {
333
- if (count > 63) {
334
- result_string += '\r\n';
335
- count = 0;
336
- }
337
- result_string += base64[i];
338
- }
339
- title =
340
- (title || '')
341
- .replace(/[^\w\s]+/, '')
342
- .trim()
343
- .toUpperCase() || 'CERTIFICATE';
344
- return "-----BEGIN ".concat(title, "-----\r\n").concat(result_string, "\r\n-----END ").concat(title, "-----\r\n");
345
- }
346
- function bufferToPem(pkcs10_buf, title) {
347
- var base64 = ne_text__namespace.bufferToBase64(pkcs10_buf);
348
- return base64ToPem(base64, title);
79
+ var crypto = require('node:crypto');
80
+ var pkijs = require('pkijs');
81
+ pkijs.setEngine('OpenSSL', crypto, new pkijs.CryptoEngine({ name: '', crypto: crypto, subtle: crypto.subtle }));
82
+ var DEF_ALGO = {
83
+ name: 'RSASSA-PKCS1-v1_5',
84
+ modulusLength: 2048,
85
+ publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
86
+ hash: { name: 'SHA-256' },
87
+ };
88
+ function randomString() {
89
+ return __awaiter(this, void 0, void 0, function () {
90
+ var bytes;
91
+ return __generator(this, function (_a) {
92
+ bytes = new Uint8Array(24);
93
+ crypto.getRandomValues(bytes);
94
+ return [2 /*return*/, ne_text__namespace.bufferToBase64Url(bytes.buffer)];
95
+ });
96
+ });
97
+ }
98
+ function gen() {
99
+ return __awaiter(this, void 0, void 0, function () {
100
+ var keys;
101
+ var _a;
102
+ return __generator(this, function (_b) {
103
+ switch (_b.label) {
104
+ case 0: return [4 /*yield*/, crypto.subtle.generateKey(DEF_ALGO, true, ['sign'])];
105
+ case 1:
106
+ keys = _b.sent();
107
+ _a = {};
108
+ return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.publicKey)];
109
+ case 2:
110
+ _a.publicJwk = _b.sent();
111
+ return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.privateKey)];
112
+ case 3: return [2 /*return*/, (_a.privateJwk = _b.sent(),
113
+ _a)];
114
+ }
115
+ });
116
+ });
117
+ }
118
+ function sign(text, privateJwk) {
119
+ return __awaiter(this, void 0, void 0, function () {
120
+ var cKey, buffer, signed;
121
+ return __generator(this, function (_a) {
122
+ switch (_a.label) {
123
+ case 0: return [4 /*yield*/, crypto.subtle.importKey('jwk', privateJwk, DEF_ALGO, true, ['sign'])];
124
+ case 1:
125
+ cKey = _a.sent(), buffer = ne_text__namespace.textToBuffer(text);
126
+ return [4 /*yield*/, crypto.subtle.sign(DEF_ALGO.name, cKey, buffer)];
127
+ case 2:
128
+ signed = _a.sent();
129
+ return [2 /*return*/, ne_text__namespace.bufferToBase64Url(signed)];
130
+ }
131
+ });
132
+ });
133
+ }
134
+ function digest(text) {
135
+ return __awaiter(this, void 0, void 0, function () {
136
+ var buffer, digest;
137
+ return __generator(this, function (_a) {
138
+ switch (_a.label) {
139
+ case 0:
140
+ buffer = ne_text__namespace.textToBuffer(text);
141
+ return [4 /*yield*/, crypto.subtle.digest('SHA-256', buffer)];
142
+ case 1:
143
+ digest = _a.sent();
144
+ return [2 /*return*/, ne_text__namespace.bufferToBase64Url(digest)];
145
+ }
146
+ });
147
+ });
148
+ }
149
+ //https://github.com/PeculiarVentures/PKI.js/blob/master/examples/PKCS10ComplexExample/es6.js
150
+ function csr(params) {
151
+ return __awaiter(this, void 0, void 0, function () {
152
+ var pkcs10, keys, publicKey, toDigest, pubkeyhash_sha1, privateKey, pkcs10_schema, pkcs10_encoded, exportedPkcs8;
153
+ var _a;
154
+ return __generator(this, function (_b) {
155
+ switch (_b.label) {
156
+ case 0:
157
+ pkcs10 = new pkijs.CertificationRequest();
158
+ pkcs10.version = 0;
159
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
160
+ type: '2.5.4.3',
161
+ value: new asn1js__namespace.PrintableString({ value: params.domains[0] }),
162
+ }));
163
+ if (params.country) {
164
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
165
+ type: '2.5.4.6',
166
+ value: new asn1js__namespace.PrintableString({ value: params.country }),
167
+ }));
168
+ }
169
+ if (params.town) {
170
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
171
+ type: '2.5.4.7',
172
+ value: new asn1js__namespace.PrintableString({ value: params.town }),
173
+ }));
174
+ }
175
+ if (params.county) {
176
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
177
+ type: '2.5.4.8',
178
+ value: new asn1js__namespace.PrintableString({ value: params.county }),
179
+ }));
180
+ }
181
+ if (params.company) {
182
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
183
+ type: '2.5.4.10',
184
+ value: new asn1js__namespace.PrintableString({ value: params.company }),
185
+ }));
186
+ }
187
+ if (params.department) {
188
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
189
+ type: '2.5.4.11',
190
+ value: new asn1js__namespace.PrintableString({ value: params.department }),
191
+ }));
192
+ }
193
+ return [4 /*yield*/, crypto.subtle.generateKey(DEF_ALGO, true, ['sign'])];
194
+ case 1:
195
+ keys = _b.sent();
196
+ publicKey = keys.publicKey;
197
+ return [4 /*yield*/, pkcs10.subjectPublicKeyInfo.importKey(publicKey)];
198
+ case 2:
199
+ _b.sent();
200
+ toDigest = pkcs10.subjectPublicKeyInfo.subjectPublicKey.valueBlock.valueHex;
201
+ return [4 /*yield*/, crypto.subtle.digest('SHA-1', toDigest)];
202
+ case 3:
203
+ pubkeyhash_sha1 = _b.sent();
204
+ //pubkeyhash_sha256 = await crypto.subtle.digest('SHA-256', toDigest);
205
+ pkcs10.attributes = [];
206
+ pkcs10.attributes.push(new pkijs.Attribute({
207
+ type: '1.2.840.113549.1.9.14',
208
+ values: [
209
+ new pkijs.Extensions({
210
+ extensions: [
211
+ new pkijs.Extension({
212
+ extnID: '2.5.29.14',
213
+ critical: false,
214
+ extnValue: new asn1js__namespace.OctetString({ valueHex: pubkeyhash_sha1 }).toBER(false),
215
+ }),
216
+ new pkijs.Extension({
217
+ extnID: '2.5.29.17',
218
+ critical: false,
219
+ extnValue: new pkijs.GeneralNames({
220
+ names: params.domains.map(function (dom) {
221
+ return new pkijs.GeneralName({
222
+ type: 2,
223
+ value: dom,
224
+ });
225
+ }),
226
+ })
227
+ .toSchema()
228
+ .toBER(false),
229
+ }),
230
+ ],
231
+ }).toSchema(),
232
+ ],
233
+ }));
234
+ privateKey = keys.privateKey;
235
+ return [4 /*yield*/, pkcs10.sign(privateKey, 'SHA-256')];
236
+ case 4:
237
+ _b.sent(), pkcs10_schema = pkcs10.toSchema(), pkcs10_encoded = pkcs10_schema.toBER(false);
238
+ return [4 /*yield*/, crypto.subtle.exportKey('pkcs8', keys.privateKey)];
239
+ case 5:
240
+ exportedPkcs8 = _b.sent();
241
+ _a = {
242
+ pem: bufferToPem(pkcs10_encoded, 'CERTIFICATE REQUEST'),
243
+ der: ne_text__namespace.bufferToBase64Url(pkcs10_encoded),
244
+ pkcs8_pem: bufferToPem(exportedPkcs8, 'PRIVATE KEY'),
245
+ pkcs8_b64: ne_text__namespace.bufferToBase64(exportedPkcs8)
246
+ };
247
+ return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.privateKey)];
248
+ case 6:
249
+ _a.privateJwk = _b.sent();
250
+ return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.publicKey)];
251
+ case 7: return [2 /*return*/, (_a.publicJwk = _b.sent(),
252
+ _a)];
253
+ }
254
+ });
255
+ });
256
+ }
257
+ //https://github.com/PeculiarVentures/PKI.js/blob/master/examples/PKCS12SimpleExample/es6.js
258
+ function pfx(friendlyName, cert_b64, key_b64, password, hash) {
259
+ if (hash === void 0) { hash = 'SHA-256'; }
260
+ return __awaiter(this, void 0, void 0, function () {
261
+ var asn1_cer, asn1_key, objc_cer, objc_key, pkcs12;
262
+ return __generator(this, function (_a) {
263
+ switch (_a.label) {
264
+ case 0:
265
+ asn1_cer = asn1js__namespace.fromBER(ne_text__namespace.textToBuffer(ne_text__namespace.base64ToText(cert_b64))), asn1_key = asn1js__namespace.fromBER(ne_text__namespace.textToBuffer(ne_text__namespace.base64ToText(key_b64)));
266
+ objc_cer = new pkijs.Certificate({ schema: asn1_cer.result }), objc_key = new pkijs.PrivateKeyInfo({ schema: asn1_key.result });
267
+ pkcs12 = new pkijs.PFX({
268
+ parsedValue: {
269
+ integrityMode: 0,
270
+ authenticatedSafe: new pkijs.AuthenticatedSafe({
271
+ parsedValue: {
272
+ safeContents: [
273
+ {
274
+ privacyMode: 0,
275
+ value: new pkijs.SafeContents({
276
+ safeBags: [
277
+ new pkijs.SafeBag({
278
+ bagId: '1.2.840.113549.1.12.10.1.1',
279
+ bagValue: objc_key,
280
+ }),
281
+ new pkijs.SafeBag({
282
+ bagId: '1.2.840.113549.1.12.10.1.3',
283
+ bagValue: new pkijs.CertBag({
284
+ parsedValue: objc_cer,
285
+ }),
286
+ bagAttributes: [
287
+ new pkijs.Attribute({
288
+ type: '1.2.840.113549.1.9.20',
289
+ values: [new asn1js__namespace.BmpString({ value: friendlyName })],
290
+ }),
291
+ ],
292
+ }),
293
+ ],
294
+ }),
295
+ },
296
+ ],
297
+ },
298
+ }),
299
+ },
300
+ });
301
+ return [4 /*yield*/, pkcs12.parsedValue.authenticatedSafe.makeInternalValues({
302
+ safeContents: [
303
+ {
304
+ /* Empty as 'No Privacy' for SafeContents */
305
+ },
306
+ ],
307
+ })];
308
+ case 1:
309
+ _a.sent();
310
+ return [4 /*yield*/, pkcs12.makeInternalValues({
311
+ password: ne_text__namespace.textToBuffer(password),
312
+ iterations: 1000,
313
+ pbkdf2HashAlgorithm: hash,
314
+ hmacHashAlgorithm: hash,
315
+ })];
316
+ case 2:
317
+ _a.sent();
318
+ return [2 /*return*/, pkcs12.toSchema().toBER(false)];
319
+ }
320
+ });
321
+ });
322
+ }
323
+ function pemToBase64Parts(pem) {
324
+ return pem
325
+ .split(/-----[\w\s]+-----/)
326
+ .map(function (p) { return p.replace(/\s/g, ''); })
327
+ .filter(function (p) { return p; });
328
+ }
329
+ function base64ToPem(base64, title) {
330
+ var string_length = base64.length;
331
+ var result_string = '';
332
+ for (var i = 0, count = 0; i < string_length; i++, count++) {
333
+ if (count > 63) {
334
+ result_string += '\r\n';
335
+ count = 0;
336
+ }
337
+ result_string += base64[i];
338
+ }
339
+ title =
340
+ (title || '')
341
+ .replace(/[^\w\s]+/, '')
342
+ .trim()
343
+ .toUpperCase() || 'CERTIFICATE';
344
+ return "-----BEGIN ".concat(title, "-----\r\n").concat(result_string, "\r\n-----END ").concat(title, "-----\r\n");
345
+ }
346
+ function bufferToPem(pkcs10_buf, title) {
347
+ var base64 = ne_text__namespace.bufferToBase64(pkcs10_buf);
348
+ return base64ToPem(base64, title);
349
349
  }
350
350
 
351
351
  exports.base64ToPem = base64ToPem;