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