-
Notifications
You must be signed in to change notification settings - Fork 466
Description
First of all, thanks a lot for creating this very useful library! 🙏
I recently needed to calculate an MD5 hash of a File object, and while I saw the section in the README showing how to do that, I really didn't like how much custom code it involves.
I was wondering, could maybe this functionality be part of this library? In comparison, Ruby has a Digest::MD5 class which supports calculating hash from a single string, incremental hashing in chunks, and calculating a hash from a file on disk.
Digest::MD5.hexdigest("string")
# or
md5 = Digest::MD5.new
md5.update("chunk1")
md5.update("chunk2")
md5.hexdigest
# or
Digest::MD5.file("/path/to/file").hexdigestI took me quite a while to find a JavaScript library which simplifies reading a File object in chunks – chunked-file-reader – and it appears to work correctly (I get the same MD5 hash as with the snippet in the README here). So I came up with the following function:
function fileMD5 (file) {
return new Promise(function (resolve, reject) {
var spark = new SparkMD5.ArrayBuffer(),
reader = new ChunkedFileReader();
reader.subscribe('chunk', function (e) {
spark.append(e.chunk);
});
reader.subscribe('end', function (e) {
var rawHash = spark.end(true);
var base64Hash = btoa(rawHash);
resolve(base64Hash);
});
reader.readChunks(file);
})
}Since it took me a while to come up with this solution, I was wondering if it made sense to have that built into spark-md5.