Module: Safeguard::Digest

Defined in:
lib/safeguard/digest.rb,
lib/safeguard/digest/crc32.rb

Overview

Encapsulates checksum computation for files using a set of hash functions.

Defined Under Namespace

Classes: CRC32

Constant Summary collapse

SUPPORTED_ALGORITHMS =
algorithms.keys.freeze

Class Method Summary collapse

Class Method Details

.digest_with(algorithm, filename) ⇒ Object

Digests a file using an algorithm.

Algorithms are expected include the Digest::Instance module.

OpenSSL::Digest::SHA1.include? Digest::Instance
=> true


32
33
34
35
36
37
38
39
40
# File 'lib/safeguard/digest.rb', line 32

def digest_with(algorithm, filename)
  digest = algorithm.new
  File.open(filename, 'rb') do |file|
    file.each_chunk do |chunk|
      digest << chunk
    end
  end
  digest.hexdigest!
end

.file(file, hash_function = :sha1) ⇒ Object

Digests a file using a hash function, which can be the symbol of any Digest module method that takes a file. Uses SHA1 by default.

Safeguard::Digest.file(file, :md5)

Is equivalent to:

Safeguard::Digest.md5(file)


50
51
52
53
54
55
56
# File 'lib/safeguard/digest.rb', line 50

def file(file, hash_function = :sha1)
  hash_function = hash_function.to_sym
  unless respond_to? hash_function
    raise ArgumentError, "Unsupported hash function: #{hash_function}"
  end
  send hash_function, file
end