Class: Digestif::Hasher

Inherits:
Object
  • Object
show all
Defined in:
lib/digestif/hasher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = CLI.default_options) ⇒ Hasher

Returns a new instance of Hasher.



11
12
13
14
# File 'lib/digestif/hasher.rb', line 11

def initialize(filename, options = CLI.default_options)
  self.filename = filename
  self.options = options
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



9
10
11
# File 'lib/digestif/hasher.rb', line 9

def filename
  @filename
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/digestif/hasher.rb', line 9

def options
  @options
end

Instance Method Details

#digestObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/digestif/hasher.rb', line 16

def digest
  hasher = Digest.const_get(options.digest.to_s.upcase).new
  sample_count = 0

  File.open(filename, 'rb') do |f|
    until f.eof
      hasher.update(f.read(options.read_size))
      f.seek(options.seek_size, IO::SEEK_CUR)
      sample_count += 1
    end
  end
  if options.print_sample_count
    puts "#{filename}\t#{File.size(filename)}b\t#{sample_count} samples"
  end
  hasher.hexdigest
end