module Text::Checkm

Constants

CHUNK_SIZE

Size (in bytes) to read (in chunks) to compute checksums

Public Class Methods

checksum(file, alg) click to toggle source

Compute the checksum 'alg' for a file @param [File] file @param [String] alg md5, sha1, sha256, dir

# File lib/text/checkm/checksum.rb, line 12
def checksum(file, alg) # TODO: don't pass file handles around
  return true unless alg # TODO: something less counterintuitive
  return File.directory?(file) if alg =~ /dir/

  digest_alg = digest_for(alg)
  return false unless digest_alg # TODO: something less counterintuitive

  while !file.eof? && (chunk = file.readpartial(CHUNK_SIZE))
    digest_alg << chunk
  end
  digest_alg.hexdigest
end

Private Class Methods

digest_for(alg) click to toggle source
# File lib/text/checkm/checksum.rb, line 27
def digest_for(alg)
  case alg
  when /md5/
    Digest::MD5.new
  when /sha1/
    Digest::SHA1.new
  when /sha256/
    Digest::SHA2.new(256)
  end
end