convert to Dist::Zilla
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Base / WithChecksum.pm
1 package MooseX::Storage::Base::WithChecksum;
2 use Moose::Role;
3
4 with 'MooseX::Storage::Basic';
5
6 use Digest       ();
7 use Data::Dumper ();
8
9 our $DIGEST_MARKER = '__DIGEST__';
10
11 around pack => sub {
12     my $orig = shift;
13     my $self = shift;
14     my @args = @_;
15
16     my $collapsed = $self->$orig( @args );
17
18     $collapsed->{$DIGEST_MARKER} = $self->_digest_packed($collapsed, @args);
19
20     return $collapsed;
21 };
22
23 around unpack  => sub {
24     my ($orig, $class, $data, @args) = @_;
25
26     # check checksum on data
27     my $old_checksum = delete $data->{$DIGEST_MARKER};
28
29     my $checksum = $class->_digest_packed($data, @args);
30
31     ($checksum eq $old_checksum)
32         || confess "Bad Checksum got=($checksum) expected=($old_checksum)";
33
34     $class->$orig( $data, @args );
35 };
36
37
38 sub _digest_packed {
39     my ( $self, $collapsed, @args ) = @_;
40
41     my $d = $self->_digest_object(@args);
42
43     {
44         local $Data::Dumper::Indent   = 0;
45         local $Data::Dumper::Sortkeys = 1;
46         local $Data::Dumper::Terse    = 1;
47         local $Data::Dumper::Useqq    = 0;
48         local $Data::Dumper::Deparse  = 0; # FIXME?
49         my $str = Data::Dumper::Dumper($collapsed);
50         # NOTE:
51         # Canonicalize numbers to strings even if it
52         # mangles numbers inside strings. It really
53         # does not matter since its just the checksum
54         # anyway.
55         # - YK/SL
56         $str =~ s/(?<! ['"] ) \b (\d+) \b (?! ['"] )/'$1'/gx;
57         $d->add( $str );
58     }
59
60     return $d->hexdigest;
61 }
62
63 sub _digest_object {
64     my ( $self, %options ) = @_;
65     my $digest_opts = $options{digest};
66
67     $digest_opts = [ $digest_opts ]
68         if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY';
69
70     my ( $d, @args ) = @$digest_opts;
71
72     if ( ref $d ) {
73         if ( $d->can("clone") ) {
74             return $d->clone;
75         }
76         elsif ( $d->can("reset") ) {
77             $d->reset;
78             return $d;
79         }
80         else {
81             die "Can't clone or reset digest object: $d";
82         }
83     }
84     else {
85         return Digest->new($d || "SHA-1", @args);
86     }
87 }
88
89 no Moose::Role;
90
91 1;
92
93 __END__
94
95 =pod
96
97 =head1 NAME
98
99 MooseX::Storage::Base::WithChecksum - A more secure serialization role
100
101 =head1 DESCRIPTION
102
103 This is an early implementation of a more secure Storage role,
104 which does integrity checks on the data. It is still being
105 developed so I recommend using it with caution.
106
107 Any thoughts, ideas or suggestions on improving our technique
108 are very welcome.
109
110 =head1 METHODS
111
112 =over 4
113
114 =item B<pack (?$salt)>
115
116 =item B<unpack ($data, ?$salt)>
117
118 =back
119
120 =head2 Introspection
121
122 =over 4
123
124 =item B<meta>
125
126 =back
127
128 =head1 BUGS
129
130 All complex software has bugs lurking in it, and this module is no
131 exception. If you find a bug please either email me, or add the bug
132 to cpan-RT.
133
134 =head1 AUTHOR
135
136 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
137
138 Yuval Kogman
139
140 =head1 COPYRIGHT AND LICENSE
141
142 Copyright 2007-2008 by Infinity Interactive, Inc.
143
144 L<http://www.iinteractive.com>
145
146 This library is free software; you can redistribute it and/or modify
147 it under the same terms as Perl itself.
148
149 =cut