$ perldoc -f each each HASH Entries are returned in an apparently random order. [...] Since Perl 5.8.1 the ordering is different even between different runs of Perl for security reasons (see "Algorithmic Complexity Attacks" in perlsec).

Great feature, so let's try it!

$ PERL_HASH_SEED_DEBUG=1 perl -e 'my %a=0..299; print $_ for each %a' +| md5sum HASH_SEED = 3379561142 b6d67a24906e8a8541291882f81d31ca - $ PERL_HASH_SEED_DEBUG=1 perl -e 'my %a=0..299; print $_ for each %a' +| md5sum HASH_SEED = 4068799219 b6d67a24906e8a8541291882f81d31ca -

Hmm, same MD5? And what about keys and values?

$ PERL_HASH_SEED_DEBUG=1 perl -e 'my %a=0..299; print $_ for keys %a' +| md5sum HASH_SEED = 1151419008 dd56233cf84603df0d47d272da1af003 - $ PERL_HASH_SEED_DEBUG=1 perl -e 'my %a=0..299; print $_ for keys %a' +| md5sum HASH_SEED = 2731377116 dd56233cf84603df0d47d272da1af003 - $ PERL_HASH_SEED_DEBUG=1 perl -e 'my %a=0..299; print $_ for values %a +' | md5sum HASH_SEED = 1861095523 c1a75ab8e3bf1ff6c07b01025a1219e9 - $ PERL_HASH_SEED_DEBUG=1 perl -e 'my %a=0..299; print $_ for values %a +' | md5sum HASH_SEED = 788024661 c1a75ab8e3bf1ff6c07b01025a1219e9 -

This implies I'm always getting the same output, so each, keys and values are returning the elements in the same order even when the seeds are different! My Perl was compiled without -DUSE_HASH_SEED_EXPLICIT (according to $Config::Config{ccflags}) so I shouldn't be setting the seed manually but anyway I tried, just in case. I used bash's $RANDOM variable, that returns a different value each time it's evaluated:

$ echo $RANDOM 8035 $ echo $RANDOM 797 $ PERL_HASH_SEED=$RANDOM PERL_HASH_SEED_DEBUG=1 perl -e 'my %a=0..299; + print $_ for values %a' | md5sum HASH_SEED = 26815 c1a75ab8e3bf1ff6c07b01025a1219e9 - $ PERL_HASH_SEED=$RANDOM PERL_HASH_SEED_DEBUG=1 perl -e 'my %a=0..299; + print $_ for values %a' | md5sum HASH_SEED = 30449 c1a75ab8e3bf1ff6c07b01025a1219e9 -

Super Search found this but it doesn't seem to apply here since perl -V | grep SEED shows nothing. The test here also returns the same output every time I run it. This is a 5.8.8 running on Debian/Linux. Ideas?

--
David Serrano


In reply to Hashes aren't being differently randomized by Hue-Bond

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.