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.

5
  • 1
    Hint: 48 in decimal = 30 in hex Commented Nov 17, 2020 at 7:45
  • Does that mean, that in frontend it didn't use hex? I've called const buff = Buffer.from(pubKey, 'hex'); Commented Nov 17, 2020 at 7:47
  • It means that in console you see two different representations of the same data: in hex and in decimal Commented Nov 17, 2020 at 7:50
  • Try to turn Buffer into array in the first case and output it to console Commented Nov 17, 2020 at 7: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! Commented Nov 17, 2020 at 7:55

1 Answer 1

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,
Sign up to request clarification or add additional context in comments.

7 Comments

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?
I'm not sure they both have identical slice method. But if so then yes the result should be the same
Try to output to console buff.slice(22) in both cases
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!
61 in hex = 97 in dec
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.