Move generateRandomString() to subprocess
This commit is contained in:
parent
6a5b6af31f
commit
ece11954a1
1 changed files with 14 additions and 13 deletions
|
|
@ -2,9 +2,14 @@
|
||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
const { Buffer } = require('buffer')
|
const { Buffer } = require('buffer')
|
||||||
const Crypto = require('crypto')
|
const { fork } = require('child_process')
|
||||||
|
|
||||||
const FieldError = require('../fieldError')
|
const FieldError = require('../fieldError')
|
||||||
|
|
||||||
|
const { invoke } = require('./subprocess')
|
||||||
|
|
||||||
|
const cryptoSubprocess = fork('utils/ECC/subprocess')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {object} EncryptedMessageBuffer
|
* @typedef {object} EncryptedMessageBuffer
|
||||||
* @prop {Buffer} ciphertext
|
* @prop {Buffer} ciphertext
|
||||||
|
|
@ -23,19 +28,15 @@ const FieldError = require('../fieldError')
|
||||||
* @prop {any?} metadata
|
* @prop {any?} metadata
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const generateRandomString = (length = 16) =>
|
const generateRandomString = async (length = 16) => {
|
||||||
new Promise((resolve, reject) => {
|
if (length % 2 !== 0 || length < 2) {
|
||||||
// Gotta halve because randomBytes returns a sequence twice the size
|
throw new Error('Random string length must be an even number.')
|
||||||
Crypto.randomBytes(length / 2, (err, buffer) => {
|
|
||||||
if (err) {
|
|
||||||
reject(err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = buffer.toString('hex')
|
const res = await invoke('generateRandomString', [length], cryptoSubprocess)
|
||||||
resolve(token)
|
|
||||||
})
|
return res
|
||||||
})
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} value
|
* @param {string} value
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue