Sha256: ead7f649dab91fec62e012ddde0dded8d55bcd99fbb02228741144145e416420

Contents?: true

Size: 1.13 KB

Versions: 18

Compression:

Stored size: 1.13 KB

Contents

require 'digest'

module Ridley::Chef
  # Borrowed and modified from: {https://github.com/opscode/chef/blob/11.4.0/lib/chef/digester.rb}
  class Digester
    class << self
      def instance
        @instance ||= new
      end

      def checksum_for_file(*args)
        instance.checksum_for_file(*args)
      end

      def md5_checksum_for_file(*args)
        instance.generate_md5_checksum_for_file(*args)
      end
    end

    def validate_checksum(*args)
      self.class.validate_checksum(*args)
    end

    def checksum_for_file(file)
      generate_checksum(file)
    end

    def generate_checksum(file)
      checksum_file(file, Digest::SHA256.new)
    end

    def generate_md5_checksum_for_file(file)
      checksum_file(file, Digest::MD5.new)
    end

    def generate_md5_checksum(io)
      checksum_io(io, Digest::MD5.new)
    end

    private

      def checksum_file(file, digest)
        File.open(file, 'rb') do |f|
          checksum_io(f, digest)
        end
      end

      def checksum_io(io, digest)
        while chunk = io.read(1024 * 8)
          digest.update(chunk)
        end
        digest.hexdigest
      end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ridley-5.1.1 lib/ridley/chef/digester.rb
ridley-5.1.0 lib/ridley/chef/digester.rb
ridley-5.0.0 lib/ridley/chef/digester.rb
ridley-4.6.1 lib/ridley/chef/digester.rb
ridley-4.6.0 lib/ridley/chef/digester.rb
ridley-4.5.1 lib/ridley/chef/digester.rb
ridley-4.5.0 lib/ridley/chef/digester.rb
ridley-4.4.3 lib/ridley/chef/digester.rb
ridley-4.4.2 lib/ridley/chef/digester.rb
ridley-4.4.1 lib/ridley/chef/digester.rb
ridley-4.4.0 lib/ridley/chef/digester.rb
ridley-4.3.2 lib/ridley/chef/digester.rb
ridley-4.3.1 lib/ridley/chef/digester.rb
ridley-4.3.0 lib/ridley/chef/digester.rb
ridley-4.2.0 lib/ridley/chef/digester.rb
ridley-4.1.2 lib/ridley/chef/digester.rb
ridley-4.1.1 lib/ridley/chef/digester.rb
ridley-4.1.0 lib/ridley/chef/digester.rb