spark-md5 Documentation

repository·master·Indexed 25 days ago

https://github.com/satazor/js-spark-md5

A high-performance MD5 implementation for JavaScript optimized for browser usage. It provides support for direct string hashing via SparkMD5.hash, incremental hashing for large datasets, and specialized binary hashing using the SparkMD5.ArrayBuffer class for efficient processing of ArrayBuffers and typed arrays.

Tokens
1.6K
Snippets
4
Records
13
Agent score
33%

What's inside spark-md5

  1. Hash a file incrementally using chunks

    master

    To avoid high memory usage when hashing large files in the browser, read the file in chunks using File.prototype.slice and FileReader.readAsArrayBuffer, appending each chunk to a SparkMD5.ArrayBuffer instance.

    document.getElementById('file').addEventListener('change', function () {
        var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
            file = this.files[0],
            chunkSize = 2097152,                             // Read in chunks of 2MB
            chunks = Math.ceil(file.size / chunkSize),
            currentChunk = 0,
            spark = new SparkMD5.ArrayBuffer(),
            fileReader = new FileReader();
    
        fileReader.onload = function (e) {
            console.log('read chunk nr', currentChunk + 1, 'of', chunks);
            spark.append(e.target.result);                   // Append array buffer
            currentChunk++;
    
            if (currentChunk < chunks) {
                loadNext();
            } else {
                console.log('finished loading');
                console.info('computed hash', spark.end());  // Compute hash
            }
        };
    
        fileReader.onerror = function () {
            console.warn('oops, something went wrong.');
        };
    
        function loadNext() {
            var start = currentChunk * chunkSize,
                end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
    
            fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
        }
    
        loadNext();
    });
  2. Hash a string directly with SparkMD5.hash

    master

    Use the static SparkMD5.hash method to compute the MD5 hash of a string immediately. By default, it returns a hex hash. Pass true as the second argument to receive a raw binary string instead.

    var hexHash = SparkMD5.hash('Hi there');        // hex hash
    var rawHash = SparkMD5.hash('Hi there', true);  // OR raw hash (binary string)
  3. Use incremental hashing with SparkMD5

    master

    For scenarios where data arrives in pieces, instantiate a new SparkMD5() object. Use .append(str) to add data chunks and .end(raw) to finalize the computation and retrieve the hash.

    var spark = new SparkMD5();
    spark.append('Hi');
    spark.append(' there');
    var hexHash = spark.end();                      // hex hash
    var rawHash = spark.end(true);                  // OR raw hash (binary string)
  4. Hash a binary string immediately with SparkMD5.hashBinary()

    master
    Use SparkMD5.hashBinary() to compute the MD5 hash of a binary string. This method does not perform UTF-8 conversion. Like hash(), it returns a hex string by default or a raw binary string if the raw parameter is set to true.
  5. Hash a string immediately with SparkMD5.hash()

    master
    Use the static SparkMD5.hash() method to compute the MD5 hash of a string in a single call. If the string contains UTF-8 characters, they will be automatically converted. By default, it returns a hex string, but you can request a raw binary string by passing true as the second argument.
  6. Manage SparkMD5 incremental state

    master

    The SparkMD5 and SparkMD5.ArrayBuffer classes allow you to capture and restore the internal state of a hash computation.

    • getState(): Returns an object containing the current buffer, length, and hash state.
    • setState(state): Restores the computation from a previously captured state object.
    • reset(): Clears the current buffer and resets the hash state to initial values.
    • destroy(): Releases memory used by the buffer and hash state (use reset() if you intend to reuse the instance).
  7. Perform incremental MD5 hashing for strings using SparkMD5

    master

    For large strings or streaming data, use the SparkMD5 class to append data in chunks.

    1. Instantiate new SparkMD5().
    2. Use .append(str) to add a string (automatically handles UTF-8 conversion).
    3. Use .appendBinary(contents) to add a binary string.
    4. Call .end(raw) to finalize the computation and get the result. The internal state is reset after .end() is called.