blib/lib/Crypt/Checksum/CRC32.pm

filter: Statementstmt 100% 28 / 28 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 100% 39 / 39 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Crypt/Checksum/Adler32.pm ↑ Summary blib/lib/Crypt/Cipher/AES.pm »
1 package Crypt::Checksum::CRC32;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.091';
6  
7 2 ▶use base qw(Crypt::Checksum Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our %EXPORT_TAGS = ( all => [qw( crc32_data crc32_data_hex crc32_data_int crc32_file crc32_file_hex crc32_file_int )] );
9 2 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
10 2 our @EXPORT = qw();
11  
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 $Carp::Internal{(__PACKAGE__)}++;
14 2 ▶use CryptX;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶sub crc32_file { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->digest }
Subroutine crc32_file: called CC=1 SCAR=0.0
Pod documented
17 4 ▶sub crc32_file_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->hexdigest }
Subroutine crc32_file_hex: called CC=1 SCAR=0.0
Pod documented
18 1 ▶sub crc32_file_int { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->intdigest }
Subroutine crc32_file_int: called CC=1 SCAR=0.0
Pod documented
19  
20 2 1;
21  
22 =pod
23  
24 =head1 NAME
25  
26 Crypt::Checksum::CRC32 - Compute CRC32 checksum
27  
28 =head1 SYNOPSIS
29  
30 ### Functional interface:
31 use Crypt::Checksum::CRC32 ':all';
32  
33 # calculate CRC32 checksum from string/buffer
34 my $data = 'data string';
35 my $checksum_raw = crc32_data($data);
36 my $checksum_hex = crc32_data_hex($data);
37 my $checksum_int = crc32_data_int($data);
38 # or from file
39 my $checksum_file_raw = crc32_file('filename.dat');
40 my $checksum_file_hex = crc32_file_hex('filename.dat');
41 my $checksum_file_int = crc32_file_int('filename.dat');
42 # or from filehandle
43 my $filehandle = ...; # existing binary-mode filehandle
44 my $checksum_fh_raw = crc32_file($filehandle);
45 my $checksum_fh_hex = crc32_file_hex($filehandle);
46 my $checksum_fh_int = crc32_file_int($filehandle);
47  
48 ### OO interface:
49 use Crypt::Checksum::CRC32;
50  
51 my $d = Crypt::Checksum::CRC32->new;
52 $d->add('any data');
53 $d->add('another data');
54 my $checksum_raw = $d->digest; # raw 4 bytes
55 my $checksum_hex = $d->hexdigest; # hexadecimal form
56 my $checksum_int = $d->intdigest; # 32-bit unsigned integer
57  
58 # or checksum a file instead
59 my $checksum_file_raw = Crypt::Checksum::CRC32->new->addfile('filename.dat')->digest;
60  
61 =head1 DESCRIPTION
62  
63 Computes CRC-32 checksums using the ISO 3309 / ITU-T V.42 polynomial
64 (C<0xEDB88320>, also known as CRC-32/ISO-HDLC). This is the same variant
65 used by Ethernet (IEEE 802.3), PKZIP, gzip, and PNG.
66  
67 I<Updated: v0.057>
68  
69 =head1 EXPORT
70  
71 Nothing is exported by default.
72  
73 You can export selected functions:
74  
75 use Crypt::Checksum::CRC32 qw(crc32_data crc32_data_hex crc32_data_int crc32_file crc32_file_hex crc32_file_int);
76  
77 Or all of them at once:
78  
79 use Crypt::Checksum::CRC32 ':all';
80  
81 =head1 FUNCTIONS
82  
83 =head2 crc32_data
84  
85 Returns the checksum as raw octets.
86  
87 my $checksum_raw = crc32_data('data string');
88 #or
89 my $checksum_raw = crc32_data('any data', 'more data', 'even more data');
90  
91 =head2 crc32_data_hex
92  
93 Returns checksum as a hexadecimal string.
94  
95 my $checksum_hex = crc32_data_hex('data string');
96 #or
97 my $checksum_hex = crc32_data_hex('any data', 'more data', 'even more data');
98  
99 =head2 crc32_data_int
100  
101 Returns checksum as unsigned 32-bit integer.
102  
103 my $checksum_int = crc32_data_int('data string');
104 #or
105 my $checksum_int = crc32_data_int('any data', 'more data', 'even more data');
106  
107 Each C<crc32_data*> function converts its data arguments to bytes using Perl's
108 usual scalar stringification. Defined scalars, including numbers and
109 string-overloaded objects, are accepted. C<undef> is treated as an empty string
110 and may emit Perl's usual "uninitialized value" warning.
111  
112 =head2 crc32_file
113  
114 Returns the checksum as raw octets.
115  
116 my $checksum_raw = crc32_file('filename.dat');
117 #or
118 my $filehandle = ...; # existing binary-mode filehandle
119 my $checksum_raw = crc32_file($filehandle);
120  
121 =head2 crc32_file_hex
122  
123 Returns checksum as a hexadecimal string.
124  
125 my $checksum_hex = crc32_file_hex('filename.dat');
126 #or
127 my $filehandle = ...; # existing binary-mode filehandle
128 my $checksum_hex = crc32_file_hex($filehandle);
129  
130 =head2 crc32_file_int
131  
132 Returns checksum as unsigned 32-bit integer.
133  
134 my $checksum_int = crc32_file_int('filename.dat');
135 #or
136 my $filehandle = ...; # existing binary-mode filehandle
137 my $checksum_int = crc32_file_int($filehandle);
138  
139 =head1 METHODS
140  
141 Unless noted otherwise, assume C<$d> is an existing checksum object created via
142 C<new>.
143  
144 =head2 new
145  
146 Constructor, returns a reference to the checksum object.
147  
148 my $d = Crypt::Checksum::CRC32->new;
149  
150 =head2 clone
151  
152 Creates a copy of the checksum object state and returns a reference to the copy.
153  
154 $d->clone();
155  
156 =head2 reset
157  
158 Reinitialize the checksum object state and returns a reference to the checksum object.
159  
160 $d->reset();
161  
162 =head2 add
163  
164 All arguments are appended to the message we calculate checksum for.
165 The return value is the checksum object itself.
166  
167 Each argument is converted to bytes using Perl's usual scalar stringification.
168 Defined scalars, including numbers and string-overloaded objects, are accepted.
169 C<undef> is treated as an empty string and may emit Perl's usual
170 "uninitialized value" warning.
171  
172 $d->add('any data');
173 #or
174 $d->add('any data', 'more data', 'even more data');
175  
176 =head2 addfile
177  
178 The content of the file (or filehandle) is appended to the message we calculate checksum for.
179 The return value is the checksum object itself.
180  
181 $d->addfile('filename.dat');
182 #or
183 my $filehandle = ...; # existing binary-mode filehandle
184 $d->addfile($filehandle);
185  
186 B<Note:> The filehandle must be in binary mode before you pass it to C<addfile()>.
187  
188 =head2 digest
189  
190 Returns the binary checksum (raw bytes).
191 This method does not alter the object state, so you can call it
192 repeatedly and continue with C<add()> or C<addfile()> afterwards.
193  
194 my $result_raw = $d->digest();
195  
196 =head2 hexdigest
197  
198 Returns the checksum encoded as a hexadecimal string.
199 Like C<digest()>, this method does not alter the object state.
200  
201 my $result_hex = $d->hexdigest();
202  
203 =head2 intdigest
204  
205 Returns the checksum encoded as unsigned 32-bit integer.
206 Like C<digest()>, this method does not alter the object state.
207  
208 my $result_int = $d->intdigest();
209  
210 =head1 SEE ALSO
211  
212 =over
213  
214 =item * L<CryptX>
215  
216 =item * L<https://en.wikipedia.org/wiki/Cyclic_redundancy_check>
217  
218 =back
219  
220 =cut
« blib/lib/Crypt/Checksum/Adler32.pm ↑ Summary blib/lib/Crypt/Cipher/AES.pm »