node.jsbrowserbufferpublic-key-encryptionnode-rsa

Buffer library in browser does not return the same value as in nodejs


When running the Buffer.from static method in node js on a public key I get different console.log when running it from the browser (in an angular project). Should'nt they be the same? Is there something I'm doing wrong?

const pubKey='30819F300D06092A864886F70D010101050003818D0030818902818100B2E9AFAFEED76A5C31D069F84328D785DFE6C40A69F51B29C7D7C91EF171A5EF6AD9FC30AF31F4B59C0FE317E47B5DBAA04E3753AC7F8B0E54D8EB4372894900DE247FD11B8C2208FE1C837ADEC409B0F2EE89A5C54B8AB80D5934FC65100406077D129DC5EB961E883B937C4251FDA4BD77224D1CDEF09151894F902758AA3B0203010001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';

const buff = Buffer.from(pubKey, 'hex');

console.log(buff)
<Buffer 30 81 9f 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 03 81 8d 00 30 81 89 02 81 81 00 b2 e9 af af ee d7 6a 5c 31 d0 69 f8 43 28 d7 85 df e6 c4 0a 69 ... 244 more bytes>

In angular I have installed npm i buffer and provided it in the pollyfills.ts like this

(window as any).global = window;
(window as any).global.Buffer = require('buffer').Buffer;
(window as any).process = {};

Running the same code on the browser Stackblitz yields

enter image description here

So my questions is

Why does the Buffer in the browser return something different? [0] = 48 vs [0]= 30

In the Stackblitz example you can see that I'm using the buffer with node-rsa for encyrption. The encrypted value from the nodesjs script works while the one from the browser doesnt.


Solution

  • The data are the same, just two different representation:

    console.log(buff)
    <Buffer 30 81 9f 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 03 81 8d 00 30 81 89 02 81 81 00 b2 e9 af af ee d7 6a 5c 31 d0 69 f8 43 28 d7 85 df e6 c4 0a 69 ... >
    undefined
    console.log(buff.toJSON())
    { type: 'Buffer',
      data:
       [ 48,
         129,