2 package MooseX::Storage::Base::WithChecksum;
8 use MooseX::Storage::Engine;
10 our $VERSION = '0.19';
11 our $AUTHORITY = 'cpan:STEVAN';
13 our $DIGEST_MARKER = '__DIGEST__';
16 my ($self, @args ) = @_;
18 my $e = MooseX::Storage::Engine->new( object => $self );
20 my $collapsed = $e->collapse_object(@args);
22 $collapsed->{$DIGEST_MARKER} = $self->_digest_packed($collapsed, @args);
28 my ($class, $data, @args) = @_;
30 # check checksum on data
32 my $old_checksum = delete $data->{$DIGEST_MARKER};
34 my $checksum = $class->_digest_packed($data, @args);
36 ($checksum eq $old_checksum)
37 || confess "Bad Checksum got=($checksum) expected=($old_checksum)";
39 my $e = MooseX::Storage::Engine->new(class => $class);
40 $class->new($e->expand_object($data, @args));
45 my ( $self, $collapsed, @args ) = @_;
47 my $d = $self->_digest_object(@args);
50 local $Data::Dumper::Indent = 0;
51 local $Data::Dumper::Sortkeys = 1;
52 local $Data::Dumper::Terse = 1;
53 local $Data::Dumper::Useqq = 0;
54 local $Data::Dumper::Deparse = 0; # FIXME?
55 my $str = Data::Dumper::Dumper($collapsed);
57 # Canonicalize numbers to strings even if it
58 # mangles numbers inside strings. It really
59 # does not matter since its just the checksum
62 $str =~ s/(?<! ['"] ) \b (\d+) \b (?! ['"] )/'$1'/gx;
70 my ( $self, %options ) = @_;
71 my $digest_opts = $options{digest};
73 $digest_opts = [ $digest_opts ]
74 if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY';
76 my ( $d, @args ) = @$digest_opts;
79 if ( $d->can("clone") ) {
82 elsif ( $d->can("reset") ) {
87 die "Can't clone or reset digest object: $d";
91 return Digest->new($d || "SHA1", @args);
103 MooseX::Storage::Base::WithChecksum - A more secure serialization role
107 This is an early implementation of a more secure Storage role,
108 which does integrity checks on the data. It is still being
109 developed so I recommend using it with caution.
111 Any thoughts, ideas or suggestions on improving our technique
118 =item B<pack (?$salt)>
120 =item B<unpack ($data, ?$salt)>
134 All complex software has bugs lurking in it, and this module is no
135 exception. If you find a bug please either email me, or add the bug
140 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
144 =head1 COPYRIGHT AND LICENSE
146 Copyright 2007-2008 by Infinity Interactive, Inc.
148 L<http://www.iinteractive.com>
150 This library is free software; you can redistribute it and/or modify
151 it under the same terms as Perl itself.