0

I feel like I'm missing something very basic, but not sure.

crypto = require('crypto')

bytes = crypto.randomBytes(32)

str = bytes.toString('hex')

Buffer.byteLength(str) //64 why....?

Ok found it

1 hex = 4 bits (2^4 = 16) also (0,1,2,3...D, E, F = 16)

2 hex = 8 bits

1 byte = 8 bits (definition of byte)

1 byte = 2^8 = 256 possible values (0-255)

1 hex = 16 possible values (0-F)

2 hex = 16^2 = 256 possible values (00-FF)

Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168

1 Answers1

0

You are creating a hex-string from the raw-bytes - as far as I know characters of a string are stored in 16bit and since 16 bits equal 2 bytes (see this for more information), you get a total length of 64 for the resulting string. The length of the bytes-array will still be 32.

eol
  • 23,236
  • 5
  • 46
  • 64