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