Question

Can somebody advise me what this code does and how can I convert it to Ruby in most simple 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()});

It's a piece of a mexumgen, Perl library which sign update.rdf for Mozilla products with openssl.

Was it helpful?

Solution

This particular example can be converted as

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

where "308191300b06092a864886f70d01010d03818100" is prefix made from that ASN expression up to BIT STRING field (including size of BIT STRING), pack("H") converts binary data to hex representation and unpack("H") converts string in hex back to binary.

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

OTHER TIPS

Have you looked at Net::ASN1?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top