Commit | Line | Data |
c4a322ec |
1 | |
2 | package MooseX::Storage::Base::WithChecksum; |
3 | use Moose::Role; |
4 | |
06a66732 |
5 | use Digest (); |
34dcaa5d |
6 | use Data::Dumper (); |
7 | |
06a66732 |
8 | use MooseX::Storage::Engine; |
9 | |
c4a322ec |
10 | our $VERSION = '0.01'; |
11 | |
72a40e08 |
12 | our $DIGEST_MARKER = '__DIGEST__'; |
88651e76 |
13 | |
c4a322ec |
14 | sub pack { |
98ae09f0 |
15 | my ($self, @args ) = @_; |
16 | |
c4a322ec |
17 | my $e = MooseX::Storage::Engine->new( object => $self ); |
c4a322ec |
18 | |
a6ebb4c8 |
19 | my $collapsed = $e->collapse_object(@args); |
c4a322ec |
20 | |
88651e76 |
21 | $collapsed->{$DIGEST_MARKER} = $self->_digest_packed($collapsed, @args); |
c4a322ec |
22 | |
23 | return $collapsed; |
24 | } |
25 | |
26 | sub unpack { |
98ae09f0 |
27 | my ($class, $data, @args) = @_; |
c4a322ec |
28 | |
29 | # check checksum on data |
30 | |
34dcaa5d |
31 | my $old_checksum = delete $data->{$DIGEST_MARKER}; |
c4a322ec |
32 | |
98ae09f0 |
33 | my $checksum = $class->_digest_packed($data, @args); |
34 | |
c4a322ec |
35 | ($checksum eq $old_checksum) |
98ae09f0 |
36 | || confess "Bad Checksum got=($checksum) expected=($old_checksum)"; |
c4a322ec |
37 | |
38 | my $e = MooseX::Storage::Engine->new(class => $class); |
a6ebb4c8 |
39 | $class->new($e->expand_object($data, @args)); |
c4a322ec |
40 | } |
41 | |
98ae09f0 |
42 | |
43 | sub _digest_packed { |
44 | my ( $self, $collapsed, @args ) = @_; |
45 | |
a6ebb4c8 |
46 | my $d = $self->_digest_object(@args); |
47 | |
a6ebb4c8 |
48 | { |
06a66732 |
49 | local $Data::Dumper::Indent = 0; |
34dcaa5d |
50 | local $Data::Dumper::Sortkeys = 1; |
34dcaa5d |
51 | $d->add( Data::Dumper::Dumper($collapsed) ); |
a6ebb4c8 |
52 | } |
53 | |
54 | return $d->hexdigest; |
55 | } |
56 | |
57 | sub _digest_object { |
58 | my ( $self, %options ) = @_; |
59 | my $digest_opts = $options{digest}; |
06a66732 |
60 | |
61 | $digest_opts = [ $digest_opts ] |
62 | if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY'; |
63 | |
a6ebb4c8 |
64 | my ( $d, @args ) = @$digest_opts; |
98ae09f0 |
65 | |
66 | if ( ref $d ) { |
67 | if ( $d->can("clone") ) { |
a6ebb4c8 |
68 | return $d->clone; |
06a66732 |
69 | } |
70 | elsif ( $d->can("reset") ) { |
98ae09f0 |
71 | $d->reset; |
a6ebb4c8 |
72 | return $d; |
06a66732 |
73 | } |
74 | else { |
98ae09f0 |
75 | die "Can't clone or reset digest object: $d"; |
76 | } |
06a66732 |
77 | } |
78 | else { |
a6ebb4c8 |
79 | return Digest->new($d || "SHA1", @args); |
98ae09f0 |
80 | } |
98ae09f0 |
81 | } |
82 | |
c4a322ec |
83 | 1; |
84 | |
85 | __END__ |
86 | |
87 | =pod |
88 | |
89 | =head1 NAME |
90 | |
c86a46cc |
91 | MooseX::Storage::Base::WithChecksum |
c4a322ec |
92 | |
93 | =head1 DESCRIPTION |
94 | |
c86a46cc |
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 | |
c4a322ec |
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 | |
06a66732 |
130 | Yuval Kogman |
131 | |
c4a322ec |
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 |