0f1292d0e4bb01259594385899188b3fa3fc39af
[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.02';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 our $DIGEST_MARKER = '__DIGEST__';
14
15 sub pack {
16     my ($self, @args ) = @_;
17
18     my $e = MooseX::Storage::Engine->new( object => $self );
19
20     my $collapsed = $e->collapse_object(@args);
21     
22     $collapsed->{$DIGEST_MARKER} = $self->_digest_packed($collapsed, @args);
23     
24     return $collapsed;
25 }
26
27 sub unpack {
28     my ($class, $data, @args) = @_;
29
30     # check checksum on data
31     
32     my $old_checksum = delete $data->{$DIGEST_MARKER};
33     
34     my $checksum = $class->_digest_packed($data, @args);
35
36     ($checksum eq $old_checksum)
37         || confess "Bad Checksum got=($checksum) expected=($old_checksum)";    
38
39     my $e = MooseX::Storage::Engine->new(class => $class);
40     $class->new($e->expand_object($data, @args));
41 }
42
43
44 sub _digest_packed {
45     my ( $self, $collapsed, @args ) = @_;
46
47     my $d = $self->_digest_object(@args);
48
49     {
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);
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; 
63         $d->add( $str );
64     }
65
66     return $d->hexdigest;
67 }
68
69 sub _digest_object {
70     my ( $self, %options ) = @_;
71     my $digest_opts = $options{digest};
72     
73     $digest_opts = [ $digest_opts ] 
74         if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY';
75         
76     my ( $d, @args ) = @$digest_opts;
77
78     if ( ref $d ) {
79         if ( $d->can("clone") ) {
80             return $d->clone;
81         } 
82         elsif ( $d->can("reset") ) {
83             $d->reset;
84             return $d;
85         } 
86         else {
87             die "Can't clone or reset digest object: $d";
88         }
89     } 
90     else {
91         return Digest->new($d || "SHA1", @args);
92     }
93 }
94
95 1;
96
97 __END__
98
99 =pod
100
101 =head1 NAME
102
103 MooseX::Storage::Base::WithChecksum 
104
105 =head1 DESCRIPTION
106
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. 
110
111 Any thoughts, ideas or suggestions on improving our technique 
112 are very welcome.
113
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
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
136 to cpan-RT.
137
138 =head1 AUTHOR
139
140 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
141
142 Yuval Kogman
143
144 =head1 COPYRIGHT AND LICENSE
145
146 Copyright 2007 by Infinity Interactive, Inc.
147
148 L<http://www.iinteractive.com>
149
150 This library is free software; you can redistribute it and/or modify
151 it under the same terms as Perl itself.
152
153 =cut