diff options
Diffstat (limited to 'server/helpers/core-utils.ts')
-rw-r--r-- | server/helpers/core-utils.ts | 56 |
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 | ||
8 | import { exec, ExecOptions } from 'child_process' | 8 | import { exec, ExecOptions } from 'child_process' |
9 | import { randomBytes } from 'crypto' | 9 | import { ED25519KeyPairOptions, generateKeyPair, randomBytes, RSAKeyPairOptions } from 'crypto' |
10 | import { truncate } from 'lodash' | 10 | import { truncate } from 'lodash' |
11 | import { createPrivateKey as createPrivateKey_1, getPublicKey as getPublicKey_1 } from 'pem' | ||
12 | import { pipeline } from 'stream' | 11 | import { pipeline } from 'stream' |
13 | import { URL } from 'url' | 12 | import { URL } from 'url' |
14 | import { promisify } from 'util' | 13 | import { promisify } from 'util' |
@@ -242,6 +241,51 @@ function toEven (num: number) { | |||
242 | 241 | ||
243 | // --------------------------------------------------------------------------- | 242 | // --------------------------------------------------------------------------- |
244 | 243 | ||
244 | function 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 | |||
266 | function 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 | |||
245 | function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { | 289 | function 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 | ||
270 | const randomBytesPromise = promisify1<number, Buffer>(randomBytes) | 314 | const randomBytesPromise = promisify1<number, Buffer>(randomBytes) |
271 | const createPrivateKey = promisify1<number, { key: string }>(createPrivateKey_1) | ||
272 | const getPublicKey = promisify1<string, { publicKey: string }>(getPublicKey_1) | ||
273 | const execPromise2 = promisify2<string, any, string>(exec) | 315 | const execPromise2 = promisify2<string, any, string>(exec) |
274 | const execPromise = promisify1<string, string>(exec) | 316 | const execPromise = promisify1<string, string>(exec) |
275 | const pipelinePromise = promisify(pipeline) | 317 | const 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, |