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

Time:03-04

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?

CodePudding user response:

I think this could help you: 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)

CodePudding user response:

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.

  • Related