class DropboxContentHasher::Calculator

Constants

BLOCK_SIZE

Public Class Methods

new(path) click to toggle source
# File lib/dropbox_content_hasher/calculator.rb, line 7
def initialize(path)
  @file = Pathname.new(path)
end

Public Instance Methods

content_hash() click to toggle source
# File lib/dropbox_content_hasher/calculator.rb, line 11
def content_hash
  raise FileDoesNotExist unless @file.exist?

  Digest::SHA256.hexdigest(hashes.join)
end

Private Instance Methods

blocks_count() click to toggle source
# File lib/dropbox_content_hasher/calculator.rb, line 25
def blocks_count
  (@file.size / BLOCK_SIZE).ceil
end
hashes() click to toggle source
# File lib/dropbox_content_hasher/calculator.rb, line 19
def hashes
  Array.new(blocks_count) do |i|
    Digest::SHA256.digest(@file.binread(BLOCK_SIZE, BLOCK_SIZE * i))
  end
end