Rails:上传文件的校验和
我正在 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 技术交流群。
发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您是否比较过“data1”和“data2”的内容?尝试将它们保存到文件中并查看。
我想,您可能想在第一次读取之前调用
upload.rewind
,但第一件事是查看从文件中读取的原始数据。更新:
您没有说您使用的是 Windows。在这种情况下,您应该小心并以所谓的“二进制”模式读取文件。
将
File.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:(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.