*/
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') {
// ---------------------------------------------------------------------------
-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<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
- return function promisified (): Promise<A> {
- return new Promise<A>((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<T, A> (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise<A> {
- return function promisified (arg: T): Promise<A> {
- return new Promise<A>((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<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise<A> {
- return function promisified (arg1: T, arg2: U): Promise<A> {
- return new Promise<A>((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<number, Buffer>(randomBytes)
-const createPrivateKey = promisify1<number, { key: string }>(createPrivateKey_1)
-const getPublicKey = promisify1<string, { publicKey: string }>(getPublicKey_1)
+const scryptPromise = promisify3<string, string, number, Buffer>(scrypt)
const execPromise2 = promisify2<string, any, string>(exec)
const execPromise = promisify1<string, string>(exec)
const pipelinePromise = promisify(pipeline)
pageToStartAndCount,
peertubeTruncate,
- promisify0,
- promisify1,
- promisify2,
+ scryptPromise,
randomBytesPromise,
- createPrivateKey,
- getPublicKey,
+
+ generateRSAKeyPairPromise,
+ generateED25519KeyPairPromise,
+
execPromise2,
execPromise,
pipelinePromise,
- parseSemVersion,
-
- isOdd,
- toEven
+ parseSemVersion
}