X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcore-utils.ts;h=242c49e896090249c3e1d6cda61a5e05ba2cd519;hb=HEAD;hp=4bbf0228d6369e558453f6b69d8f2511fb88f416;hpb=9452d4fd3321148fb80b64a67bd9983fee6c208e;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 4bbf0228d..242c49e89 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts @@ -6,12 +6,12 @@ */ import { exec, ExecOptions } from 'child_process' -import { randomBytes } from 'crypto' +import { ED25519KeyPairOptions, generateKeyPair, randomBytes, RSAKeyPairOptions, scrypt } from 'crypto' import { truncate } from 'lodash' -import { createPrivateKey as createPrivateKey_1, getPublicKey as getPublicKey_1 } from 'pem' import { pipeline } from 'stream' import { URL } from 'url' import { promisify } from 'util' +import { promisify1, promisify2, promisify3 } from '@shared/core-utils' const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { if (!oldObject || typeof oldObject !== 'object') { @@ -230,46 +230,53 @@ function execShell (command: string, options?: ExecOptions) { // --------------------------------------------------------------------------- -function isOdd (num: number) { - return (num % 2) !== 0 -} +function generateRSAKeyPairPromise (size: number) { + return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => { + const options: RSAKeyPairOptions<'pem', 'pem'> = { + modulusLength: size, + publicKeyEncoding: { + type: 'spki', + format: 'pem' + }, + privateKeyEncoding: { + type: 'pkcs1', + format: 'pem' + } + } -function toEven (num: number) { - if (isOdd(num)) return num + 1 + generateKeyPair('rsa', options, (err, publicKey, privateKey) => { + if (err) return rej(err) - return num + return res({ publicKey, privateKey }) + }) + }) } -// --------------------------------------------------------------------------- +function generateED25519KeyPairPromise () { + return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => { + const options: ED25519KeyPairOptions<'pem', 'pem'> = { + publicKeyEncoding: { + type: 'spki', + format: 'pem' + }, + privateKeyEncoding: { + type: 'pkcs8', + format: 'pem' + } + } -function promisify0 (func: (cb: (err: any, result: A) => void) => void): () => Promise { - return function promisified (): Promise { - return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { - func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ]) - }) - } -} + generateKeyPair('ed25519', options, (err, publicKey, privateKey) => { + if (err) return rej(err) -// Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2 -function promisify1 (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise { - return function promisified (arg: T): Promise { - return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { - func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ]) + return res({ publicKey, privateKey }) }) - } + }) } -function promisify2 (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise { - return function promisified (arg1: T, arg2: U): Promise { - return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { - func.apply(null, [ arg1, arg2, (err: any, res: A) => err ? reject(err) : resolve(res) ]) - }) - } -} +// --------------------------------------------------------------------------- const randomBytesPromise = promisify1(randomBytes) -const createPrivateKey = promisify1(createPrivateKey_1) -const getPublicKey = promisify1(getPublicKey_1) +const scryptPromise = promisify3(scrypt) const execPromise2 = promisify2(exec) const execPromise = promisify1(exec) const pipelinePromise = promisify(pipeline) @@ -293,19 +300,16 @@ export { pageToStartAndCount, peertubeTruncate, - promisify0, - promisify1, - promisify2, + scryptPromise, randomBytesPromise, - createPrivateKey, - getPublicKey, + + generateRSAKeyPairPromise, + generateED25519KeyPairPromise, + execPromise2, execPromise, pipelinePromise, - parseSemVersion, - - isOdd, - toEven + parseSemVersion }