File Coverage

blib/lib/Crypt/OpenPGP/MDC.pm
Criterion Covered Total %
statement 27 27 100.0
branch 2 2 100.0
condition n/a
subroutine 9 9 100.0
pod 4 5 80.0
total 42 43 97.6


line stmt bran cond sub pod time code
1             package Crypt::OpenPGP::MDC;
2 1     1   4 use strict;
  1         2  
  1         27  
3              
4 1     1   3 use Crypt::OpenPGP::Digest;
  1         1  
  1         14  
5 1     1   3 use Crypt::OpenPGP::ErrorHandler;
  1         1  
  1         23  
6 1     1   2 use base qw( Crypt::OpenPGP::ErrorHandler );
  1         1  
  1         287  
7              
8             sub new {
9 2     2 1 5 my $mdc = bless { }, shift;
10 2         7 $mdc->init(@_);
11             }
12              
13             sub init {
14 2     2 0 3 my $mdc = shift;
15 2         5 my %param = @_;
16 2 100       7 if (my $data = $param{Data}) {
17 1         4 my $dgst = Crypt::OpenPGP::Digest->new('SHA1');
18 1         4 $mdc->{body} = $dgst->hash($data);
19             }
20 2         6 $mdc;
21             }
22              
23 1     1 1 11 sub digest { $_[0]->{body} }
24              
25             sub parse {
26 1     1 1 3 my $class = shift;
27 1         3 my($buf) = @_;
28 1         5 my $mdc = $class->new;
29 1         7 $mdc->{body} = $buf->get_bytes($buf->length - $buf->offset);
30 1         21 $mdc;
31             }
32              
33 1     1 1 2 sub save { $_[0]->{body} }
34              
35             1;
36             __END__
37              
38             =head1 NAME
39              
40             Crypt::OpenPGP::MDC - MDC (modification detection code) packet
41              
42             =head1 SYNOPSIS
43              
44             use Crypt::OpenPGP::MDC;
45              
46             my $mdc = Crypt::OpenPGP::MDC->new( Data => 'foobar' );
47             my $digest = $mdc->digest;
48             my $serialized = $mdc->save;
49              
50             =head1 DESCRIPTION
51              
52             I<Crypt::OpenPGP::MDC> is a PGP MDC (modification detection code) packet.
53             Such a packet is used alongside Encrypted-MDC data packets so that
54             modifications to the ciphertext can be detected. The MDC packet contains
55             a C<SHA-1> digest of the plaintext for comparison with the decrypted
56             plaintext.
57              
58             You generally will never need to construct a I<Crypt::OpenPGP::MDC>
59             packet yourself; usage is by the I<Crypt::OpenPGP::Ciphertext> object.
60              
61             =head1 USAGE
62              
63             =head2 Crypt::OpenPGP::MDC->new( [ Data => $data ] )
64              
65             Creates a new MDC packet object and returns that object. If you do not
66             supply any data I<$data>, the object is created empty; this is used, for
67             example, in I<parse> (below), to create an empty packet which is then
68             filled from the data in the buffer.
69              
70             If you wish to initialize a non-empty object, supply I<new> with
71             the I<Data> parameter along with a value I<$data>. I<$data> should
72             contain the plaintext prefix (length = cipher blocksize + 2), the actual
73             plaintext, and two octets corresponding to the hex digits C<0xd3> and
74             C<0x14>.
75              
76             =head2 $mdc->save
77              
78             Returns the text of the MDC packet; this is the digest of the data passed
79             to I<new> (above) as I<$data>, for example.
80              
81             =head2 Crypt::OpenPGP::MDC->parse($buffer)
82              
83             Given I<$buffer>, a I<Crypt::OpenPGP::Buffer> object holding (or
84             with offset pointing to) an MDC packet, returns a new <Crypt::OpenPGP::MDC>
85             object, initialized with the MDC data in the buffer.
86              
87             =head2 $mdc->digest
88              
89             Returns the MDC digest data (eg. the string passed as I<$data> to
90             I<new>, above).
91              
92             =head1 AUTHOR & COPYRIGHTS
93              
94             Please see the Crypt::OpenPGP manpage for author, copyright, and
95             license information.
96              
97             =cut