Module: Digest

Defined in:
digest.c,
lib/digest.rb,
sha2/lib/sha2.rb,
lib/digest/hmac.rb

Overview

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.

Example

require 'digest'

# Compute a complete digest
sha256 = Digest::SHA256.new
digest = sha256.digest message

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

digest = sha256.digest

Digest algorithms

Different digest algorithms (or hash functions) are available :

HMAC

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

RIPEMD-160

(as Digest::RMD160) see homes.esat.kuleuven.be/~bosselae/ripemd160.html

SHA1

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: csrc.nist.gov/publications/PubsFIPS.html

Additionally Digest::BubbleBabble encodes a digest as a sequence of consonants and vowels which is more recognizable and comparable than a hexadecimal digest. See en.wikipedia.org/wiki/Bubblebabble

Defined Under Namespace

Modules: Instance Classes: Base, Class, HMAC, MD5, RMD160, SHA1, SHA2

Class Method Summary collapse

Class Method Details

.bubblebabble(string) ⇒ Object

Returns a BubbleBabble encoded version of a given string.



87
88
89
90
91
# File 'bubblebabble/bubblebabble.c', line 87

static VALUE
rb_digest_s_bubblebabble(VALUE klass, VALUE str)
{
    return bubblebabble_str_new(str);
}

.const_missing(name) ⇒ Object

:nodoc:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/digest.rb', line 4

def self.const_missing(name) # :nodoc:
  case name
  when :SHA256, :SHA384, :SHA512
    lib = 'digest/sha2.so'
  else
    lib = File.join('digest', name.to_s.downcase)
  end

  begin
    require lib
  rescue LoadError
    raise LoadError, "library not found for class Digest::#{name} -- #{lib}", caller(1)
  end
  unless Digest.const_defined?(name)
    raise NameError, "uninitialized constant Digest::#{name}", caller(1)
  end
  Digest.const_get(name)
end

.hexencode(string) ⇒ Object

Generates a hex-encoded version of a given string.



120
121
122
123
124
# File 'digest.c', line 120

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