简体   繁体   English

是否可以使用 Javascript 计算文件的 SHA1 ID?

[英]Is it possible to compute a file's SHA1 ID using Javascript?

If this were possible to do prior to posting a form, it may save me having to upload the file to my server...如果可以在发布表格之前这样做,它可能会节省我将文件上传到我的服务器的时间......

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.实际上,您现在可以读取客户端文件的内容,只要在文件上传字段中选择它并且您使用的是 Firefox。 See the input.files array.请参阅input.files数组。 You can then indeed hash it, although it might be rather slow.然后你确实可以 hash 它,虽然它可能会很慢。

See How would I get a Hash value of a users file with Javascript or Flash?请参阅如何使用 Javascript 或 Flash 获得用户文件的 Hash 值? for an example and a compact SHA-1 implementation.示例和紧凑的 SHA-1 实现。

One can read their local file using the HTML5 File interface: https://developer.mozilla.org/en-US/docs/Web/API/File可以使用 HTML5 文件接口读取他们的本地文件: 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.然后,您可以使用 Crypto.js https://code.google.com/p/crypto-js/之类的库来完成 hash 阅读文本。

To do that you would have to load the file's binary information into JavaScript.为此,您必须将文件的二进制信息加载到 JavaScript 中。 Which is not possible.这是不可能的。

But here's an implementation of SHA1 in JavaScript.但这是 JavaScript 中 SHA1实现。

It is possible to use SHA1, though performance isn't going to be the best...可以使用 SHA1,但性能不会是最好的......

For anything over a few hundred KB's you will have to run some benchmarks and determine if indeed its a viable solution.对于超过几百 KB 的任何内容,您都必须运行一些基准测试并确定它是否确实是一个可行的解决方案。

See this link for a good implementation (passpack and quite a few OS projects use it)请参阅此链接以获得良好的实现(passpack 和相当多的 OS 项目使用它)

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.编辑:正如其他人已经回答的那样,实际获取文件内容可能是完全不同的事情 - 所以除非您使用 Google Gears 或 Adobe AIR 之类的东西,否则几乎不可能。

No, you can't access a file from a local computer using JavaScript.不,您不能使用 JavaScript 从本地计算机访问文件。

You're going to have to upload it first to the server, then checking the checksum of the file.您必须先将其上传到服务器,然后检查文件的校验和。

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.文件中的每个字节都必须由 Javascript 加载到 memory 中,并且您需要一种方法将其加载到那里。

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.例如,这是一个

You could do this with a Java applet.您可以使用 Java 小程序来执行此操作。 I've never used any of them, but there are quite a few Java upload applets out there.我从来没有使用过它们中的任何一个,但是那里有很多 Java 上传小程序。 The hash algorithm itself is available with Java and can be accessed through java.security.MessageDigest. hash 算法本身与 Java 一起可用,可以通过 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.如果客户端没有可用的 Java 插件,您可以故障回复到常规上传和服务器上的 hash。

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.附注:根据您对文件进行哈希处理的原因,您可能希望在上传后在服务器上重新对其进行哈希处理,而不是信任客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM