[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Digest::Base

Brad Tilley

3/20/2006 1:27:00 PM

I wish to write one method to work with all classes in Digest::Base
without repeatedly writing the same code.

def setup(file, hash_type)

# hash_type could be MD5, SHA1, SHA256, etc.

Digest::hash_type.new
Hash the file.

end

The above example is what I'd like to be able to do, but it does not
work. I cannot insert 'hash_type' into the method in this manner. Trying
to do so produces errors. I can do a bunch of conditionals like this:

def setup(file, hash_type)

if hash_type == MD5
Digest::MD5.new
Hash the file.

elsif hash_type == RMD160
Digest::RMD160.new
Hash the file.

else....
....

end

But then, I begin repeating myself. Any tips on making this work with
fewer lines of code?

Thanks!
4 Answers

Tim Hunter

3/20/2006 1:51:00 PM

0

Check out Module#const_get in ri:

Digest.const_get('MD5').new

Brad Tilley

3/20/2006 1:54:00 PM

0

rmagick@gmail.com wrote:
> Check out Module#const_get in ri:
>
> Digest.const_get('MD5').new

That's great... Thanks for the tip!

Daniel Harple

3/20/2006 1:56:00 PM

0

On Mar 20, 2006, at 2:28 PM, rtilley wrote:
> I wish to write one method to work with all classes in Digest::Base
> without repeatedly writing the same code.

How about this:

$ cat digest.rb
require 'digest/md5'
require 'digest/sha1'
require 'digest/sha2'
require 'digest/rmd160'

def hash_file(file, hash_kind)
digest = Digest.const_get(hash_kind.to_s.upcase).new
open(file, 'rb') do |f|
while (chunk = f.read(1))
digest.update(chunk)
end
end
digest
end

puts "RMD160: #{hash_file(__FILE__, :rmd160)}"
puts "MD5: #{hash_file(__FILE__, :md5)}"
puts "SHA1: #{hash_file(__FILE__, :sha1)}"
puts "SHA256: #{hash_file(__FILE__, :sha256)}"

$ ruby digest.rb
RMD160: a99c2c1919b289412a98dc4562598d904e2a5750
MD5: 0a35b2d64cc5201314b691f3c8588ac6
SHA1: f47b890ec66801e1f24956214d8a6f13c7f8daac
SHA256: bda670c53c5ff548a88f274ce17493adc0c229e32c221990e0c779c881425886

-- Daniel




Simon Kröger

3/20/2006 2:21:00 PM

0

Daniel Harple schrieb:
> On Mar 20, 2006, at 2:28 PM, rtilley wrote:
>> I wish to write one method to work with all classes in Digest::Base
>> without repeatedly writing the same code.
>
> How about this:
>
> $ cat digest.rb
> require 'digest/md5'
> require 'digest/sha1'
> require 'digest/sha2'
> require 'digest/rmd160'
>
> def hash_file(file, hash_kind)
> digest = Digest.const_get(hash_kind.to_s.upcase).new
> open(file, 'rb') do |f|
> while (chunk = f.read(1))
> digest.update(chunk)
> end
> end
> digest
> end
>
> puts "RMD160: #{hash_file(__FILE__, :rmd160)}"
> puts "MD5: #{hash_file(__FILE__, :md5)}"
> puts "SHA1: #{hash_file(__FILE__, :sha1)}"
> puts "SHA256: #{hash_file(__FILE__, :sha256)}"
>
> $ ruby digest.rb
> RMD160: a99c2c1919b289412a98dc4562598d904e2a5750
> MD5: 0a35b2d64cc5201314b691f3c8588ac6
> SHA1: f47b890ec66801e1f24956214d8a6f13c7f8daac
> SHA256: bda670c53c5ff548a88f274ce17493adc0c229e32c221990e0c779c881425886
>
> -- Daniel

Quite nice. However i think const_get isn't necessary here and should be
avoided if possible. e.g.:

def hash_file(file, hash_kind)
digest, chunk = hash_kind.new, ''
open(file, 'rb') do |f|
digest << chunk while f.read(4096, chunk)
end
digest
end

puts "RMD160: #{hash_file(__FILE__, Digest::RMD160)}"
puts "MD5: #{hash_file(__FILE__, Digest::MD5)}"
puts "SHA1: #{hash_file(__FILE__, Digest::SHA1)}"
puts "SHA256: #{hash_file(__FILE__, Digest::SHA256)}"

cheers

Simon