[英]How to get from PrintableString to GeneralString in ASN.1 Golang?
I have Golang structure:我有 Golang 结构:
type Dog struct {
Name string
UUID *big.Int
}
Then I asn1.Marshal it:然后我 asn1.Marshal 它:
dog := Dog{Name: "Rex", UUID: new(big.Int).SetBytes([]byte{0, 0, 7})}
dogAsn, _ := asn1.Marshal(dog)
When I look at the ASN.1 structure of "dogAsn" (using dumpasn1 Linux utility)I see:当我查看“dogAsn”的 ASN.1 结构(使用 dumpasn1 Linux 实用程序)时,我看到:
SEQUENCE {
PrintableString 'Rex'
INTEGER
00 00 00 07
}
I wish NOT to have "PrintableString" there, but instead "GeneralString":我不希望那里有“PrintableString”,而是“GeneralString”:
(Desired output): (期望的输出):
SEQUENCE {
GeneralString 'Rex'
INTEGER
00 00 00 07
}
Adding asn1:"tag:27"
to the "Name" field:将
asn1:"tag:27"
添加到“名称”字段:
type Dog struct {
Name string `asn1:"tag:27"`
...
}
Doesn't solve my issue.不能解决我的问题。 Any good ideas?
有什么好主意吗? Thanks in advance!
提前致谢!
> Solution has been found! >已找到解决方案!
============================== ==============================
If you need to create an ASN.1 GeneralString in a Golang, then do this:如果您需要在 Golang 中创建 ASN.1 GeneralString,请执行以下操作:
1. 1.
type Dog struct {
Name asn1.RawValue // previously was "string"
UUID *big.Int
}
dogGeneralStr := asn1.RawValue{Tag: asn1.TagGeneralString, Bytes: []byte("Rex")}
dog := Dog{Name: dogGeneralStr, UUID: new(big.Int).SetBytes([]byte{0, 0,7})}
dogAsn, _ := asn1.Marshal(dog)
PS Then inside your program, whenever you need this dog.Name in a []byte or string form you just do: dogGeneralStr.Bytes or string(dogGeneralStr.Bytes) respectively. PS 然后在你的程序中,每当你需要这个 dog.Name 的 []byte 或字符串形式时,你只需分别执行:dogGeneralStr.Bytes 或 string(dogGeneralStr.Bytes)。
You can't do this with the built in library.您不能使用内置库执行此操作。 The marshalling logic allows you to pick between
IA5String
, PrintableString
, NumericString
or UTF8String
. 编组逻辑允许您在
IA5String
、 PrintableString
、 NumericString
或UTF8String
之间进行选择。
I found an alternative ASN.1 library: https://github.com/Logicalis/asn1 But is is no longer maintained and also encodes all strings as OctetString
我找到了一个替代的 ASN.1 库: https://github.com/Logicalis/asn1但是它不再维护并且还将所有字符串编码为
OctetString
So you best bet is to copy the source code of encoding/asn1
and add a custom tag to indicate GeneralString
while encoding.因此,最好的办法是复制
encoding/asn1
的源代码,并在编码时添加自定义标记以指示GeneralString
。 Perhaps you could even make a PR out of it.或许您甚至可以利用它制作 PR。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.