Class: Longleaf::DigestHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/longleaf/helpers/digest_helper.rb

Overview

Helper methods for generating digests

Class Method Summary collapse

Class Method Details

.is_known_algorithm?(alg) ⇒ Boolean

Returns true if the digest is a valid known algorithm.

Parameters:

  • alg (String)

    identifier of digest algorithm

Returns:

  • (Boolean)

    true if the digest is a valid known algorithm



29
30
31
# File 'lib/longleaf/helpers/digest_helper.rb', line 29

def self.is_known_algorithm?(alg)
  KNOWN_DIGESTS.include?(alg)
end

.start_digest(alg) ⇒ Digest

Get a Digest class for the specified algorithm

Parameters:

  • alg (String)

    name of the digest algorithm

Returns:

  • (Digest)

    A digest class for the requested algorithm

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/longleaf/helpers/digest_helper.rb', line 37

def self.start_digest(alg)
  case alg
  when 'md5'
    return Digest::MD5.new
  when 'sha1'
    return Digest::SHA1.new
  when 'sha2', 'sha256'
    return Digest::SHA2.new
  when 'sha384'
    return Digest::SHA2.new(384)
  when 'sha512'
    return Digest::SHA2.new(512)
  when 'rmd160'
    return Digest::RMD160.new
  else
    raise InvalidDigestAlgorithmError.new("Cannot produce digest for unknown algorithm '#{alg}'.")
  end
end

.validate_algorithms(algs) ⇒ Object

Parameters:

  • algs

    Either a string containing one or an array containing zero or more digest algorithm names.

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/longleaf/helpers/digest_helper.rb', line 13

def self.validate_algorithms(algs)
  return if algs.nil?
  if algs.is_a?(String)
    unless self.is_known_algorithm?(algs)
      raise InvalidDigestAlgorithmError.new("Unknown digest algorithm #{algs}")
    end
  else
    unknown = algs.select { |alg| !KNOWN_DIGESTS.include?(alg) }
    unless unknown.empty?
      raise InvalidDigestAlgorithmError.new("Unknown digest algorithm(s): #{unknown}")
    end
  end
end