Immediate re-encoding of a decoded value adds extra bytes #52
-
I've got some CBOR-encoded public key bytes in "CTAP2 canonical CBOR encoding form". I'm finding that I can use cbor-x just fine to decode these bytes into an expected JS value, but when I re-encode the decoded value back to bytes, the length increases! import * as cborx from 'cbor-x';
const publicKey = Buffer.from(
'a501020326200121582092245fc7ecf708ca52fe47581528a7efba8a0e12cecc63f2006024999a5feff92258202795d576351f08c95595466e1c947108b14bcd909e2bc160de5e8e2df0081e72',
'hex',
);
console.log('Raw bytes length:', publicKey.length); // 77
const decoded = cborx.decode(publicKey);
const reEncoded = cborx.encode(decoded);
console.log('Re-encoded bytes length:', reEncoded.length); // 87 For comparison, the cbor library I'm replacing with cbor-x correctly re-encodes back to the original length of 77 bytes: import cbor from 'cbor';
const publicKey = Buffer.from(
'a501020326200121582092245fc7ecf708ca52fe47581528a7efba8a0e12cecc63f2006024999a5feff92258202795d576351f08c95595466e1c947108b14bcd909e2bc160de5e8e2df0081e72',
'hex',
);
console.log('Raw bytes length:', publicKey.length); // 77
const decoded = cbor.decode(publicKey);
const reEncoded = cbor.encode(decoded);
console.log('Re-encoded bytes length:', (reEncoded as Buffer).length); // 77 I tried playing around with This has thrown off logic I wrote to shift a pointer ahead the number of bytes of the re-encoded value, because the pointer is moved too far forward:
This means a subsequent attempt to decode the "extensions" bytes next to the public key bytes fails to decode the entire value, because the pointer is too far ahead. What are my options here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
This maintains 77 bytes for me:
Does that work for you? (the reason for this is that JavaScript objects do not preserve the type of the keys and so the keys were getting converted to strings when deserialized as objects, whereas Maps preserve the number type of the keys) |
Beta Was this translation helpful? Give feedback.
This maintains 77 bytes for me:
Does that work for you?
(the reason for this is that JavaScript objects do not preserve the type of the keys and so the keys were getting converted to strings when deserialized as objects, whereas Maps preserve the number type of the keys)