String/Hash.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« String/Dump.pm ↑ Summary String/Hexa.pm »
1 #
2 # $Id$
3 #
4 # string::hash Brik
5 #
6 package Metabrik::String::Hash;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable sha sha1 sha256 sha512 md5 md5sum sum) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 commands => {
19 sha1 => [ qw(data) ],
20 sha256 => [ qw(data) ],
21 sha512 => [ qw(data) ],
22 md5 => [ qw(data) ],
23 mmh3 => [ qw(data signed|OPTIONAL) ],
24 },
25 require_modules => {
26 'Crypt::Digest' => [ ],
27 'Digest::MurmurHash3' => [ qw(murmur32) ],
28 },
29 };
30 }
31  
32 sub _hash {
33 my $self = shift;
34 my ($function, $data) = @_;
35  
36 $self->brik_help_run_undef_arg($function, $data) or return;
37  
38 eval("use Crypt::Digest qw(digest_data_hex);");
39 if ($@) {
40 chomp($@);
41 return $self->log->error("$function: unable to load function: $@");
42 }
43  
44 my $hash;
45 eval {
46 $hash = Crypt::Digest::digest_data_hex(uc($function), $data);
47 };
48 if ($@) {
49 chomp($@);
50 return $self->log->error("$function: unable to compute hash: $@");
51 }
52  
53 return $hash;
54 }
55  
56 sub sha1 {
57 my $self = shift;
58 my ($data) = @_;
59  
60 return $self->_hash('sha1', $data);
61 }
62  
63 sub sha256 {
64 my $self = shift;
65 my ($data) = @_;
66  
67 return $self->_hash('sha256', $data);
68 }
69  
70 sub sha512 {
71 my $self = shift;
72 my ($data) = @_;
73  
74 return $self->_hash('sha512', $data);
75 }
76  
77 sub md5 {
78 my $self = shift;
79 my ($data) = @_;
80  
81 return $self->_hash('md5', $data);
82 }
83  
84 sub mmh3 {
85 my $self = shift;
86 my ($data, $signed) = @_;
87  
88 $signed ||= 0;
89  
90 my $hash;
91 eval {
92 $hash = Digest::MurmurHash3::murmur32($data, 0, $signed);
93 };
94 if ($@) {
95 chomp($@);
96 return $self->log->error("mmh3: unable to compute hash: $@");
97 }
98  
99 return $hash;
100 }
101  
102 1;
103  
104 __END__
« String/Dump.pm ↑ Summary String/Hexa.pm »