Golang中字节的格式化方式

huangapple go评论70阅读模式
英文:

Format of bytes in Golang

问题

根据你提供的信息,我看到Python版本和Golang版本的输出结果不同。可能存在一些问题。以下是一些建议:

  1. 检查输入参数的值是否正确。确保SenderId、RecipientId、pv和sk的值与预期一致。

  2. 检查编码方式是否正确。确认在Python版本中使用的是UTF-8编码,而在Golang版本中使用的是默认编码。

  3. 检查字节顺序是否正确。Python版本使用的是little-endian字节顺序,而Golang版本中使用的是默认字节顺序。确保两个版本使用相同的字节顺序。

  4. 检查数据类型是否正确。确认在Golang版本中使用的是字节数组([]byte),而在Python版本中使用的是字节串(bytes)。

请根据以上建议检查代码,看看是否能够找到问题所在。

英文:

I have faced up with difficulties to decode signature in bytes.
Condition:

 signedStringForIntermediateSigningKeySignature =
length_of_sender_id || sender_id || length_of_protocol_version || protocol_version || length_of_signed_key || signed_key

The "||" notation means concatenate. Each component—sender_id, protocolVersion, signedKey—must be UTF-8 encoded. The signedKey must be the string of intermediateSigningKey.signedKey. The byte length of each component is 4 bytes in little-endian format.

input:

signedStringForIntermediateSigningKeySignature =
\x06\x00\x00\x00 || Google || | \x04\x00\x00\x00 || ECv2 || \xb5\x00\x00\x00 || {"keyExpiration":"1542323393147","keyValue":"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE/1+3HBVSbdv+j7NaArdgMyoSAM43yRydzqdg1TxodSzA96Dj4Mc1EiKroxxunavVIvdxGnJeFViTzFvzFRxyCw\u003d\u003d"}

Python version:

signed = b""
for a in args:
    signed += len(a).to_bytes(4, byteorder="little")
    signed += bytes(a, "utf-8")
return signed

output:

b'\x06\x00\x00\x00Google\x04\x00\x00\x00ECv2\xb5\x00\x00\x00{"keyExpiration":"1542323393147","keyValue":"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE/1+3HBVSbdv+j7NaArdgMyoSAM43yRydzqdg1TxodSzA96Dj4Mc1EiKroxxunavVIvdxGnJeFViTzFvzFRxyCw\\u003d\\u003d"}'

Golang version:

var signed []byte
arguments := []string{SenderId, RecipientId, pv, sk}
for _, a := range arguments {
	b := make([]byte, 4)
	binary.LittleEndian.PutUint32(b, uint32(len(a)))
	signed = append(signed, b...)
	signed = append(signed, []byte(a)...)
}
return signed

output:

[6 0 0 0 71 111 111 103 108 101 0 0 0 0 4 0 0 0 69 67 118 50 181 0 0 0 123 34 107 101 121 86 97 108 117 101 34 58 34 77 70 107 119 69 119 89 72 75 111 90 73 122 106 48 67 65 81 89 73 75 111 90 73 122 106 48 68 65 81 99 68 81 103 65 69 113 121 86 105 110 120 85 122 104 82 103 101 71 88 83 118 78 85 56 55 84 52 120 72 65 72 102 70 98 84 101 98 82 103 71 74 106 78 116 117 105 115 79 81 81 121 49 73 81 122 118 117 83 50 86 52 110 102 70 102 117 109 71 78 71 117 53 55 43 112 57 70 47 72 87 115 65 119 48 80 100 43 55 73 67 65 92 117 48 48 51 100 92 117 48 48 51 100 34 44 34 107 101 121 69 120 112 105 114 97 116 105 111 110 34 58 34 49 54 51 57 49 48 54 55 50 54 57 48 48 34 125]

I see that output is different.
Can you hint me is there any problem?

答案1

得分: 0

似乎问题出在输出的查看上,两个输出的字节数相等。
如果你想像Python一样查看你的signed变量,下面的代码会很有用:

signedStr := strconv.Quote(string(signed))
fmt.Println(signedStr)
英文:

It seems the problem is the viewing of output and both outputs are equal in bytes.
if you want to view your signed variable like Python, following code is useful:

signedStr := strconv.Quote(string(signed))
fmt.Println(signedStr)

huangapple
  • 本文由 发表于 2022年2月4日 12:23:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/70981384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定