avatarmonkeykirby
avatarmonkeykirby

Reputation: 339

Ruby Hash Algorithm

I need a hash algorithm while using Ruby. In my situation I'm comparing the contents of the file. I was using MD5, but it examines the filename as well (or seems to anyway). Is there an algorithm that I can easily implement or will I have to write one from scratch?

Upvotes: 0

Views: 638

Answers (3)

Adjam
Adjam

Reputation: 640

This is similar to the answer above but uses SHA256, as MD5 was broken by rainbow tables if I recall correctly

require 'digest'

puts "Hello!"
puts Digest::SHA256.hexdigest 'message'
puts Digest::SHA256.hexdigest 'message2'

Upvotes: 0

Slartibartfast
Slartibartfast

Reputation: 8805

I'm not sure why do you think it compares filename?

require "digest"
Digest::MD5.hexdigest(File.read('file1'))
=> "60b725f10c9c85c70d97880dfe8191b3"
Digest::MD5.hexdigest(File.read('file2'))
=> "60b725f10c9c85c70d97880dfe8191b3"

What did you do to get different checksums?

Upvotes: 2

Pedro Nascimento
Pedro Nascimento

Reputation: 13886

Use FileUtils.compare_file.

require 'fileutils'
FileUtils.compare_file('somefile', 'somefile')  #=> true

Upvotes: 2

Related Questions