1

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.

Han Che
  • 8,239
  • 19
  • 70
  • 116
  • 1
    Hint: 48 in decimal = 30 in hex – Anatoly Nov 17 '20 at 07:45
  • Does that mean, that in frontend it didn't use hex? I've called const buff = Buffer.from(pubKey, 'hex'); – Han Che Nov 17 '20 at 07:47
  • It means that in console you see two different representations of the same data: in hex and in decimal – Anatoly Nov 17 '20 at 07:50
  • Try to turn Buffer into array in the first case and output it to console – Anatoly Nov 17 '20 at 07:51
  • I'm using the buffer in node-rsa in the browser to do encryption. Does a decimal reprentation will cause a difference? Because the browser encryption is not working while the nodejs version does. Thanks for the help anyway! – Han Che Nov 17 '20 at 07:55

1 Answers1

1

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,
Anatoly
  • 20,799
  • 3
  • 28
  • 42
  • Thanks for the anwser. On stackblitz you can see that on the next line i'm calling const rsa = new NodeRSA(buff.slice(22),...). So according to you buff.slice(22) will return the same value in the browser as in node? – Han Che Nov 17 '20 at 08:10
  • I'm not sure they both have identical `slice` method. But if so then yes the result should be the same – Anatoly Nov 17 '20 at 08:13
  • Try to output to console `buff.slice(22)` in both cases – Anatoly Nov 17 '20 at 08:14
  • Hi, I could narrow down the error i'm getting. In nodejs running buffer.from('abc','utf-8) yields [61,62,63] while in browser it yields [97,98,99] do you know why there is a shift in encoding? Thanks! – Han Che Nov 24 '20 at 10:16
  • I suppose it's just a difference in string representation – Anatoly Nov 24 '20 at 10:29
  • I just checked, 'a' in utf8 is 61 while 97 in ascii... – Han Che Nov 24 '20 at 10:39
  • 1
    61 in hex = 97 in dec – Anatoly Nov 24 '20 at 10:42