xxhashjs

repository·master·Indexed 43 days ago

https://github.com/pierrec/js-xxhash

A 100% JavaScript implementation of the fast xxHash non-cryptographic hashing algorithm for Node.js and browser environments. Version 0.2.2 provides 32-bit and 64-bit hashing via XXH.h32() and XXH.h64(), supporting one-shot calculations or incremental hashing using .init(), .update(), and .digest().

Tokens
874
Snippets
8
Records
8
Agent score
5%

What's inside xxhashjs

  1. Compute hashes incrementally using update()

    master

    For streaming data or multi-step calculations, instantiate a hash object, update it with data chunks, and then finalize it with digest(). Once digest() is called, the object can be reused or re-initialized with init(seed).

    // Example using 32-bit hash
    var H = XXH.h32( 0xABCD )
    var h = H.update( 'abcd' ).digest().toString(16)
  2. Compute a 64-bit hash in one step

    master

    Use XXH.h64(data, seed) to compute a 64-bit hash immediately. The data can be a string, an ArrayBuffer, or a NodeJS Buffer. The seed can be a number or a UINT64 object.

    var h = XXH.h64( 'abcd', 0xABCD ).toString(16)
  3. Compute a 32-bit hash in one step

    master

    Use XXH.h32(data, seed) to compute a 32-bit hash immediately. The data can be a string, an ArrayBuffer, or a NodeJS Buffer. The seed can be a number or a UINT32 object.

    var h = XXH.h32( 'abcd', 0xABCD ).toString(16)
    // Result: 0xCDA8FAE4
  4. Use XXH.h32() for 32-bit hashing

    master

    The XXH.h32() method provides 32-bit hashing capabilities. It can be used for one-shot hashing or as an incremental hasher.

    Methods:

    • .init(<seed>): Initialize the object with a seed (number or UINT32).
    • .update(<data>): Add data (string, ArrayBuffer, or NodeJS Buffer) to the calculation.
    • .digest(): Finalizes the calculation and returns a UINT32 object. Use .toString(<radix>) or .toNumber() on the result.
    // One-shot
    XXH.h32(data, seed);
    
    // Incremental
    var H = XXH.h32(seed);
    H.update(data);
    H.digest();
  5. Use XXH.h64() for 64-bit hashing

    master

    The XXH.h64() method provides 64-bit hashing capabilities. It can be used for one-shot hashing or as an incremental hasher.

    Methods:

    • .init(<seed>): Initialize the object with a seed (number or UINT64).
    • .update(<data>): Add data (string, ArrayBuffer, or NodeJS Buffer) to the calculation.
    • .digest(): Finalizes the calculation and returns a UINT64 object. Use .toString(<radix>) or .toNumber() on the result.
    // One-shot
    XXH.h64(data, seed);
    
    // Incremental
    var H = XXH.h64(seed);
    H.update(data);
    H.digest();
  6. Use h32 and h64 for 32-bit and 64-bit hashing

    master

    The js-xxhash library provides two main hashing implementations via its entrypoint:

    • h32: Provides 32-bit xxHash functionality.
    • h64: Provides 64-bit xxHash functionality.

    Import the module to access these hashing engines.

    const xxhash = require('xxhashjs');
    
    // Access 32-bit hashing
    const hash32 = xxhash.h32('hello', 0);
    
    // Access 64-bit hashing
    const hash64 = xxhash.h64('hello', 0);