hildjj / node-cbor

Encode and decode CBOR documents, with both easy mode, streaming mode, and SAX-style evented mode.
MIT License
361 stars 74 forks source link

enconding bigger numbers are behaving weird #106

Closed ashisherc closed 4 years ago

ashisherc commented 4 years ago

I am encoding number 9999998497035121

This results in cbor fb4341c3790b15c9b8

Which decoding results in 9999998497035120.0 (incorrect number)

I want to encode an unsigned integer, I tried BigNumber and BigInt, non of them are working as expected.

ashisherc commented 4 years ago

More details,

trying to encode using BigInt,

cbor.encodeOne(9999998497035121n).toString("hex")

gives the cbor

c2472386f2162b9371

The decoding is as expected but the cbor is weird, eg. decode the cbor below

1B002386F2162B9371

and we get the same output number, 2 different cbor encoding for the exact same output.

c2472386f2162b9371 - this defined tag and bytes

1B002386F2162B9371 - this defined proper uint number

Although the end result is same but is very critical when the cbor is used to get a hash, and we will receive 2 different hashes.

ashisherc commented 4 years ago

Unable to get this 1B002386F2162B9371 cbor using js.

@hildjj can you please look at this one

ashisherc commented 4 years ago

I see that all the other integers are working perfectly fine but not the 64 bit integers, is there a way I can properly encode them?

hildjj commented 4 years ago

cbor.encodeOne(i, {collapseBigIntegers: true}).toString('hex')