Bump version numbers for release
[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
5ca52230 10our $VERSION = '0.18';
69b45b7d 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);
a7f358fb 56 # NOTE:
57 # Canonicalize numbers to strings even if it
58 # mangles numbers inside strings. It really
59 # does not matter since its just the checksum
60 # anyway.
61 # - YK/SL
62 $str =~ s/(?<! ['"] ) \b (\d+) \b (?! ['"] )/'$1'/gx;
b7e2e91b 63 $d->add( $str );
a6ebb4c8 64 }
65
66 return $d->hexdigest;
67}
68
69sub _digest_object {
70 my ( $self, %options ) = @_;
71 my $digest_opts = $options{digest};
06a66732 72
73 $digest_opts = [ $digest_opts ]
74 if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY';
75
a6ebb4c8 76 my ( $d, @args ) = @$digest_opts;
98ae09f0 77
78 if ( ref $d ) {
79 if ( $d->can("clone") ) {
a6ebb4c8 80 return $d->clone;
06a66732 81 }
82 elsif ( $d->can("reset") ) {
98ae09f0 83 $d->reset;
a6ebb4c8 84 return $d;
06a66732 85 }
86 else {
98ae09f0 87 die "Can't clone or reset digest object: $d";
88 }
06a66732 89 }
90 else {
a6ebb4c8 91 return Digest->new($d || "SHA1", @args);
98ae09f0 92 }
98ae09f0 93}
94
c4a322ec 951;
96
97__END__
98
99=pod
100
101=head1 NAME
102
4fa64e86 103MooseX::Storage::Base::WithChecksum - A more secure serialization role
c4a322ec 104
105=head1 DESCRIPTION
106
c86a46cc 107This is an early implementation of a more secure Storage role,
108which does integrity checks on the data. It is still being
109developed so I recommend using it with caution.
110
111Any thoughts, ideas or suggestions on improving our technique
112are very welcome.
113
c4a322ec 114=head1 METHODS
115
116=over 4
117
118=item B<pack (?$salt)>
119
120=item B<unpack ($data, ?$salt)>
121
122=back
123
124=head2 Introspection
125
126=over 4
127
128=item B<meta>
129
130=back
131
132=head1 BUGS
133
134All complex software has bugs lurking in it, and this module is no
135exception. If you find a bug please either email me, or add the bug
136to cpan-RT.
137
138=head1 AUTHOR
139
140Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
141
06a66732 142Yuval Kogman
143
c4a322ec 144=head1 COPYRIGHT AND LICENSE
145
1f3074ea 146Copyright 2007-2008 by Infinity Interactive, Inc.
c4a322ec 147
148L<http://www.iinteractive.com>
149
150This library is free software; you can redistribute it and/or modify
151it under the same terms as Perl itself.
152
153=cut