If this were possible to do prior to posting a form, it may save me having to upload the file to my server...
-
possible duplicate of [How would I get a Hash value of a users file with Javascript or Flash?](http://stackoverflow.com/questions/539488/how-would-i-get-a-hash-value-of-a-users-file-with-javascript-or-flash) – cHao Oct 29 '11 at 22:42
-
It may help.: [How can I hash a string with SHA256 in JS?](https://stackoverflow.com/a/68545495/9935654) support `SHA-1`, `SHA-256`, `SHA-384`, `SHA-512` – Carson Feb 05 '22 at 11:17
7 Answers
To do that you would have to load the file's binary information into JavaScript. Which is not possible.
But here's an implementation of SHA1 in JavaScript.

- 68,817
- 22
- 142
- 198
Actually you can read the contents of a client-side file now, as long as it's chosen in a file upload field and you are using Firefox. See the input.files array. You can then indeed hash it, although it might be rather slow.
See How would I get a Hash value of a users file with Javascript or Flash? for an example and a compact SHA-1 implementation.
One can read their local file using the HTML5 File interface: https://developer.mozilla.org/en-US/docs/Web/API/File
And then you can use a library for like Crypto.js https://code.google.com/p/crypto-js/ to finish the hash over the read text.

- 729
- 5
- 2
It is possible to use SHA1, though performance isn't going to be the best...
For anything over a few hundred KB's you will have to run some benchmarks and determine if indeed its a viable solution.
See this link for a good implementation (passpack and quite a few OS projects use it)
Edit: As other have already replied, actually getting the file contents may be a whole different matter - so unless you use something like Google Gears or Adobe AIR it should be virtually impossible.

- 4,442
- 2
- 22
- 25
No, you can't access a file from a local computer using JavaScript .
You're going to have to upload it first to the server, then checking the checksum of the file.

- 227
- 3
- 9
Not natively, no, and this is a bad idea anyway. Every byte in the file will have to be loaded into memory by Javascript, and you'd need a way to get it there.
If you must do this and you've got a way to put the file's binary information into your script, then there's plenty of third-party scripts you can use. Here's one, for example.

- 303,634
- 46
- 339
- 357
-
Second link is broken. Fixed link with sha1: http://pajhome.org.uk/crypt/md5/index.html – loentar Mar 01 '14 at 16:11
-
@JohnFeminella thx for your comment: would you know how these guys calculating SHAs: https://md5file.com/calculator – BenKoshy Jul 03 '20 at 22:18
You could do this with a Java applet. I've never used any of them, but there are quite a few Java upload applets out there. The hash algorithm itself is available with Java and can be accessed through java.security.MessageDigest. If the client doesn't have the Java plug-in available you could just fail back to a regular upload and hash on the server.
A side note: depending upon why you're hashing the file you'll probably want to re-hash it on the server after the upload rather than trust the client.

- 1,980
- 1
- 14
- 10