cf1ebdfc7ec4a0848cecf19afa35630524a86ce3
[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 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         $str =~ s/(?<! ['"] ) \b (\d+) \b (?! ['"] )/'$1'/gx; # canonicalize numbers to strings even if it mangles numbers inside strings
57         $d->add( $str );
58     }
59
60     return $d->hexdigest;
61 }
62
63 sub _digest_object {
64     my ( $self, %options ) = @_;
65     my $digest_opts = $options{digest};
66     
67     $digest_opts = [ $digest_opts ] 
68         if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY';
69         
70     my ( $d, @args ) = @$digest_opts;
71
72     if ( ref $d ) {
73         if ( $d->can("clone") ) {
74             return $d->clone;
75         } 
76         elsif ( $d->can("reset") ) {
77             $d->reset;
78             return $d;
79         } 
80         else {
81             die "Can't clone or reset digest object: $d";
82         }
83     } 
84     else {
85         return Digest->new($d || "SHA1", @args);
86     }
87 }
88
89 1;
90
91 __END__
92
93 =pod
94
95 =head1 NAME
96
97 MooseX::Storage::Base::WithChecksum 
98
99 =head1 DESCRIPTION
100
101 This is an early implementation of a more secure Storage role, 
102 which does integrity checks on the data. It is still being 
103 developed so I recommend using it with caution. 
104
105 Any thoughts, ideas or suggestions on improving our technique 
106 are very welcome.
107
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
128 All complex software has bugs lurking in it, and this module is no 
129 exception. If you find a bug please either email me, or add the bug
130 to cpan-RT.
131
132 =head1 AUTHOR
133
134 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
135
136 Yuval Kogman
137
138 =head1 COPYRIGHT AND LICENSE
139
140 Copyright 2007 by Infinity Interactive, Inc.
141
142 L<http://www.iinteractive.com>
143
144 This library is free software; you can redistribute it and/or modify
145 it under the same terms as Perl itself.
146
147 =cut