NPM 'crypto' returning different hash values than online generators (node)





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am probably not using the crypto module correctly, maybe someone can help me out.



The goal is to find the sha-256 hash of a file dropped in a dropzone. The problem is that the hash being returned is different from online hash checkers (which are returning the seemingly correct values). Here is my code:



const crypto = require("crypto");
const hash = crypto.createHash("sha256");

handleOnDrop = file => {
hash.update(file);
const hashOutput = hash.digest("hex");
console.log(hashOutput);
};


Crypto docs - https://nodejs.org/api/crypto.html#crypto_node_js_crypto_constants



I am fairly sure the hash values I am getting from this code is not just the file name, I checked a few permutations with the online checkers.



Any ideas? Thanks!










share|improve this question

























  • What is file?

    – Josh Lee
    Jan 3 at 22:08











  • And what is hash? How do you create it?

    – Styx
    Jan 3 at 22:29











  • File is input as a blob by the client (through react-dropzone). Hash is from the crypto docs, I edited the original post to include that. Thanks

    – Hanley Soilsmith
    Jan 3 at 23:49


















0















I am probably not using the crypto module correctly, maybe someone can help me out.



The goal is to find the sha-256 hash of a file dropped in a dropzone. The problem is that the hash being returned is different from online hash checkers (which are returning the seemingly correct values). Here is my code:



const crypto = require("crypto");
const hash = crypto.createHash("sha256");

handleOnDrop = file => {
hash.update(file);
const hashOutput = hash.digest("hex");
console.log(hashOutput);
};


Crypto docs - https://nodejs.org/api/crypto.html#crypto_node_js_crypto_constants



I am fairly sure the hash values I am getting from this code is not just the file name, I checked a few permutations with the online checkers.



Any ideas? Thanks!










share|improve this question

























  • What is file?

    – Josh Lee
    Jan 3 at 22:08











  • And what is hash? How do you create it?

    – Styx
    Jan 3 at 22:29











  • File is input as a blob by the client (through react-dropzone). Hash is from the crypto docs, I edited the original post to include that. Thanks

    – Hanley Soilsmith
    Jan 3 at 23:49














0












0








0








I am probably not using the crypto module correctly, maybe someone can help me out.



The goal is to find the sha-256 hash of a file dropped in a dropzone. The problem is that the hash being returned is different from online hash checkers (which are returning the seemingly correct values). Here is my code:



const crypto = require("crypto");
const hash = crypto.createHash("sha256");

handleOnDrop = file => {
hash.update(file);
const hashOutput = hash.digest("hex");
console.log(hashOutput);
};


Crypto docs - https://nodejs.org/api/crypto.html#crypto_node_js_crypto_constants



I am fairly sure the hash values I am getting from this code is not just the file name, I checked a few permutations with the online checkers.



Any ideas? Thanks!










share|improve this question
















I am probably not using the crypto module correctly, maybe someone can help me out.



The goal is to find the sha-256 hash of a file dropped in a dropzone. The problem is that the hash being returned is different from online hash checkers (which are returning the seemingly correct values). Here is my code:



const crypto = require("crypto");
const hash = crypto.createHash("sha256");

handleOnDrop = file => {
hash.update(file);
const hashOutput = hash.digest("hex");
console.log(hashOutput);
};


Crypto docs - https://nodejs.org/api/crypto.html#crypto_node_js_crypto_constants



I am fairly sure the hash values I am getting from this code is not just the file name, I checked a few permutations with the online checkers.



Any ideas? Thanks!







node.js hash sha






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 23:49







Hanley Soilsmith

















asked Jan 3 at 22:06









Hanley SoilsmithHanley Soilsmith

18210




18210













  • What is file?

    – Josh Lee
    Jan 3 at 22:08











  • And what is hash? How do you create it?

    – Styx
    Jan 3 at 22:29











  • File is input as a blob by the client (through react-dropzone). Hash is from the crypto docs, I edited the original post to include that. Thanks

    – Hanley Soilsmith
    Jan 3 at 23:49



















  • What is file?

    – Josh Lee
    Jan 3 at 22:08











  • And what is hash? How do you create it?

    – Styx
    Jan 3 at 22:29











  • File is input as a blob by the client (through react-dropzone). Hash is from the crypto docs, I edited the original post to include that. Thanks

    – Hanley Soilsmith
    Jan 3 at 23:49

















What is file?

– Josh Lee
Jan 3 at 22:08





What is file?

– Josh Lee
Jan 3 at 22:08













And what is hash? How do you create it?

– Styx
Jan 3 at 22:29





And what is hash? How do you create it?

– Styx
Jan 3 at 22:29













File is input as a blob by the client (through react-dropzone). Hash is from the crypto docs, I edited the original post to include that. Thanks

– Hanley Soilsmith
Jan 3 at 23:49





File is input as a blob by the client (through react-dropzone). Hash is from the crypto docs, I edited the original post to include that. Thanks

– Hanley Soilsmith
Jan 3 at 23:49












1 Answer
1






active

oldest

votes


















1














Dropzone events return a File class object, this object is based on the Blob class and doesn't provide direct access to the data of the file. In order to use the data in the file, you must use the FileReader class as outlined in the Mozilla examples



Crypto is expecting a buffer when you call hash.update, but file isn't a Buffer like it would be in these examples. Dropping a Blob into hash.update probably doesn't have the behavior you are expecting.



So, assuming you're using WebPack to provide access to the Node standard libraries, your code should need to do something like this:



  handleOnDrop = ((file) => {
const reader = new FileReader();
reader.onload = ((event) => {
hash.update(Buffer.from(event.target.result));
const hashOutput = hash.digest("hex");
console.log(hashOutput);
});
reader.readAsArrayBuffer(file);
});





share|improve this answer
























  • Code is not working as is, getting an error Error: cannot read as File: [{}] Your response is very helpful though..I think I'm on the right path, thanks

    – Hanley Soilsmith
    Jan 4 at 0:18











  • Thank you so much for taking the time. The only modification is that I place reader.readAsArrayBuffer(file) on top of reader.onload My other issue was solved by creating a new create-react-app. Not sure why that helped.

    – Hanley Soilsmith
    Jan 5 at 9:14












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030452%2fnpm-crypto-returning-different-hash-values-than-online-generators-node%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Dropzone events return a File class object, this object is based on the Blob class and doesn't provide direct access to the data of the file. In order to use the data in the file, you must use the FileReader class as outlined in the Mozilla examples



Crypto is expecting a buffer when you call hash.update, but file isn't a Buffer like it would be in these examples. Dropping a Blob into hash.update probably doesn't have the behavior you are expecting.



So, assuming you're using WebPack to provide access to the Node standard libraries, your code should need to do something like this:



  handleOnDrop = ((file) => {
const reader = new FileReader();
reader.onload = ((event) => {
hash.update(Buffer.from(event.target.result));
const hashOutput = hash.digest("hex");
console.log(hashOutput);
});
reader.readAsArrayBuffer(file);
});





share|improve this answer
























  • Code is not working as is, getting an error Error: cannot read as File: [{}] Your response is very helpful though..I think I'm on the right path, thanks

    – Hanley Soilsmith
    Jan 4 at 0:18











  • Thank you so much for taking the time. The only modification is that I place reader.readAsArrayBuffer(file) on top of reader.onload My other issue was solved by creating a new create-react-app. Not sure why that helped.

    – Hanley Soilsmith
    Jan 5 at 9:14
















1














Dropzone events return a File class object, this object is based on the Blob class and doesn't provide direct access to the data of the file. In order to use the data in the file, you must use the FileReader class as outlined in the Mozilla examples



Crypto is expecting a buffer when you call hash.update, but file isn't a Buffer like it would be in these examples. Dropping a Blob into hash.update probably doesn't have the behavior you are expecting.



So, assuming you're using WebPack to provide access to the Node standard libraries, your code should need to do something like this:



  handleOnDrop = ((file) => {
const reader = new FileReader();
reader.onload = ((event) => {
hash.update(Buffer.from(event.target.result));
const hashOutput = hash.digest("hex");
console.log(hashOutput);
});
reader.readAsArrayBuffer(file);
});





share|improve this answer
























  • Code is not working as is, getting an error Error: cannot read as File: [{}] Your response is very helpful though..I think I'm on the right path, thanks

    – Hanley Soilsmith
    Jan 4 at 0:18











  • Thank you so much for taking the time. The only modification is that I place reader.readAsArrayBuffer(file) on top of reader.onload My other issue was solved by creating a new create-react-app. Not sure why that helped.

    – Hanley Soilsmith
    Jan 5 at 9:14














1












1








1







Dropzone events return a File class object, this object is based on the Blob class and doesn't provide direct access to the data of the file. In order to use the data in the file, you must use the FileReader class as outlined in the Mozilla examples



Crypto is expecting a buffer when you call hash.update, but file isn't a Buffer like it would be in these examples. Dropping a Blob into hash.update probably doesn't have the behavior you are expecting.



So, assuming you're using WebPack to provide access to the Node standard libraries, your code should need to do something like this:



  handleOnDrop = ((file) => {
const reader = new FileReader();
reader.onload = ((event) => {
hash.update(Buffer.from(event.target.result));
const hashOutput = hash.digest("hex");
console.log(hashOutput);
});
reader.readAsArrayBuffer(file);
});





share|improve this answer













Dropzone events return a File class object, this object is based on the Blob class and doesn't provide direct access to the data of the file. In order to use the data in the file, you must use the FileReader class as outlined in the Mozilla examples



Crypto is expecting a buffer when you call hash.update, but file isn't a Buffer like it would be in these examples. Dropping a Blob into hash.update probably doesn't have the behavior you are expecting.



So, assuming you're using WebPack to provide access to the Node standard libraries, your code should need to do something like this:



  handleOnDrop = ((file) => {
const reader = new FileReader();
reader.onload = ((event) => {
hash.update(Buffer.from(event.target.result));
const hashOutput = hash.digest("hex");
console.log(hashOutput);
});
reader.readAsArrayBuffer(file);
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 22:47









JayReardonJayReardon

515518




515518













  • Code is not working as is, getting an error Error: cannot read as File: [{}] Your response is very helpful though..I think I'm on the right path, thanks

    – Hanley Soilsmith
    Jan 4 at 0:18











  • Thank you so much for taking the time. The only modification is that I place reader.readAsArrayBuffer(file) on top of reader.onload My other issue was solved by creating a new create-react-app. Not sure why that helped.

    – Hanley Soilsmith
    Jan 5 at 9:14



















  • Code is not working as is, getting an error Error: cannot read as File: [{}] Your response is very helpful though..I think I'm on the right path, thanks

    – Hanley Soilsmith
    Jan 4 at 0:18











  • Thank you so much for taking the time. The only modification is that I place reader.readAsArrayBuffer(file) on top of reader.onload My other issue was solved by creating a new create-react-app. Not sure why that helped.

    – Hanley Soilsmith
    Jan 5 at 9:14

















Code is not working as is, getting an error Error: cannot read as File: [{}] Your response is very helpful though..I think I'm on the right path, thanks

– Hanley Soilsmith
Jan 4 at 0:18





Code is not working as is, getting an error Error: cannot read as File: [{}] Your response is very helpful though..I think I'm on the right path, thanks

– Hanley Soilsmith
Jan 4 at 0:18













Thank you so much for taking the time. The only modification is that I place reader.readAsArrayBuffer(file) on top of reader.onload My other issue was solved by creating a new create-react-app. Not sure why that helped.

– Hanley Soilsmith
Jan 5 at 9:14





Thank you so much for taking the time. The only modification is that I place reader.readAsArrayBuffer(file) on top of reader.onload My other issue was solved by creating a new create-react-app. Not sure why that helped.

– Hanley Soilsmith
Jan 5 at 9:14




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030452%2fnpm-crypto-returning-different-hash-values-than-online-generators-node%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Angular Downloading a file using contenturl with Basic Authentication

Monofisismo

Olmecas