aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/core-utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/core-utils.ts')
-rw-r--r--server/helpers/core-utils.ts56
1 files changed, 50 insertions, 6 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts
index 4bbf0228d..c762f6a29 100644
--- a/server/helpers/core-utils.ts
+++ b/server/helpers/core-utils.ts
@@ -6,9 +6,8 @@
6*/ 6*/
7 7
8import { exec, ExecOptions } from 'child_process' 8import { exec, ExecOptions } from 'child_process'
9import { randomBytes } from 'crypto' 9import { ED25519KeyPairOptions, generateKeyPair, randomBytes, RSAKeyPairOptions } from 'crypto'
10import { truncate } from 'lodash' 10import { truncate } from 'lodash'
11import { createPrivateKey as createPrivateKey_1, getPublicKey as getPublicKey_1 } from 'pem'
12import { pipeline } from 'stream' 11import { pipeline } from 'stream'
13import { URL } from 'url' 12import { URL } from 'url'
14import { promisify } from 'util' 13import { promisify } from 'util'
@@ -242,6 +241,51 @@ function toEven (num: number) {
242 241
243// --------------------------------------------------------------------------- 242// ---------------------------------------------------------------------------
244 243
244function generateRSAKeyPairPromise (size: number) {
245 return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => {
246 const options: RSAKeyPairOptions<'pem', 'pem'> = {
247 modulusLength: size,
248 publicKeyEncoding: {
249 type: 'spki',
250 format: 'pem'
251 },
252 privateKeyEncoding: {
253 type: 'pkcs1',
254 format: 'pem'
255 }
256 }
257
258 generateKeyPair('rsa', options, (err, publicKey, privateKey) => {
259 if (err) return rej(err)
260
261 return res({ publicKey, privateKey })
262 })
263 })
264}
265
266function generateED25519KeyPairPromise () {
267 return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => {
268 const options: ED25519KeyPairOptions<'pem', 'pem'> = {
269 publicKeyEncoding: {
270 type: 'spki',
271 format: 'pem'
272 },
273 privateKeyEncoding: {
274 type: 'pkcs8',
275 format: 'pem'
276 }
277 }
278
279 generateKeyPair('ed25519', options, (err, publicKey, privateKey) => {
280 if (err) return rej(err)
281
282 return res({ publicKey, privateKey })
283 })
284 })
285}
286
287// ---------------------------------------------------------------------------
288
245function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { 289function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
246 return function promisified (): Promise<A> { 290 return function promisified (): Promise<A> {
247 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { 291 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
@@ -268,8 +312,6 @@ function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A)
268} 312}
269 313
270const randomBytesPromise = promisify1<number, Buffer>(randomBytes) 314const randomBytesPromise = promisify1<number, Buffer>(randomBytes)
271const createPrivateKey = promisify1<number, { key: string }>(createPrivateKey_1)
272const getPublicKey = promisify1<string, { publicKey: string }>(getPublicKey_1)
273const execPromise2 = promisify2<string, any, string>(exec) 315const execPromise2 = promisify2<string, any, string>(exec)
274const execPromise = promisify1<string, string>(exec) 316const execPromise = promisify1<string, string>(exec)
275const pipelinePromise = promisify(pipeline) 317const pipelinePromise = promisify(pipeline)
@@ -298,8 +340,10 @@ export {
298 promisify2, 340 promisify2,
299 341
300 randomBytesPromise, 342 randomBytesPromise,
301 createPrivateKey, 343
302 getPublicKey, 344 generateRSAKeyPairPromise,
345 generateED25519KeyPairPromise,
346
303 execPromise2, 347 execPromise2,
304 execPromise, 348 execPromise,
305 pipelinePromise, 349 pipelinePromise,