1 package MooseX::Storage::Base::WithChecksum;
4 with 'MooseX::Storage::Basic';
9 our $DIGEST_MARKER = '__DIGEST__';
16 my $collapsed = $self->$orig( @args );
18 $collapsed->{$DIGEST_MARKER} = $self->_digest_packed($collapsed, @args);
23 around unpack => sub {
24 my ($orig, $class, $data, @args) = @_;
26 # check checksum on data
27 my $old_checksum = delete $data->{$DIGEST_MARKER};
29 my $checksum = $class->_digest_packed($data, @args);
31 ($checksum eq $old_checksum)
32 || confess "Bad Checksum got=($checksum) expected=($old_checksum)";
34 $class->$orig( $data, @args );
39 my ( $self, $collapsed, @args ) = @_;
41 my $d = $self->_digest_object(@args);
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);
51 # Canonicalize numbers to strings even if it
52 # mangles numbers inside strings. It really
53 # does not matter since its just the checksum
56 $str =~ s/(?<! ['"] ) \b (\d+) \b (?! ['"] )/'$1'/gx;
64 my ( $self, %options ) = @_;
65 my $digest_opts = $options{digest};
67 $digest_opts = [ $digest_opts ]
68 if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY';
70 my ( $d, @args ) = @$digest_opts;
73 if ( $d->can("clone") ) {
76 elsif ( $d->can("reset") ) {
81 die "Can't clone or reset digest object: $d";
85 return Digest->new($d || "SHA-1", @args);
99 MooseX::Storage::Base::WithChecksum - A more secure serialization role
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.
107 Any thoughts, ideas or suggestions on improving our technique
114 =item B<pack (?$salt)>
116 =item B<unpack ($data, ?$salt)>
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
136 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
140 =head1 COPYRIGHT AND LICENSE
142 Copyright 2007-2008 by Infinity Interactive, Inc.
144 L<http://www.iinteractive.com>
146 This library is free software; you can redistribute it and/or modify
147 it under the same terms as Perl itself.