Perl: Checksum of complex data-structure

565 Views Asked by At

Is there a package/function in Perl that gives me in an easy way

  • a short information (e.g. a number, short string)
  • like a hashvalue/checksum (e.g. MD5)
  • with good distinction (e.g. cryptographic hash like MDx, SHAx)
  • representing the content of a complex datastructure (e.g. hash of arrays of hashes).

Best idea I have in mind is

  1. serialize my structure to a string (e.g. with Data::Dumper)

  2. Hash over the string with MDx

But maybe there is some more elegant way.

2

There are 2 best solutions below

1
On BEST ANSWER

In the past, I used the Data::Dumper (with sorted keys, as pointed by @mob) + Digest::MD5 approach for creating checksums of complex data structures. In my case, the purpose was to compare two or more data structures for equality.

(Very) Simple snippet:

use Data::Dumper qw( Dumper ) ;
use Digest::MD5 qw( md5_hex) ;

sub digest {
    my $data = shift ;
    local $Data::Dumper::Sortkeys = 1;
    return md5_hex( Dumper($data) ) ;
}

Synopsis:

my $cplx_data_checksum = digest({ 
    c => 1 , 
    b => [ 1 , { a => 2 } ]
}) ;

For insights about Digest algo's speed please take a look to the Digest Perl module at https://metacpan.org/pod/Digest#Digest-speed

Hope this helps

0
On

I would consider using

Sereal Encoder

I've used it for a similar problem and was very happy with it, it is fast, offers all the options I could think of needing, and did not take me long to get going at all.

For example, it allows you to choose how to deal with objects, and whether to sort keys on hashes which can be very useful.

Have fun!