fix checksuming
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Base / WithChecksum.pm
index 496ca72..cf1ebdf 100644 (file)
@@ -2,13 +2,13 @@
 package MooseX::Storage::Base::WithChecksum;
 use Moose::Role;
 
-use Digest ();
-#use Storable ();
-use MooseX::Storage::Engine;
-
+use Digest       ();
 use Data::Dumper ();
 
-our $VERSION = '0.01';
+use MooseX::Storage::Engine;
+
+our $VERSION   = '0.01';
+our $AUTHORITY = 'cpan:STEVAN';
 
 our $DIGEST_MARKER = '__DIGEST__';
 
@@ -46,14 +46,15 @@ sub _digest_packed {
 
     my $d = $self->_digest_object(@args);
 
-
     {
-        local $Storable::canonical = 1;
-        local $Data::Dumper::Indent = 0;
+        local $Data::Dumper::Indent   = 0;
         local $Data::Dumper::Sortkeys = 1;
-
-        #Storable::nfreeze($collapsed);
-        $d->add( Data::Dumper::Dumper($collapsed) );
+        local $Data::Dumper::Terse    = 1;
+        local $Data::Dumper::Useqq    = 0;
+        local $Data::Dumper::Deparse  = 0; # FIXME?
+        my $str = Data::Dumper::Dumper($collapsed);
+        $str =~ s/(?<! ['"] ) \b (\d+) \b (?! ['"] )/'$1'/gx; # canonicalize numbers to strings even if it mangles numbers inside strings
+        $d->add( $str );
     }
 
     return $d->hexdigest;
@@ -62,19 +63,25 @@ sub _digest_packed {
 sub _digest_object {
     my ( $self, %options ) = @_;
     my $digest_opts = $options{digest};
-    $digest_opts = [ $digest_opts ] if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY';
+    
+    $digest_opts = [ $digest_opts ] 
+        if !ref($digest_opts) or ref($digest_opts) ne 'ARRAY';
+        
     my ( $d, @args ) = @$digest_opts;
 
     if ( ref $d ) {
         if ( $d->can("clone") ) {
             return $d->clone;
-        } elsif ( $d->can("reset") ) {
+        } 
+        elsif ( $d->can("reset") ) {
             $d->reset;
             return $d;
-        } else {
+        } 
+        else {
             die "Can't clone or reset digest object: $d";
         }
-    } else {
+    } 
+    else {
         return Digest->new($d || "SHA1", @args);
     }
 }
@@ -87,12 +94,17 @@ __END__
 
 =head1 NAME
 
-MooseX::Storage::Base::WithChecksum
-
-=head1 SYNOPSIS
+MooseX::Storage::Base::WithChecksum 
 
 =head1 DESCRIPTION
 
+This is an early implementation of a more secure Storage role, 
+which does integrity checks on the data. It is still being 
+developed so I recommend using it with caution. 
+
+Any thoughts, ideas or suggestions on improving our technique 
+are very welcome.
+
 =head1 METHODS
 
 =over 4
@@ -121,6 +133,8 @@ to cpan-RT.
 
 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
 
+Yuval Kogman
+
 =head1 COPYRIGHT AND LICENSE
 
 Copyright 2007 by Infinity Interactive, Inc.