From: mike@... (Mike Stok) Date: 2004-01-28T01:59:55+09:00 Subject: Re: checksum with ruby, how? In article <40168bd4$0$322$ba620e4c@news.skynet.be>, shasckaw wrote: >Hello there, I need help. >I'm searching for checksum funtionality to use with ruby, >it seems that I have all I need with digest/md5 but I can't find any >documentation about how to use it (searched on ruby-doc and RAA) > >Can someone help me? To set a baseline, I used md5sum to checksum "hello there\n" [mike@won mike]$ echo "hello there" > test.txt [mike@won mike]$ md5sum test.txt 2d01d5d9c24034d54fe4fba0ede5182d test.txt I used irb and some guesswork to figure out its methods, the guess being that Digest::MD5 was the right package: [mike@ratdog mike]$ irb >> require 'digest/md5' => true >> d = Digest::MD5.new => d41d8cd98f00b204e9800998ecf8427e >> d.methods - Object.methods => ["<<", "digest", "hexdigest", "update"] So now I see what I can do, let's try updating the empty digest, pick one of update or << ... >> d << "hello there\n" => 2d01d5d9c24034d54fe4fba0ede5182d Looks familiar :-) >> hex = d.hexdigest => "2d01d5d9c24034d54fe4fba0ede5182d" So you can use ruby's reflection to get a crude handle on things, but documentation would say whether you can add content in the constructor: >> d2 = Digest::MD5.new("hello there\n") => 2d01d5d9c24034d54fe4fba0ede5182d It seems that you can initialise it with any String, so >> File.open('/etc/passwd') do |f| ?> puts Digest::MD5.new(f.read).hexdigest >> end f087dea38c54a8b40d3dbc8b0becfb4c => nil or >> d3 = Digest::MD5.new => d41d8cd98f00b204e9800998ecf8427e >> File.new('/etc/passwd').each_line { |l| d3 << l } => # >> d3.hexdigest => "f087dea38c54a8b40d3dbc8b0becfb4c" are reasonable ways to checksum a file (at least on linux where I don't care about binary mkode...) Hope this helps, Mike -- mike@stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA