xxh64.pl

#!/usr/bin/env perl
use strict;
use warnings;

################################################################################
# 2026-11-19
#
# Pure Perl version of xxHash64 with two functions: xxh64() and xxh64_hex()
#
# See also: Crypt::xxHash
################################################################################
# Usage:
#
# require("xxh64.pl");
#
# my $x = xxh64($data);
# my $y = xxh64($data, $seed);
################################################################################

if (!caller()) {
    my $seed = 0;
    foreach my $data (@ARGV) {
        printf("'%s': %s\n", $data, xxh64($data, $seed));
    }
}

################################################################################
################################################################################

# Standalone XXH64 hex function. Requires Perl built with 64-bit integers.
# Purposely written in-line so it's easy to copy and paste
sub xxh64 {
    my ($data, $seed) = @_;
    my $h;

    # Keep all XXH64 arithmetic in wrapping signed 64-bit mode; explicitly
    # restore default semantics for the unsigned bit operations and formatting.
    {
        use integer;
        no warnings 'portable';

    $seed = 0 unless defined $seed;

    # Use signed 64-bit forms of the XXH64 primes so arithmetic wraps.
	my $p1 = 0x9E3779B185EBCA87 | 0;
    my $p2 = 0xC2B2AE3D27D4EB4F | 0,
    my $p3 = 0x165667B19E3779F9 | 0,
    my $p4 = 0x85EBCA77C2B2AE63 | 0,
	my $p5 = 0x27D4EB2F165667C5 | 0,

    # Convert signed to unsigned
	$seed |= 0;

    my $rotl = sub {
        my ($x, $r) = @_;
        return ($x << $r) | (($x >> (64 - $r)) & ((1 << $r) - 1));
    };

    my $round = sub {
        my ($acc, $in) = @_;
        $acc += $in * $p2;
        $acc = $rotl->($acc, 31);
        return $acc * $p1;
    };

    my $merge = sub {
        my ($h, $v) = @_;
        $h ^= $round->(0, $v);
        return $h * $p1 + $p4;
    };

    my $len = length $data;
    my $p   = 0;
    if ($len >= 32) {
        my @v = ($seed + $p1 + $p2, $seed + $p2, $seed, $seed - $p1);
        my $limit = $len - 32;
        while ($p <= $limit) {
            my @k = unpack('q<4', substr($data, $p, 32));
            $v[0] = $round->($v[0], $k[0]);
            $v[1] = $round->($v[1], $k[1]);
            $v[2] = $round->($v[2], $k[2]);
            $v[3] = $round->($v[3], $k[3]);
            $p += 32;
        }
        $h = $rotl->($v[0], 1) + $rotl->($v[1], 7)
           + $rotl->($v[2], 12) + $rotl->($v[3], 18);
        for my $v (@v) {
            $h = $merge->($h, $v);
        }
    }
    else {
        $h = $seed + $p5;
    }

    $h += $len;

    while ($p + 8 <= $len) {
        my $k = unpack('q<', substr($data, $p, 8));
        $h ^= $round->(0, $k);
        $h = $rotl->($h, 27) * $p1 + $p4;
        $p += 8;
    }

    if ($p + 4 <= $len) {
        $h ^= unpack('V', substr($data, $p, 4)) * $p1;
        $h = $rotl->($h, 23) * $p2 + $p3;
        $p += 4;
    }

    while ($p < $len) {
        $h ^= ord(substr($data, $p, 1)) * $p5;
        $h = $rotl->($h, 11) * $p1;
        $p++;
    }

    # Avalanche; masks make these arithmetic right shifts logical.
    $h ^= ($h >> 33) & 0x7FFFFFFF;
    $h *= $p2;
    $h ^= ($h >> 29) & 0x7FFFFFFFF;
    $h *= $p3;
    $h ^= ($h >> 32) & 0xFFFFFFFF;

    }

    no integer;
    # Convert the signed IV to its unsigned UV bit-pattern value.
    $h |= 0;

	return $h;
}

# Returns the XXH64 digest as a 16-character lowercase hex string.
sub xxh64_hex {
    my ($data, $seed) = @_;

    my $ret = sprintf('%016x', xxh64($data, $seed));

	return $ret;
}

1;