data_hash.js

/* global BigInt */
/* eslint-disable no-bitwise */

/**
 * @function fnvHash
 * @description FNV-1a 64-bit hash function
 *
 * @since 4.0.2
 *
 * @param {*} input The input to hash
 *
 * @return {string} The 64-bit FNV-1a hash of the input as a 16-character hex string
 */
export const fnvHash = ( input ) => {
	const str = String( input );
	let hash = 14695981039346656037n; // FNV-1a 64-bit basis (as BigInt)
	const prime = 1099511628211n; // FNV-1a 64-bit prime
	const mask = 0xFFFFFFFFFFFFFFFFn; // Mask to 64 bits

	for ( let i = 0; i < str.length; i++ ) {
		const charCode = BigInt( str.charCodeAt( i ) );
		hash ^= charCode; // XOR with current character
		hash *= prime; // Multiply by prime
		hash &= mask; // Mask to 64 bits
	}

	// Convert to hex string (16 chars for 64 bits)
	return hash.toString( 16 ).padStart( 16, '0' );
};