解码 SSL 证书序列号

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

Go decode ssl serial number

问题

我有一个由其他应用程序以二进制编码的 SSL 序列号 7b:c9:91:be:0b:be:08:2f:3a:97:60:84:f3:f8:4a:f2:d4:30:57:e2,我想将其从二进制解码为字符串形式。也许有一个库或一组函数可以实现这个功能。

英文:

I have ssl serial number 7b:c9:91:be:0b:be:08:2f:3a:97:60:84:f3:f8:4a:f2:d4:30:57:e2, binary encoded by other application in \u001c\ufffdدc\ufffd\u001e\ufffd\ufffd\ufffdN\ufffdhr\u001a\ufffd䶓\ufffd

How can I decode ssl serial number from binary to string view? Maybe there is a library or a set of functions for this?

答案1

得分: 1

对于最基本的情况,你需要像这样的代码:https://go.dev/play/p/gp74V27lmAv

它遍历二进制编码的串口数据的每个字节,并将其转换为十六进制。它使用“:”作为字节之间的分隔符。

这段代码只是一个示例,可以进行大量改进,特别是如果你知道这些串口数据的长度。然后,你可以通过传递各个字节来使用单个fmt.Sprintf()调用进行转换,例如如果你的串口数据长度为6个字节,可以使用fmt.Sprintf("%x:%x:%x:%x:%x:%x", b[0], b[1], b[2], b[3], b[4], b[5])。当然,你首先需要进行所有相关的验证。

英文:

For the most basic case you need something like this: https://go.dev/play/p/gp74V27lmAv

It goes over each byte of the binary encoded serial and converts it to hex. It separates the bytes with a ":" delimiter.

The code is an example and can be vastly improved, especially if you know how long those serials are. Then you can convert them with a single fmt.Sprintf() call by passing the individual bytes, e.g. fmt.Sprintf("%x:%x:%x:%x:%x:%x", b[0], b[1], b[2], b[3], b[4], b[5]) if your serial is 6 bytes long. You will obviously need to do all the relevant validation first.

huangapple
  • 本文由 发表于 2022年3月30日 21:38:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/71678315.html
匿名

发表评论

匿名网友

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

确定