SparkMD5 is a fast md5 implementation of the MD5 algorithm.
This script is based in the JKM md5 library which is the
fastest algorithm around (see: http://jsperf.com/md5-shootout/7).
This is most suitable for browser usage, because nodejs
version might be faster.
NOTE: Please disable Firebug while performing the test! Firebug consumes a lot of memory and CPU and slows the test by a great margin.
- Fix computation for large amounts of data (overflow)
- Functionality wrapped in a closure
- Object oriented library
- Incremental md5 (see bellow)
- Support for array buffers (typed arrays)
- CommonJS (it can be used in node) and AMD integration
- Validates using jshint
Incremental md5 performs a lot better for hashing large ammounts of data, such as files. One could read files in chunks, using the FileReader & Blob's, and append each chunk for md5 hashing while keeping memory usage low. See example bellow.
var hexHash = SparkMD5.hash('Hi there'); // hex hash
var rawHash = SparkMD5.hash('Hi there', true); // OR raw 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
NOTE: If you test the code bellow using the file:// protocol in chrome you must start the browser with -allow-file-access-from-files argument. Please see: http://code.google.com/p/chromium/issues/detail?id=60889
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();
});
You can see some more examples in the test folder.
Appends a string.
Appends a binary string (e.g.: string returned from the deprecated readAsBinaryString).
Finishes the computation of the md5, returning the hex result.
If raw
is true, the raw result will be returned instead.
Resets the internal state of the computation.
Returns an object representing the internal computation state. You can pass this state to setState(). This feature is useful to resume an incremental md5.
Sets the internal computation state. See: getState().
Releases memory used by the incremental buffer and other additional resources.
Hashes a string directly, returning the hex result.
If raw
is true, the raw result will be returned instead.
Note that this function is static
.
Hashes a binary string directly (e.g.: string returned from the deprecated readAsBinaryString), returning the hex result.
If raw
is true, the raw result will be returned instead.
Note that this function is static
.
Appends an array buffer.
Finishes the computation of the md5, returning the hex result.
If raw
is true, the raw result will be returned instead.
Resets the internal state of the computation.
Releases memory used by the incremental buffer and other additional resources.
Returns an object representing the internal computation state. You can pass this state to setState(). This feature is useful to resume an incremental md5.
Sets the internal computation state. See: getState().
Hashes an array buffer directly, returning the hex result.
If raw
is true, the raw result will be returned instead.
Note that this function is static
.
Joseph Myers (http://www.myersdaily.org/joseph/javascript/md5-text.html)