Rails:上传文件的校验和

发布于 11-16 22:30 字数 895 浏览 2 评论 0原文

我正在 Ruby on Rails 中生成上传图像的校验和 (sha256)。

upload = params[:file]
data1 = upload.read
data2 = File.read(upload.tempfile)
checksum1 = Digest::SHA256.hexdigest(data1)
checksum2 = Digest::SHA256.hexdigest(data2)
puts checksum1
puts checksum2

最后两个语句返回不同的值。 checksum1 是通过使用 UploadedFile 对象读取数据生成的。 checksum2是通过从文件系统读取临时文件生成的。

ActionDispatch::Http::UploadedFile 的对象是否返回除上传文件内容之外的任何内容?当我生成写入文件系统的上传文件的校验和时,它与 checksum2 (临时文件校验和)匹配,而不是与 checksum1 (UploadedFile.read) 匹配。

我假设通过从文件系统读取临时文件生成的校验和更可靠,因为对象(UploadedFile)实现可能会改变。如果需要,可以更轻松地生成文件系统上现有文件的校验和。

那么,到底是什么原因导致校验和不同,哪一种更可靠呢?

谢谢。


更新1: 根据@pablo-castellazzi的建议,我使用 Digest::SHA256.file(upload.path).hexdigest 生成了哈希值。让我们称之为 checksum3

这个 checksum3 等于 checksum1 但与 checksum2 不同


更新 2:如果我使用二进制模式读取 @Arsen7 提到的文件,那么所有校验和都是相等的。

I am generating the checksum (sha256) of an uploaded image in Ruby on Rails.

upload = params[:file]
data1 = upload.read
data2 = File.read(upload.tempfile)
checksum1 = Digest::SHA256.hexdigest(data1)
checksum2 = Digest::SHA256.hexdigest(data2)
puts checksum1
puts checksum2

Last two statements are returning different values.
checksum1 is generated by reading the data using the UploadedFile object.
checksum2 is generated by reading the temporary file from the file system.

Does an object of ActionDispatch::Http::UploadedFile return anything more than the contents of the uploaded file? When I generate the checksum of the uploaded file written to the file-system, it is matching with checksum2 (temporary file checksum) , not with checksum1 (UploadedFile.read).

I am assuming that the checksum generated by reading the temporary file from the filesystem is more reliable as the object (UploadedFile) implementation might change. If needed, it will be easier to generate checksums of existing files on the file system.

So, what is the reason for the difference of checksums and which one is more reliable?

Thank you.


Update 1:
As per @pablo-castellazzi suggestion i generated the hash by using Digest::SHA256.file(upload.path).hexdigest . Let us call this checksum3

This checksum3 equals checksum1 but differs from checksum2


Update 2: If i use the binary mode to read the file as mentioned by @Arsen7 , then all the checksums are equal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡大本事2024-11-23 22:30:24

您是否比较过“data1”和“data2”的内容?尝试将它们保存到文件中并查看。

我想,您可能想在第一次读取之前调用 upload.rewind,但第一件事是查看从文件中读取的原始数据。

更新:

您没有说您使用的是 Windows。在这种情况下,您应该小心并以所谓的“二进制”模式读取文件。

File.read 方法更改为如下所示:(

data2 = nil
File.open(upload.path, "rb") {|f| data2 = f.read }

实现 Pablo Castellazzi 使用 .path 方法的建议)

我建议您在某些二进制安全编辑器中打开文件(例如 vim)并比较不同之处。您会注意到,也许大多数数据是相同的,但其中一个文件的行结尾不同,或者您可能会发现一些其他差异。

对于 Windows,最常见的问题是二进制模式。

Have you compared the 'data1' and 'data2' contents? Try to save them to files and take a look.

I suppose, you may want to call upload.rewind before you do the first read, but the first thing is to take a look at the raw data read from the files.

Update:

You did not say that you are on Windows. In this case you should take care and read the files in so-called 'binary' mode.

Change the File.read method to something like this:

data2 = nil
File.open(upload.path, "rb") {|f| data2 = f.read }

(Implement Pablo Castellazzi suggestion of using .path method)

I was suggesting that you open the files in some binary-safe editor (vim, for example) and compare what differs. You would notice that maybe most of the data is the same, but in one of the files line endings are different, or maybe you would spot some other differences.

In case of Windows, the most popular problem is the binary mode.

只是偏爱你2024-11-23 22:30:24

假设您使用 Rails 3.x data1 校验和是正确的。 data2 内容应读取为:

data2 = File.read(upload.path)

upload.tempfile 是保存文件对象的实例,而不是临时文件的路径。

这里是相关的实现细节。

这也很奇怪,因为 File.read(File.read) 应该抛出某种有关未找到文件或无效文件名的异常。

Assuming you are using Rails 3.x data1 checksum is correct. data2 content should be read with:

data2 = File.read(upload.path)

upload.tempfile is the instance holding the file object, not the path to the temporary file.

Here are the relevant implementation details.

Also this is weird, because File.read(File.read) should throw some kind of exception about a file not found or an invalid file name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文