-
Notifications
You must be signed in to change notification settings - Fork 15
Using with Electron #10
Description
Electron and crypto-async
I can't get this lib to work with Electron. I made a basic project reproducing my problem here
Bug Description
I can't use the module without recompiling it because, the following code crashes when executed with electron but works perfectly fine when executed on node directly.
const cryptoAsync = require('@ronomon/crypto-async')
try {
const text = Buffer.from('Hello World')
const key = Buffer.alloc(16)
const iv = Buffer.alloc(16)
console.log('This prints to the console')
const encryptedText = cryptoAsync.cipher('aes-128-ctr', 0, key, iv, text)
console.log('But this will not print if the previous call crashed')
const clearText = cryptoAsync.cipher('aes-128-ctr', 1, key, iv, encryptedText)
console.log(clearText.toString())
} catch (e) {
console.error(e)
}I've tested on both Windows (Windows 10) and Mac OS (El Capitan) and observed two different behaviors (I'm guessing cause MacOS uses the system openssl)
- Windows
- It crashes completly with the following exit code
npm ERR! code ELIFECYCLE npm ERR! errno 3221225477 npm ERR! electron-quick-start@1.0.0 start-electron: `electron .` npm ERR! Exit status 3221225477
- It crashes completly with the following exit code
- Mac OS
- The first
cryptoAsync.ciphermethod call throws an exceptionError: algorithm unknown
- The first
How to reproduce
- Clone this repo
- Run
npm i - Run
npm run start-electronto execute with electron - Run
npm run start-nodeto execute with node
Causes
As far as I can tell both errors seems to originate from the same line in binding.c in the @ronomon/crypto-async repo :
const EVP_CIPHER* evp_cipher = EVP_get_cipherbyname(algorithm);
if (!evp_cipher) {
THROW(env, E_ALGORITHM_UNKNOWN);
}On windows the EVP_get_cipherbyname method crashes, on Mac it returns null
This seems to be the result of the differences with the bundled version of node in electron.
Rebuilding for Electron
I've also tried to rebuild the native module for electron using the following commands
cd node_modules
cd @ronomon/crypto-async
node-gyp rebuild --target=8.1.0 --arch=x64 --target_platform=win32 --dist-url=https://atom.io/download/atom-shell --module_name=@ronomon/crypto-async --module_path=../lib/binding/electron-v8.1-win32-x64
But the build fails cause openssl/err.h and others openssl headers file cannot be found.
It seems that electron now uses boringssl instead of openssl and does not export the same headers/symbols that Node does.
- Addons cannot use the bundled OpenSSL electron/electron#13176
- Include openssl headers into the headers tarball electron/electron#11801
From the different issues I've come accross on github, I can only think of two solutions (let me know If you can think of any other workaround):
-
Wait until electron exports those symbols/headers and do this in the meantime
-
Try to bundle openssl/boringssl with the module