fnv1a64Hex function

String fnv1a64Hex(
  1. String input
)

Computes the 64-bit FNV-1a hash of input and returns a fixed 16-character lowercase hexadecimal string.

Designed as a lightweight, zero-dependency string hashing function for file caching, indexing, and bucket distribution. Guarantees deterministic output across all Dart platforms (Native, AOT, Web) and process restarts.

Examples

fnv1a64Hex('https://example.com/image.png'); // Returns: '9a779be407144bbd'

Implementation

String fnv1a64Hex(String input) {
  var hash = BigInt.parse('14695981039346656037');
  final prime = BigInt.parse('1099511628211');
  final mask64 = BigInt.parse('18446744073709551615');

  final bytes = utf8.encode(input);
  for (final b in bytes) {
    hash ^= BigInt.from(b);
    hash = (hash * prime) & mask64;
  }
  return hash.toRadixString(16).padLeft(16, '0');
}