1 package MooseX::Storage::Base::WithChecksum;
4 with 'MooseX::Storage::Basic';
10 our $AUTHORITY = 'cpan:STEVAN';
12 our $DIGEST_MARKER = '__DIGEST__';
19 my $collapsed = $self->$orig( @args );
21 $collapsed->{$DIGEST_MARKER} = $self->_digest_packed($collapsed, @args);
26 around unpack => sub {
27 my ($orig, $class, $data, @args) = @_;
29 # check checksum on data
30 my $old_checksum = delete $data->{$DIGEST_MARKER};
32 my $checksum = $class->_digest_packed($data, @args);
34 ($checksum eq $old_checksum)
35 || confess "Bad Checksum got=($checksum) expected=($old_checksum)";
37 $class->$orig( $data, @args );
42 my ( $self, $collapsed, @args ) = @_;
44 my $d = $self->_digest_object(@args);
47 local $Data::Dumper::Indent = 0;
48 local $Data::Dumper::Sortkeys = 1;
49 local $Data::Dumper::Terse = 1;
50 local $Data::Dumper::Useqq = 0;
51 local $Data::Dumper::Deparse = 0; # FIXME?
52 my $str = Data::Dumper::Dumper($collapsed);
54 # Canonicalize numbers to strings even if it
55 # mangles numbers inside strings. It really
56 # does not matter since its just the checksum
59 $str =~ s/(?<! ['"] ) \b (\d+) \b (?! ['"] )/'$1'/gx;
67 my ( $self, %options ) = @_;
68 my $digest_opts = $options{digest};
70 $digest_opts = [ $digest_opts ]
71 if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY';
73 my ( $d, @args ) = @$digest_opts;
76 if ( $d->can("clone") ) {
79 elsif ( $d->can("reset") ) {
84 die "Can't clone or reset digest object: $d";
88 return Digest->new($d || "SHA1", @args);
100 MooseX::Storage::Base::WithChecksum - A more secure serialization role
104 This is an early implementation of a more secure Storage role,
105 which does integrity checks on the data. It is still being
106 developed so I recommend using it with caution.
108 Any thoughts, ideas or suggestions on improving our technique
115 =item B<pack (?$salt)>
117 =item B<unpack ($data, ?$salt)>
131 All complex software has bugs lurking in it, and this module is no
132 exception. If you find a bug please either email me, or add the bug
137 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
141 =head1 COPYRIGHT AND LICENSE
143 Copyright 2007-2008 by Infinity Interactive, Inc.
145 L<http://www.iinteractive.com>
147 This library is free software; you can redistribute it and/or modify
148 it under the same terms as Perl itself.