# Digest::Base

**URL:** <https://www.ruby-forum.com/t/digest-base/84152>\
**Category:** Ruby\
**Created:** [March 20, 2006, 2:29pm UTC](https://www.ruby-forum.com/t/digest-base/84152 "2006-03-20T14:29:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 20, 2006, 2:29pm UTC](https://www.ruby-forum.com/t/digest-base/84152/1 "2006-03-20T14:29:03Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 20, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/digest-base/84152/2 "2006-03-20T14:53:55Z")

</div>

Check out Module#const\_get in ri:

Digest.const\_get(‘MD5’).new

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 20, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/digest-base/84152/3 "2006-03-20T14:56:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 20, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/digest-base/84152/4 "2006-03-20T15:20:59Z")

</div>

Daniel H. schrieb:

> require ‘digest/rmd160’
> 
> – 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

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 20, 2006, 2:58pm UTC](https://www.ruby-forum.com/t/digest-base/84152/5 "2006-03-20T14:58:58Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Check out Module#const\_get in ri:
> 
> Digest.const\_get(‘MD5’).new

That’s great… Thanks for the tip!
