0

we have been using xxtea-node npm package for encryption and decryption, as we can see the plugin is depended on Buffer node package, but during the decryption process, it throws Undefined error as below.

Environment :

Node : V14.17.3,

NPM : 6.14.13,

Encryption plugin : xxtea-nodejs

Decyption method which throwing error :

function decrypt(data, key) {
    if (typeof data === 'string') data = new Buffer(data, 'base64'); //This line throws Undefined error
    if (typeof key === 'string') key = toBytes(key);
    if (data === undefined || data === null || data.length === 0) {
        return data;
    }
    return toUint8Array(decryptUint32Array(toUint32Array(data, false), toUint32Array(fixk(key), false)), true);
}

In the Decrypt method new Buffer line is throwing "ReferenceError: Buffer is not defined", but if I replace this line with the following line it works fine require('buffer/').Buffer.from()

Whats the difference between both and how to fix the first said issue?

Saravana
  • 524
  • 1
  • 6
  • 28
  • did you run the nodejs code outside nodejs environment (e.g. browser)? did you use [tag:webpack]? its kinda difficult to work out an answer given the snippet is not a [mcve] - its an excerpt from the [xxtea library source](https://github.com/xxtea/xxtea-nodejs/blob/master/lib/xxtea.js#L304). – Bagus Tesa Dec 27 '22 at 04:42
  • Yes, This is application is run on the browser. and we are not using webapack for bundling. – Saravana Dec 27 '22 at 05:38
  • try slapping in [node-stdlib-browser](https://github.com/niksy/node-stdlib-browser), see if it polyfill your nodejs api. i'm not exactly encouraging running nodejs code on browser though.. – Bagus Tesa Dec 27 '22 at 05:49
  • Thanks for your suggestions and help. Instead, I tried to add this in the polyfills and it seems to be working `(window as any).global = window; (window as any).global.Buffer = require('buffer').Buffer;` – Saravana Dec 27 '22 at 06:03
  • not sure why did you use `xxtea-node` on browser though. i just found out a [pure js `xxtea` exists](https://www.npmjs.com/package/xxtea). – Bagus Tesa Dec 27 '22 at 06:05

0 Answers0