checksum with ruby, how?

S

shasckaw

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?

Lio
 
J

Joel VanderWerf

shasckaw said:
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?

Basic usage is easy:

$ irb -r digest/md5
irb(main):001:0> Digest::MD5.digest("some string")
=> "Z\307I\373\356\3116\a\374(\326f\276\205\347:"
irb(main):002:0> Digest::MD5.hexdigest("some string")
=> "5ac749fbeec93607fc28d666be85e73a"

so you have a choice between the binary string or the equivalent ASCII
hex string (use the latter in a text doc).
 
M

Mike Stok

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=> ["<<", "digest", "hexdigest", "update"]

So now I see what I can do, let's try updating the empty digest, pick
one of update or << ...
=> 2d01d5d9c24034d54fe4fba0ede5182d

Looks familiar :)
=> "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:
=> 2d01d5d9c24034d54fe4fba0ede5182d

It seems that you can initialise it with any String, so
f087dea38c54a8b40d3dbc8b0becfb4c
=> nil

or
=> "f087dea38c54a8b40d3dbc8b0becfb4c"

are reasonable ways to checksum a file (at least on linux where I don't
care about binary mkode...)

Hope this helps,

Mike
 
S

shasckaw

Mike said:
shasckaw said:
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:
Your guesswork has really been instructive in itself. Thanks a lot.
[snip]
It seems that you can initialise it with any String, sof087dea38c54a8b40d3dbc8b0becfb4c
=> nil
This is exactly what I needed.
or
=> "f087dea38c54a8b40d3dbc8b0becfb4c"

are reasonable ways to checksum a file (at least on linux where I don't
care about binary mkode...)
I'm using ruby on winNT. I've tested the code above and it gives exactly
the same result, work fine then! :)
Hope this helps,
This helped me a lot, thanks. And thanks to all other too.
Lio
 
D

daz

For the archive (and the 'doc. squad') --

This is in the source distribution:
(ext/digest/digest.txt)

Can it be made more visible ?
-------------------------------------------------------


** MD5(Class)

A class to implement the MD5 Message-Digest Algorithm by RSA Data
Security, Inc., described in RFC1321.

Superclass: Digest::Base

require 'digest/md5'

** SHA1(Class)

A class to implement the SHA-1 Secure Hash Algorithm by NIST (the US'
National Institute of Standards and Technology), described in FIPS PUB
180-1.

Superclass: Digest::Base

require 'digest/sha1'

** SHA256(Class)
** SHA384(Class)
** SHA512(Class)

Classes to implement the SHA-256/384/512 Secure Hash Algorithm(s) by
NIST (the US' National Institute of Standards and Technology),
described in FIPS PUB 180-2.

Superclass: Digest::Base

require 'digest/sha2'

** RMD160(Class)

A class to implement the RIPEMD-160 cryptographic hash function,
designed by Hans Dobbertin, Antoon Bosselaers, and Bart Preneel.

Superclass: Digest::Base

require 'digest/rmd160'


Those above classes provide a common interface as shown below.


Class Methods:

new([str])

Creates a new digest object. If a string argument is given,
it is added to the object. (see update.)

digest(str)

Immediately calculates and return the hash of the given
strings as a string. Equivalent to new(str).digest.

hexdigest(str)

Immediately calculates and return the hash of the given
strings as a string of hexadecimal digits. Equivalent to
new(str).hexdigest.

Methods:

clone

Creates a copy of the digest object.

digest

Returns the hash of the added strings as a string of 16 bytes
for MD5, 20 bytes for SHA1 and RMD160, 32 bytes for SHA256, 48
bytes for SHA384, and 64 bytes for SHA512.

hexdigest
to_s

Returns the hash of the added strings as a string of 32
hexadecimal digits for MD5, 40 hexadecimal digits for SHA1 and
RMD160, 64 hexadecimal digits for SHA256, 96 hexadecimal
digits for SHA384, and 128 hexadecimal digits for SHA512.
This method is equal to:

def hexdigest
digest.unpack("H*")[0]
end

update(str)
<< str

Appends the string str to the digest object. Repeated calls
are equivalent to a single call with the concatenation of all
the arguments, i.e. m.update(a); m.update(b) is equivalent to
m.update(a + b) and m << a << b is equivalent to m << a + b.

== md

Checks if the object is equal to the given digest object.

== str

Regards the value as either a digest value or a hexdigest
value (depending on the length) and checks if the object is
equal to the given string.

-------------------------------------------------------
 
G

Gavin Sinclair

For the archive (and the 'doc. squad') --

This is in the source distribution:
(ext/digest/digest.txt)

Can it be made more visible ?

The standard library documentation at http://ruby-doc.org/stdlib does not
yet include the "ext" stuff. That's a pretty high priority for me.

You might have some luck coaxing 'ri' into knowing about digest, stringio,
etc. Or maybe it already does :)

Gavin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top