In my AngularJS app, I am struggling to get the digest of an uploaded file. The issue is that the resulting hash is not matching the one obtained using bash locally.
Initially, I used jshashes, but when I noticed discrepancies in the hashes generated by the web app and bash, I switched to CryptoJS. However, even with CryptoJS, the hash for the uploaded file remains inconsistent.
Here's a snippet of the code I'm working with:
var reader = new FileReader();
reader.readAsBinaryString(controller.file);
controller.fileHash = CryptoJS.SHA256(reader.result).toString(CryptoJS.enc.Hex);
The above code essentially reads the uploaded file using FileReader
, converts it into a BinaryString, computes the hash, and then displays the result in the html through another controller variable.
On my local shell environment, I use the following command to calculate the file digest:
$ shasum -a 256 [path/to/file]
I am utilizing ng-file-upload for file uploads.
If anyone can offer assistance or point me towards a solution, I would greatly appreciate it.
Thank you for your help.
Update 1:
As per @destroyer's comment, I have modified the code to incorporate the asynchronous nature of readAsBinaryString
:
var reader = new FileReader();
reader.onload = function() {
controller.fileHash = CryptoJS.SHA256(reader.result).toString(CryptoJS.enc.Hex);
};
reader.readAsArrayBuffer(controller.file);
I attempted a solution mentioned here to convert the ArrayBuffer
object into a BinaryString
, but the issue persists.
Update 2: I have included a console log image of the object I am attempting to hash https://i.sstatic.net/2Y0zD.png