0.04
[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;
34dcaa5d 52 $d->add( Data::Dumper::Dumper($collapsed) );
a6ebb4c8 53 }
54
55 return $d->hexdigest;
56}
57
58sub _digest_object {
59 my ( $self, %options ) = @_;
60 my $digest_opts = $options{digest};
06a66732 61
62 $digest_opts = [ $digest_opts ]
63 if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY';
64
a6ebb4c8 65 my ( $d, @args ) = @$digest_opts;
98ae09f0 66
67 if ( ref $d ) {
68 if ( $d->can("clone") ) {
a6ebb4c8 69 return $d->clone;
06a66732 70 }
71 elsif ( $d->can("reset") ) {
98ae09f0 72 $d->reset;
a6ebb4c8 73 return $d;
06a66732 74 }
75 else {
98ae09f0 76 die "Can't clone or reset digest object: $d";
77 }
06a66732 78 }
79 else {
a6ebb4c8 80 return Digest->new($d || "SHA1", @args);
98ae09f0 81 }
98ae09f0 82}
83
c4a322ec 841;
85
86__END__
87
88=pod
89
90=head1 NAME
91
c86a46cc 92MooseX::Storage::Base::WithChecksum
c4a322ec 93
94=head1 DESCRIPTION
95
c86a46cc 96This is an early implementation of a more secure Storage role,
97which does integrity checks on the data. It is still being
98developed so I recommend using it with caution.
99
100Any thoughts, ideas or suggestions on improving our technique
101are very welcome.
102
c4a322ec 103=head1 METHODS
104
105=over 4
106
107=item B<pack (?$salt)>
108
109=item B<unpack ($data, ?$salt)>
110
111=back
112
113=head2 Introspection
114
115=over 4
116
117=item B<meta>
118
119=back
120
121=head1 BUGS
122
123All complex software has bugs lurking in it, and this module is no
124exception. If you find a bug please either email me, or add the bug
125to cpan-RT.
126
127=head1 AUTHOR
128
129Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
130
06a66732 131Yuval Kogman
132
c4a322ec 133=head1 COPYRIGHT AND LICENSE
134
135Copyright 2007 by Infinity Interactive, Inc.
136
137L<http://www.iinteractive.com>
138
139This library is free software; you can redistribute it and/or modify
140it under the same terms as Perl itself.
141
142=cut