在我的 Electron 应用程序中,我通过以下方法创建 Diffie-Hellman key :
const crypto = require('crypto');
/**
* Generate the keys and the diffie hellman key agreement object.
* @param {Integer} p The prime for Diffie Hellman Key Generation
* @param {Integer} g The generator for Diffie Hellman Key Exchange
*/
async function createSelfKey(p, g, callback) {
let returnVal = null;
if (p && g) {
returnVal = { dh: await crypto.createDiffieHellman(p, g) };
} else {
returnVal = { dh: await crypto.createDiffieHellman(2048) };
}
returnVal.keys = await returnVal.dh.generateKeys();
return callback(returnVal);
};
但是 key 生成是一个计算量较大的过程,因此它使我的应用程序卡住。一个使用示例是当我尝试从以下函数实现此方法 generateCreatorKeys
时:
function ChatRoomStatus() {
/**
* @var {Object}
*/
const chatrooms = {};
// Some other logic
/**
* This Method fetched the creator of the Chatroom and executes a callback on it.
* @param {String} chatroom The chatroom to fetch the creator
* @param {Function} callback The callback of the chatroom.
*/
this.processCreator = (chatroom, callback) => {
const index = _.findIndex(chatrooms[chatroom].friends, (friend) => friend.creator);
return callback(chatrooms[chatroom].friends[index], index , chatrooms[chatroom] );
};
/**
* Generate keys for the Chatroom Creator:
* @param {String} chatroom The chatroom to fetch the creator
* @param {Function} callback The callback of the chatroom.
*/
this.generateCreatorKeys = (chatroom, callback) => {
return this.processCreator(chatroom, (friend, index, chatroom) => {
return createSelfKey(null, null, (cryptoValues) => {
friend.encryption = cryptoValues;
return callback(friend, index, chatroom);
});
});
};
};
调用此方法的示例是:
const { xml, jid } = require('@xmpp/client');
/**
* Handling the message Exchange for group Key agreement
* @param {Function} sendMessageCallback
* @param {ChatRoomStatus} ChatroomWithParticipants
*/
function GroupKeyAgreement(sendMessageCallback, ChatroomWithParticipants) {
const self = this;
/**
* Send the Owner participant Keys into the Chatroom
*/
self.sendSelfKeys = (chatroomJid, chatroomName) => {
ChatroomWithParticipants.generateCreatorKeys(chatroomName, (creator) => {
const message = xml('message', { to: jid(chatroomJid).bare().toString()+"/"+creator.nick });
const extention = xml('x', { xmlns: 'http://pcmagas.tk/gkePlusp#intiator_key' });
extention.append(xml('p', {}, creator.encryption.dh.getPrime().toString('hex')));
extention.append(xml('g', {}, creator.encryption.dh.getGenerator().toString('hex')));
extention.append(xml('pubKey', {}, creator.encryption.keys.toString('hex')));
message.append(extention);
sendMessageCallback(message);
});
};
};
module.exports = GroupKeyAgreement;
你知道我如何在并行/单独线程中“运行”函数createSelfKey
并通过回调提供其内容吗?此外,上面的代码在 Electron 的主进程上运行,因此卡住它会导致整个应用程序停滞一段时间。
最佳答案
我会看一下https://electronjs.org/docs/tutorial/multithreading 。
Electron 基本上拥有 DOM 和 Node.js 的所有内容以及更多内容,因此您有一些选择。一般来说,它们是:
- Web 工作线程(仅限渲染器进程)。如果您在渲染器进程中执行此操作,则可以仅使用普通 DOM Web Worker。它们在单独的进程或线程中运行(不确定是哪个,这是 chromium 实现细节,但它绝对不会阻塞您的 UI)。
- 看起来node.js的worker_threads(仅渲染器进程?)现在也可以在Electron中使用。这可能也有用,但我个人从未使用过这些。
- 您始终可以创建另一个渲染器进程并将其用作单独的“线程”并通过 IPC 与其进行通信。工作完成后,您只需关闭它即可。您可以通过创建一个新的隐藏浏览器窗口来完成此操作。
- 使用node.js的cluster/child_process模块启动一个新的 Node 进程,并使用它的内置IPC(不是Electron的)与其通信。
因为您在主进程中运行此代码并假设您无法将其移出,所以您唯一的选择(据我所知)是#3。如果您同意添加库, Electron 远程(https://github.com/electron-userland/electron-remote#the-renderer-taskpool)有一些很酷的功能,可以让您在后台启动一个(或多个)渲染器进程,以 promise 的方式获取结果,然后为您关闭它们。
关于node.js - Electron 在单独的线程上运行加密 Diffie Hellman key 生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54030043/