How can I translate Perl Convert :: ASN1 to Ruby code?

Can someone please advise me what this code does and how can I convert it to Ruby in the easiest way?

    #!perl

    use Convert::ASN1;

    my $asn1 = Convert::ASN1->new(encoding => 'DER');
    $asn1->prepare(q<
        Algorithm ::= SEQUENCE {
            oid OBJECT IDENTIFIER,
            opt ANY OPTIONAL
        }
        Signature ::= SEQUENCE {
            alg Algorithm,
            sig BIT STRING
        }
    >);

   my $data = $asn1->encode(sig => $body,
        alg => {oid => sha512WithRSAEncryption()});

      

This is a snippet of mexumgen , a Perl library that sign update.rdf for Mozilla products with openssl.

+1


source to share


2 answers


This particular example can be converted as

data = ["308191300b06092a864886f70d01010d03818100" + body.unpack("H*")].pack("H*")

      



where "308191300b06092a864886f70d01010d03818100" is the prefix made from this ASN expression before the BIT STRING field (including the BIT STRING size), pack ("H") converts the binary data to hexadecimal and decompresses ("H") converts the hexadecimal string back to binary file.

But for a more general ASN converter, it's better to use OpenSSL :: ASN1, which comes with ruby ​​as a standard library. It is completely undocumented, but some people have managed to use it .

+1


source


Have you reviewed Net :: ASN1 ?



0


source







All Articles