Install spark-md5 via npm
masterInstall the spark-md5 package using npm to use it in your project.
npm install --save spark-md5repository·master·Indexed 25 days ago
https://github.com/satazor/js-spark-md5A 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.
Install the spark-md5 package using npm to use it in your project.
npm install --save spark-md5To 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();
});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)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)ArrayBuffer or typed arrays, use the SparkMD5.ArrayBuffer class. This is highly efficient for processing large files in chunks using FileReader.SparkMD5.ArrayBuffer instance for binary-based incremental hashing.SparkMD5 instance for string-based incremental hashing.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.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.SparkMD5.ArrayBuffer.hash() method to compute the MD5 hash of an ArrayBuffer in a single call.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).For large strings or streaming data, use the SparkMD5 class to append data in chunks.
new SparkMD5()..append(str) to add a string (automatically handles UTF-8 conversion)..appendBinary(contents) to add a binary string..end(raw) to finalize the computation and get the result. The internal state is reset after .end() is called.