Hello,
I have the following code working in an Ionic v3 app.
It takes a public key and a password and encrypts it.
import NodeRSA from 'node-rsa';
import Buffer from 'buffer';
let keyBuf = new Buffer(key, 'hex');
let rsa = new NodeRSA(keyBuf.slice(22), 'pkcs1-public-der', {
encryptionScheme: 'pkcs1'
});
console.log("encrypted password: " + rsa.encrypt(password, 'hex'));
Now, I am building an Ionic 4 app and trying to use the same code.
I have been unsuccessful so far.
There seems to be all sort of issues with Buffer, or I am getting into all sorts of issues with it due to the troubleshooting I am doing.
Errors may be due to new Buffer being deprecated.
I’m also getting:
TypeError: Buffer is not a constructor
Buffer is not a constructor
process is undefined
Some of them I fixed by adding this to index.html:
<script>
var global = global || window;
var Buffer = Buffer || [];
var process = process || {
env: { DEBUG: undefined },
version: []
};
</script>
and this to pollyfils.ts
(window as any).process.version = '';
(window as any).process.versions = {node: false};
I am getting into similar issues with Buffer when I use crypto.
I switched the new Buffer() into Buffer.from as recommended in NodeJS, but I see a lot of code under node_modules still uses new Buffer().
Can someone tell me how are you using encryption with an Ionic Capacitor app, if possible?
Thanks!