cryptocoinjs / base-x

Encode/decode any base
MIT License
312 stars 75 forks source link

Add capability to encode of padding to a constant length based on the buffer length #70

Closed NoamDev closed 4 years ago

NoamDev commented 4 years ago

I'm using base-x to encode 96-bit random buffers to base 62. But the output length ia not constant because leading zeros are ignored.

I suggest to add an optional boolean parameter to encode: padd. When true, the function will padd the result with leading zeros until its length is equal to

Math.ceil(bufferLength * 8 / Math.log2(base))

For the example above it'd be equal to 17.

I'm willing to write a PR, but wanted to get feedback on the proposed feature itself.

junderw commented 4 years ago

What you want is impossible without doing one of the following:

  1. Introduce a fundamental breaking change to the protocol
  2. Break the x === decode(encode(x)) assumption

So unfortunately no.

Let me know if you have any questions.