the usual
inconsistent at best
Hashing to an 8-bit Integer

I needed to come up with a way to spread out chunks of a subnet among a handful of users, so I came up with this routine:

sub hash256 {
    my $str  = shift;
    my $hmac = hmac_sha256($str);
    my $val  = unpack "L*" => $hmac;
    my $hash = $val % 256;

    return $hash;
}

Used:

for my $str ( qw/scott paul mark jayce rvd james/ ) {
    say sprintf "%-15s %d" => $str, hash256($str);
}

Now the result of my $hash = hash256('joe') can be used as, say, the third octet in a cidr: 192.168.$hash.0/24.


Last modified on 2014-12-16