Electronjs and Nodejs(Crypto) crypto.scryptSync is not a function

穿精又带淫゛_ 提交于 2021-01-28 02:34:23

问题


Hi So I have a server running Crypto it working perfectly. Im using electronjs as a client side and crypto is suppose to be build into node. when I try use the module it returns "crypto.scryptSync is not a function"

I have

 let crypto = require('crypto');

at the top of the page

im just doing a simple call like this

Encrypt_AES

function Encrypt_AES(data, pubkey) {
 const algorithm = 'aes-192-cbc';
 const key = crypto.scryptSync(pubkey, 'salt', 24);
 const iv = Buffer.alloc(16, 0); // Initialization vector.
 const cipher = crypto.createCipheriv(algorithm, key, iv);
 let encrypted = cipher.update(JSON.stringify(data), 'utf8', 'hex');
 encrypted += cipher.final('hex');
 return encrypted;
}

Decrypt_AES

function Decrypt_AES(data, pubkey) {
 const algorithm = 'aes-192-cbc';
 const key = crypto.scryptSync(pubkey, 'salt', 24);
 const iv = Buffer.alloc(16, 0); // Initialization vector.
 const decipher = crypto.createDecipheriv(algorithm, key, iv);
 let decrypted = decipher.update(data, 'hex', 'utf8');
 decrypted += decipher.final('utf8');
 return JSON.parse(decrypted);
}

I dont understand how it cannot pick up the module in electron I've never had this problem with any other module.

I tried a npm - i crypto

npm WARN deprecated crypto@1.0.1: This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in. npm notice created a lockfile as package-lock.json. You should commit this file. + crypto@1.0.1 added 1 package and audited 724 packages in 4.95s found 0 vulnerabilities


回答1:


The Problem

That a Node function is not defined when using Electron or does not take a specific parameter even though it works fine with your local Node installation can happen, when the Node version that Electron supports is older than your local Node installation.

Find out the version you use

A good first start is to look when the function in question was added to Node. You can do that by looking in the Node.js documentation. You will find a History table right below the function name in a drop down block.

Here we see that scryptSync was added in Node 10.5.0.

If you don't know what Node version your Electron installation supports, you can run the following with Electron:

console.log(process.versions)

You should find the used Node version in the output. For example for Electron 3.1.4 it should look like this:

{ http_parser: '2.8.0',
  node: '10.2.0',
  v8: '6.6.346.32',
  uv: '1.20.3',
  zlib: '1.2.11',
  ares: '1.14.0',
  modules: '64',
  nghttp2: '1.29.0',
  napi: '3',
  openssl: '1.1.0h',
  electron: '3.1.4',
  chrome: '66.0.3359.181' }

For comparison here is the output of my Electron 4.0.2 installation:

{ http_parser: '2.8.0',
  node: '10.11.0',
  v8: '6.9.427.24-electron.0',
  uv: '1.23.0',
  zlib: '1.2.11',
  ares: '1.14.0',
  modules: '64',
  nghttp2: '1.33.0',
  napi: '3',
  openssl: '1.1.0',
  electron: '4.0.2',
  chrome: '69.0.3497.106',
  icu: '62.1',
  unicode: '11.0',
  cldr: '33.1',
  tz: '2018e' }

As we see Electron 3 uses Node 10.2.0. So in the Node that Electron uses scryptSync is not yet implemented and therefore undefined.

How to solve this

You can solve this if a newer Electron version that supports the Node version you need is already released.

You can look this up in the Electron release notes. Up until now Node version upgrades were implemented in major versions so looking at the next x.0.0 version is a good start.

In this case we are in luck: Electron 4 supports Node 10.11.0 which implements scryptSync.

So upgrading to the latest Electron version should solve this problem.




回答2:


I think for nodejs modules to work in electron you have to set up more than just requiring in, like if you were using the library in node.

This link appears to give you the information you need to use NPM/nodejs modules in electron:

Using Native Node Modules



来源:https://stackoverflow.com/questions/54738225/electronjs-and-nodejscrypto-crypto-scryptsync-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!