简体   繁体   English

关于密钥格式的 CBOR 编码问题

[英]CBOR encoding issue on format of keys

On javascript (not using node), I am facing different results when CBOR encoding using library ( https://github.com/paroga/cbor-js ) and when using CBOR online ( https://cbor.me/ ).在 javascript(不使用节点)上,当使用库( https://github.com/paroga/cbor-js )进行 CBOR 编码和在线使用 CBOR( Z5E056C500A1C4B6A7DZ110B )时,我面临不同的结果。 Note that even using a more recent CBOR library, result is identical.请注意,即使使用更新的 CBOR 库,结果也是相同的。

For instance setting an object such as:例如设置一个 object 例如:

const initial = { 1: "John", "-2": 456 };

Encoding using CBOR online gives: a201644a6f686e622d321901c8.在线使用 CBOR 编码给出:a201644a6f686e622d321901c8。 Details are:详情如下:

A2             # map(2)
   01          # unsigned(1)
   64          # text(4)
      4A6F686E # "John"
   62          # text(2)
      2D32     # "-2"
   19 01C8     # unsigned(456)

Now encoding using CBOR library on javascript gives a different result: a26131644a6f686e622d321901c8现在在 javascript 上使用 CBOR 库进行编码会得到不同的结果:a26131644a6f686e622d321901c8

When decoding this above Hexadecimal on CBOR online, I got: {"1": "John", "-2": 456}.在在线 CBOR 上解码以上十六进制时,我得到:{“1”:“John”,“-2”:456}。 Result is almost identical than the constant 'initial' except that key 1 now appears with a quote (").结果几乎与常量 'initial' 相同,只是键 1 现在带有引号 (")。

CBOR online re-formats my hexadecimal value to a more 'readable' view: CBOR online 将我的十六进制值重新格式化为更“可读”的视图:

A2             # map(2)
   61          # text(1)
      31       # "1"
   64          # text(4)
      4A6F686E # "John"
   62          # text(2)
      2D32     # "-2"
   19 01C8     # unsigned(456)

See below my Javascript code:请参阅下面我的 Javascript 代码:

    //convert an array of bytes (as 8 bits) to string of Hex. ensure that Hex value are not return with 1 digit but 2 digits. ie '01' instead of '1'
    function toHexString(byteArray) {
      var s = '';
      byteArray.forEach(function(byte) {
        s += ('0' + (byte & 0xFF).toString(16)).slice(-2);
      });
      return s;
    }

    const initial = { 1: "John", "-2": 456 };
    
    var encoded = CBOR.encode(initial);
    var encodedHex = toHexString(Array.from(new Uint8Array(encoded)));
    console.log ( encodedHex );

I could manually replace specific hexadecimal values such as:我可以手动替换特定的十六进制值,例如:

'61 31 64' replaced by '01 64' '61 31 64' 替换为 '01 64'

But not fancy doing it as list could be important to cover all possible options.但不要想这样做,因为列表对于涵盖所有可能的选项可能很重要。

Does someone have a workaround as I need my result to be 'a201644a6f686e622d321901c8' and not 'a26131644a6f686e622d321901c8'?是否有人有解决方法,因为我需要我的结果是“a201644a6f686e622d321901c8”而不是“a26131644a6f686e622d321901c8”?

No workaround, but an attempt at explanation:没有解决方法,但尝试解释:

The CBOR specification, section 5.6 says: CBOR 规范第 5.6 节说:

In applications that need to interwork with JSON-based applications, conversion is simplified by limiting keys to text strings only在需要与基于 JSON 的应用程序交互的应用程序中,通过将键限制为仅文本字符串来简化转换

I assume the cbor-js package follows this approach and silently converts numeric keys to text keys.我假设cbor-js package 遵循这种方法并将数字键静默转换为文本键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM