2.35. Calculating the MD5 Hash of a String

The MD5 message-digest algorithm produces a 128-bit fingerprint or message digest of a message of arbitrary length. This is in the form of a hash, so the encryption is one-way and does not allow for the discovery of the original message from the digest. Ruby has an extension for a class to implement MD5; for those interested in the source code, it’s in the ext/md5 directory of the standard Ruby distribution.

There are two class methods, new and md5, to create a new MD5 object. There is really no difference in them:

require 'md5'
hash = MD5.md5
hash = MD5.new

There are four instance methods: clone, digest, hexdigest, and update. The clone method simply copies the object; update is used to add content ...

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.