Maintenance of Ruby 2.0.0 ended on February 24, 2016. Read more

In Files

  • digest/digest.c
  • digest/lib/digest.rb
  • digest/lib/digest/hmac.rb
  • digest/md5/md5init.c
  • digest/rmd160/rmd160init.c
  • digest/sha1/sha1init.c
  • digest/sha2/lib/sha2.rb




This module provides a framework for message digest libraries.

You may want to look at OpenSSL::Digest as it supports support more algorithms.

A cryptographic hash function is a procedure that takes data and return a fixed bit string : the hash value, also known as digest. Hash functions are also called one-way functions, it is easy to compute a digest from a message, but it is infeasible to generate a message from a digest.


require 'digest'

# Compute a complete digest
sha256 =
digest = sha256.digest message

# Compute digest by chunks
sha256 =
sha256.update message1
sha256 << message2 # << is an alias for update

digest = sha256.digest

Digest algorithms

Different digest algorithms (or hash functions) are available :


See FIPS PUB 198 The Keyed-Hash Message Authentication Code (HMAC)


(as Digest::RMD160) see


See FIPS 180 Secure Hash Standard

SHA2 family

See FIPS 180 Secure Hash Standard which defines the following algorithms:

  • SHA512

  • SHA384

  • SHA256

The latest versions of the FIPS publications can be found here:

Additionally Digest::BubbleBabble encodes a digest as a sequence of consonants and vowels which is more recognizable and comparable than a hexadecimal digest. See

Public Class Methods

hexencode(string) → hexencoded_string click to toggle source

Generates a hex-encoded version of a given string.

               static VALUE
rb_digest_s_hexencode(VALUE klass, VALUE str)
    return hexencode_str_new(str);