File: Digest.pm

package info (click to toggle)
libdata-printer-perl 1.001000-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 732 kB
  • sloc: perl: 4,305; makefile: 7; sh: 1
file content (126 lines) | stat: -rw-r--r-- 3,129 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package Data::Printer::Filter::Digest;
use strict;
use warnings;
use Data::Printer::Filter;

filter 'Digest::base' => \&_print_digest;

# these modules don't inherit from Digest::base but have the same interface:
filter 'Digest::MD2'  => \&_print_digest;
filter 'Digest::MD4'  => \&_print_digest;

sub _print_digest {
  my ($obj, $ddp) = @_;
  my $digest = $obj->clone->hexdigest;
  my $str = $digest;
  my $ref = ref $obj;

  if ( !exists $ddp->extra_config->{filter_digest}{show_class_name}
      || $ddp->extra_config->{filter_digest}{show_class_name} ) {
      $str .= " ($ref)";
  }

  if( !exists  $ddp->extra_config->{filter_digest}{show_reset}
    || $ddp->extra_config->{filter_digest}{show_reset}
   ) {
     if ($digest eq $ref->new->hexdigest) {
         $str .= ' [reset]';
     }
  }

  return $ddp->maybe_colorize($str, 'datetime', '#ffaaff');
}

1;

__END__

=head1 NAME

Data::Printer::Filter::Digest - pretty-printing MD5, SHA and many other digests

=head1 SYNOPSIS

In your C<.dataprinter> file:

    filters = Digest

You may also setup the look and feel with the following options:

    filter_digest.show_class_name = 0
    filter_digest.show_reset      = 1

    # you can even customize your themes:
    colors.digest = #27ac3c

That's it!

=head1 DESCRIPTION

This is a filter plugin for L<Data::Printer>. It filters through
several message digest objects and displays their current value in
hexadecimal format as a string.

=head2 Parsed Modules

Any module that inherits from L<Digest::base>. The following ones
are actively supported:

=over 4

=item * L<Digest::Adler32>

=item * L<Digest::MD2>

=item * L<Digest::MD4>

=item * L<Digest::MD5>

=item * L<Digest::SHA>

=item * L<Digest::SHA1>

=item * L<Digest::Whirlpool>

=back

If you have any suggestions for more modules or better output,
please let us know.

=head2 Extra Options

Aside from the display color, there are a few other options to
be customized via the C<filter_digest> option key:

=head3 show_class_name

If set to true (the default) the class name will be displayed
right next to the hexadecimal digest.

=head3 show_reset

If set to true (the default), the filter will add a C<[reset]>
tag after dumping an empty digest object. See the rationale below.

=head2 Note on dumping Digest::* objects

The digest operation is effectively a destructive, read-once operation. Once
it has been performed, most Digest::* objects are automatically reset and can
be used to calculate another digest value.

This behaviour - or, rather, forgetting about this behaviour - is
a common source of issues when working with Digests.

This Data::Printer filter will B<not> destroy your object. Instead, we
work on a I<cloned> version to display the hexdigest, leaving your
original object untouched.

As another debugging convenience for developers, since the empty
object will produce a digest even after being used, this filter
adds by default a C<[reset]> tag to indicate that the object is
empty, in a 'reset' state - i.e. its hexdigest is the same as
the hexdigest of a new, empty object of that same class.

=head1 SEE ALSO

L<Data::Printer>