views:

657

answers:

2

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.

+1  A: 

Have you looked at Net::ASN1?

Keltia
Hm, can't even find how to download code of it.
vava
Did you checked the svn repo? http://rubyforge.org/scm/?group_id=1581
Keltia
Did you? It's empty. Just a stubs lying around for two years
vava
A: 

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

vava