Perl: Using sha256 to hash strings into integers
I need 64bit integers to seed some PRNG work I'm doing. I found out that you can easily create four 64bit numbers from any string by using SHA256.
use Digest::SHA;
my $input = $ARGV[0] || int(rand() * 1000000);
# Get raw bytes and convert to an array of uint64_t
my $hash = Digest::SHA::sha256($input);
my @nums = unpack("Q*", $hash);
print "SHA256 Hashing '$input' to: " . join(", ", @nums) . "\n";
Note: We are using sha256
here, not sha256_hex
because we need raw bytes.