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