I am trying to get the checksum/hash from a file upload on my webpage.
I am currently able to upload the file via my browser then calculate the hash using the node.js crypto library. I first convert the blob to a data Url.
export function calculateHash(dataUrl, type){
const sha1sum = crypto.createHash('sha1').update(dataUrl).digest("hex");
console.log('Hash sum is ' + sha1sum);
}
Result: 66b8bdd2d1d49f708722c15b26409bc072096697
When i calculate the hash manually from the windows mand prompt using the following mand..
fciv.exe 1_1.wav -sha1
Result: b06071b13a1b50cd2976ed7bb4180f6963e8db8e
I would like to get the same checksum result from the data url in my browser as doing the manual check from the mand prompt.
Is this possible?
I am trying to get the checksum/hash from a file upload on my webpage.
I am currently able to upload the file via my browser then calculate the hash using the node.js crypto library. I first convert the blob to a data Url.
export function calculateHash(dataUrl, type){
const sha1sum = crypto.createHash('sha1').update(dataUrl).digest("hex");
console.log('Hash sum is ' + sha1sum);
}
Result: 66b8bdd2d1d49f708722c15b26409bc072096697
When i calculate the hash manually from the windows mand prompt using the following mand..
fciv.exe 1_1.wav -sha1
Result: b06071b13a1b50cd2976ed7bb4180f6963e8db8e
I would like to get the same checksum result from the data url in my browser as doing the manual check from the mand prompt.
Is this possible?
Share Improve this question edited Jun 20, 2016 at 9:54 Artjom B. 62k26 gold badges135 silver badges230 bronze badges asked Jun 18, 2016 at 1:25 user1526912user1526912 17.3k18 gold badges61 silver badges97 bronze badges 2-
The code you've presented here is from node.js' crypto module. This has nothing to do with a browser or [cryptojs]! Since this seems to be server code, how do you call your
calculateHash
function? What are the inputs to that function? Can you give some examples? This is basically the same ment as a ment to your earlier question. – Artjom B. Commented Jun 20, 2016 at 9:53 - Node.js code runs on the server and only municates with the browser through requests. What kind of requests do you use? Have you read What is the difference between client-side and server-side programming? – Artjom B. Commented Jun 20, 2016 at 9:57
1 Answer
Reset to default 4A data url looks like data:image/png;base64,<BASE-64 DATA>
. You would need to extract the BASE-64 DATA part, decode base64 and then run your hashing algorithm. Or - if you want to perform the hashing in the browser - use the FileReader API:
function calculateHash(file, callback) {
let reader = new FileReader();
reader.onload = function(event) {
let file_sha1 = sha1(reader.result);
callback(file_sha1);
};
reader.readAsArrayBuffer(file);
}
let input = document.getElementById("input-file"),
info = document.getElementById("info");
input.addEventListener("change", function(event) {
let file = input.files[0];
if (file) {
calculateHash(file, function(file_sha1) {
info.textContent = file_sha1;
});
}
});
<script src="https://cdnjs.cloudflare./ajax/libs/js-sha1/0.6.0/sha1.min.js"></script>
<input id="input-file" type="file">
<div id="info"></div>
Requires js-sha1 (npm install js-sha1
).
Credit to: How to checksum the file to be uploaded with javascript?