File Coverage

blib/lib/Crypt/DSA/Signature.pm
Criterion Covered Total %
statement 45 49 91.8
branch 12 20 60.0
condition 1 2 50.0
subroutine 9 9 100.0
pod 2 4 50.0
total 69 84 82.1


line stmt bran cond sub pod time code
1             # $Id: Signature.pm 1843 2005-05-26 16:15:07Z btrott $
2              
3             package Crypt::DSA::Signature;
4 6     6   89 use strict;
  6         84  
  6         151  
5              
6 6     6   91 use Carp qw( croak );
  6         56  
  6         110  
7              
8             sub new {
9 9     9 1 198     my $class = shift;
10 9         119     my %param = @_;
11 9         212     my $sig = bless { }, $class;
12 9 100       154     if ($param{Content}) {
13 3         45         return $sig->deserialize(%param);
14                 }
15 6         73     $sig;
16             }
17              
18             BEGIN {
19 6     6   228     no strict 'refs';
  6         58  
  6         93  
20 6     6   90     for my $meth (qw( r s )) {
21                     *$meth = sub {
22 44     44   555             my($key, $value) = @_;
23 44 50       654             if (ref $value eq 'Math::Pari') {
    100          
    50          
24 0         0                 $key->{$meth} = Math::Pari::pari2pv($value);
25                         }
26                         elsif (ref $value) {
27 18         1047                 $key->{$meth} = "$value";
28                         }
29                         elsif ($value) {
30 0 0       0                 if ($value =~ /^0x/) {
31 0         0                     $key->{$meth} = Math::BigInt->new($value)->bstr;
32                             }
33                             else {
34 0         0                     $key->{$meth} = $value;
35                             }
36                         }
37 44   50     2738             my $ret = $key->{$meth} || "";
38 44 50       1146             $ret = Math::BigInt->new("$ret") if $ret =~ /^\d+$/;
39 44         3498             $ret;
40 12         382         };
41                 }
42             }
43              
44             sub asn {
45 4     4 0 83     require Convert::ASN1;
46 4         163     my $asn = Convert::ASN1->new;
47 4 50       362     $asn->prepare('SEQUENCE { r INTEGER, s INTEGER }') or croak $asn->{error};
48 4         12655     $asn;
49             }
50              
51             sub deserialize {
52 3     3 0 26     my $sig = shift;
53 3         91     my %param = @_;
54 3         36     my $asn = __PACKAGE__->asn;
55 3         31     my $ref;
56 3         44     require MIME::Base64;
57             ## Turn off warnings, because we're attempting to base64-decode content
58             ## that may not be base64-encoded.
59 3         76     local $^W = 0;
60 3         70     for ($param{Content}, MIME::Base64::decode_base64($param{Content})) {
61 4         52         my $out = $asn->decode($_);
62 4 100       1336         $ref = $out, last if $out;
63                 }
64 3 50       39     croak "Invalid Content" unless $ref;
65 3         39     $sig->s($ref->{s});
66 3         38     $sig->r($ref->{r});
67 3         30     $sig;
68             }
69              
70             sub serialize {
71 1     1 1 11     my $sig = shift;
72 1         10     my %param = @_;
73 1         14     my $asn = __PACKAGE__->asn;
74 1 50       13     my $buf = $asn->encode({ s => $sig->s, r => $sig->r })
75                     or croak $asn->{error};
76 1         11     $buf;
77             }
78              
79             1;
80             __END__
81            
82             =head1 NAME
83            
84             Crypt::DSA::Signature - DSA signature object
85            
86             =head1 SYNOPSIS
87            
88             use Crypt::DSA::Signature;
89             my $sig = Crypt::DSA::Signature->new;
90            
91             $sig->r($r);
92             $sig->s($s);
93            
94             =head1 DESCRIPTION
95            
96             I<Crypt::DSA::Signature> represents a DSA signature. It has 2 methods,
97             I<r> and I<s>, which are the big number representations of the 2 pieces of
98             the DSA signature.
99            
100             =head1 USAGE
101            
102             =head2 Crypt::DSA::Signature->new( %options )
103            
104             Creates a new signature object, and optionally initializes it with the
105             information in I<%options>, which can contain:
106            
107             =over 4
108            
109             =item * Content
110            
111             An ASN.1-encoded string representing the DSA signature. In ASN.1 notation,
112             this looks like:
113            
114             SEQUENCE {
115             r INTEGER,
116             s INTEGER
117             }
118            
119             If I<Content> is provided, I<new> will automatically call the I<deserialize>
120             method to parse the content, and set the I<r> and I<s> methods on the
121             resulting I<Crypt::DSA::Signature> object.
122            
123             =back
124            
125             =head2 $sig->serialize
126            
127             Serializes the signature object I<$sig> into the format described above:
128             an ASN.1-encoded representation of the signature, using the ASN.1 syntax
129             above.
130            
131             =head1 AUTHOR & COPYRIGHTS
132            
133             Please see the Crypt::DSA manpage for author, copyright,
134             and license information.
135            
136             =cut
137