Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6048383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:27:25+00:00 2026-05-23T07:27:25+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:27:26+00:00Added an answer on May 23, 2026 at 7:27 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After generating / writing / running a Rails database migration, should the migration file
When generating a controller, rails automatically put 'get controller/action' at top of routes.rb file
Generating normal columnar data in excel file is quite easy but does any one
Im generating a file to present to the user for download, but the server
I'm generating json from a Rails app, and am filtering fields using the format.json
I am generating pdf using java and itext. When I add image that we
Generating a scaffold like this: rails generate scaffold User name:string email:string creates bits of
Im generating a report from a template html file in my program. It resides
Upon generating a Vistual Studio project from a CMake build file, CMake generates a
I'm trying to create a checksum of a binary file (flv/f4v, etc) to verify

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.