@anglinb/city-hash
Version:
TypeScript implementation of CityHash64 - fast, non-cryptographic hash function
48 lines (47 loc) • 1.57 kB
TypeScript
/**
* CityHash64 - Fast, non-cryptographic hash function
*
* Computes a 64-bit hash of the input data using Google's CityHash algorithm.
* This is a fast, high-quality hash function suitable for hash tables and
* other non-cryptographic uses.
*
* @param input - The data to hash (string or Uint8Array)
* @returns A 64-bit hash value as a BigInt
*
* @example
* ```typescript
* import { cityHash64 } from '@anglinb/city-hash';
*
* // Hash a string
* const hash1 = cityHash64("hello world");
* console.log(hash1); // 12386028635079221413n
*
* // Hash binary data
* const data = new Uint8Array([1, 2, 3, 4, 5]);
* const hash2 = cityHash64(data);
* ```
*/
export declare function cityHash64(input: string | Uint8Array): bigint;
/**
* CityHash64 truncated to JavaScript's MAX_SAFE_INTEGER
*
* Takes the output of cityHash64 and truncates it to fit within JavaScript's
* Number.MAX_SAFE_INTEGER (2^53 - 1). This is done by masking off the higher
* bits, which can be easily reproduced in other systems like ClickHouse.
*
* @param input - The data to hash (string or Uint8Array)
* @returns A hash value as a number, guaranteed to be <= Number.MAX_SAFE_INTEGER
*
* @example
* ```typescript
* import { cityHash64NumMax } from '@anglinb/city-hash';
*
* // Hash a string
* const hash1 = cityHash64NumMax("hello world");
* console.log(hash1); // A safe integer
*
* // Reproduce in ClickHouse/SQL:
* // SELECT cityHash64('hello world') & 0x1FFFFFFFFFFFFF
* ```
*/
export declare function cityHash64NumMax(input: string | Uint8Array): number;