File Coverage

blib/lib/Mojo/File/Role/Digest.pm
Criterion Covered Total %
statement 38 41 92.6
branch 2 2 100.0
condition n/a
subroutine 10 10 100.0
pod 4 4 100.0
total 54 57 94.7


line stmt bran cond sub pod time code
1             package Mojo::File::Role::Digest;
2 1     1   661 use Mojo::Base -strict, -role, -signatures;
  1         3  
  1         9  
3              
4             our $VERSION = '0.04';
5              
6             requires 'open';
7              
8 1     1   4570 use Carp 'croak';
  1         2  
  1         56  
9 1     1   7 use Digest::MD5;
  1         2  
  1         52  
10 1     1   6 use Digest::SHA;
  1         2  
  1         80  
11              
12             use constant SUPPORTS_QX =>
13 1     1   8 !!(eval { require Digest::QuickXor; Digest::QuickXor->VERSION('0.03'); 1 });
  1         2  
  1         2  
  1         501  
  0         0  
  0         0  
14              
15             # methods
16              
17 6     6 1 8182 sub md5_sum ($self) {
  6         10  
  6         10  
18 6         42 return $self->_calcdigest(Digest::MD5->new);
19             }
20              
21 6     6 1 5111 sub quickxor_hash ($self) {
  6         11  
  6         8  
22 6         594 croak 'Digest::QuickXor not available!' unless SUPPORTS_QX;
23 0         0 return $self->_calcdigest(Digest::QuickXor->new, 'b64digest');
24             }
25              
26 6     6 1 4298 sub sha1_sum ($self) {
  6         11  
  6         10  
27 6         31 return $self->_calcdigest(Digest::SHA->new(1));
28             }
29              
30 6     6 1 4521 sub sha256_sum ($self) {
  6         9  
  6         11  
31 6         34 return $self->_calcdigest(Digest::SHA->new(256));
32             }
33              
34             # internal methods
35              
36 18     18   175 sub _calcdigest ($self, $module, $fn = 'hexdigest') {
  18         26  
  18         23  
  18         28  
  18         31  
37 18 100       78 return -f $self ? $module->addfile($self->open('<'))->$fn : '';
38             }
39              
40             1;
41              
42             =encoding utf8
43              
44             =head1 NAME
45              
46             Mojo::File::Role::Digest - A role for Mojo::File to calculate digests
47              
48             =head1 SYNOPSIS
49              
50             # single file
51             use Mojo::File 'path';
52             my $file = path($path)->with_roles('+Digest');
53              
54             # modified file class
55             use Mojo::File;
56             my $class = Mojo::File->with_roles('+Digest');
57             my $file = $class->new($path);
58              
59             $file->md5_sum;
60             $file->quickxor_hash; # requires Digest::QuickXor
61             $file->sha1_sum;
62             $file->sha256_sum;
63              
64             =head1 DESCRIPTION
65              
66             L is a role for L to calculate MD5, SHA1, SHA256, and QuickXor digests.
67              
68             If the path isn't an existing file, all methods return an empty string C<''>.
69              
70             =head1 APPLY ROLE
71              
72             use Mojo::File 'path';
73              
74             my $file = path($path);
75             my $file_with_digest = $file->with_roles('+Digest');
76              
77             Apply to a single L object. See L.
78              
79             use Mojo::File;
80             my $class = Mojo::File->with_roles('+Digest');
81              
82             my $file1 = $class->new($path1);
83             my $file2 = $class->new($path2);
84              
85             Create a modified file class with applied digest role.
86              
87             =head1 METHODS
88              
89             =head2 md5_sum
90              
91             $string = $file->md5_sum;
92              
93             Returns the MD5 sum of the file in hexadecimal form. See L.
94              
95             =head2 quickxor_hash
96              
97             $string = $file->quickxor_hash;
98              
99             Returns the base64 encoded QuickXorHash of the file. See L.
100             Requires L 0.03 or higher.
101              
102             =head2 sha1_sum
103              
104             $string = $file->sha1_sum;
105              
106             Returns the SHA1 sum of the file in hexadecimal form. See L.
107              
108             =head2 sha256_sum
109              
110             $string = $file->sha256_sum;
111              
112             Returns the SHA256 sum of the file in hexadecimal form. See L.
113              
114             =head1 AUTHOR & COPYRIGHT
115              
116             © 2019–2020 by Tekki (Rolf Stöckli).
117              
118             This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
119              
120             =head1 SEE ALSO
121              
122             L, L, L, L, L, L.
123              
124             =cut