如何将ASN.1的大整数(公钥)INTEGER适配到struct的小int64中?

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

How can I fit large integer (public key) INTEGER of ASN.1 into small int64 of struct?

问题

我必须表示(ASN.1/DER SEQUENCE)伪代码

SEQUENCE ::= {
      INTEGER
      SEQUENCE {...}
      ...
}

其中INTEGER应该是一个公钥。

就Golang结构而言,我目前有以下伪代码

type ... struct {
      num int64,
      ...
}

但是当编译时,我得到了运行时错误,提示:

panic: asn1: structure error: integer too large

我理解,问题在于将大型公钥放入小的int64中,我应该如何解决这个问题? 当我将num int64更改为num []int64时,我得到了另一个错误,提示类型不匹配(这也是有道理的,因为之前是INTEGER,现在是SEQUENCE)...

所以,再次问一下,如何将公钥整数适配到Golang或任何其他编程语言中的int类型?

英文:

I have to represent (ASN.1/DER SEQUENCE) pseudocode:

SEQUENCE ::= {
      INTEGER
      SEQUENCE {...}
      ...
}

Where INTEGER should be a PUBLIC KEY

In terms of Golang struct I have so far pseudocode:

type ... struct {
      num int64,
      ...
}

But when compile, I got runtime error, saying:

panic: asn1: structure error: integer too large

I understand, that problem is with fitting LARGE PUBLIC KEY into small int64, how should I overcome that problem? When I change num int64 to num []int64 I got another error, saying, that type mismatch (which also MAKE SENSE, since was INTEGER and now SEQUENCE)...

So, again, how do you fit PUBLIC KEY INTEGER into int of Golang or any other prog. lang?

答案1

得分: 1

我认为这个链接可能对你有帮助:https://stackoverflow.com/questions/53139020/why-is-unmarshalling-of-a-der-asn-1-large-integer-limited-to-sequence-in-golang(看答案部分)

关于你的评论:
Go的Big.Int不是一个asn1 SEQUENCE(asn1是不可知的,由你或你使用的工具定义如何将asn1 INTEGER映射到可用的内容)。

英文:

I think this could help you: https://stackoverflow.com/questions/53139020/why-is-unmarshalling-of-a-der-asn-1-large-integer-limited-to-sequence-in-golang (look at the answer)

Note on your comment:
Go Big.Int is NOT an asn1 SEQUENCE (asn1 is agnostic, it is up to you or the tool you use to define how you will map asn1 INTEGER to something you can use)

答案2

得分: 0

ASN.1不对INTEGER的大小设置限制,这也是为什么INTEGER被用来表示大型公钥的原因之一。几种编程语言都有一个"Big.INT"表示,可以用来处理这样的大整数。一些商业的ASN.1工具在目标语言(如C或C++)中有一种用于处理这样大整数的替代表示方法,因为这些语言没有Big INT表示。在你的情况下,int64不足以处理公钥整数,它可能超过128位的长度。你需要确定你的ASN.1工具如何处理大整数,或者考虑使用一个支持大整数的ASN.1工具。

英文:

ASN.1 does not put a limit on the size of an INTEGER, which is one reason INTEGER is used to represent the large public key. Several programming languages have a "Big.INT" representation that can be used to handle such large integers. Some commercial ASN.1 Tools have an alternate representation for handling such large integers in target languages such as C or C++ which don't have a Big INT representation. In your case, int64 is not sufficient to handle public key integer which can be more than 128 bits in length. You will need to determine how your ASN.1 tool handles huge integers, or you may consider using an ASN.1 tool that does support big integers.

huangapple
  • 本文由 发表于 2022年3月2日 09:17:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/71316396.html
匿名

发表评论

匿名网友

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

确定