module SyncWrap::HashSupport

Support module for using a crytographic hash to verify the integrity of a file, for example a downloaded package.

Protected Instance Methods

guess_hash_method( hash ) click to toggle source
# File lib/syncwrap/hash_support.rb, line 45
def guess_hash_method( hash )
  case hash.length
  when 32
    :md5
  when 40
    :sha1
  when 56
    :sha224
  when 64
    :sha256
  when 96
    :sha384
  when 128
    :sha512
  end
end
hash_verify( hash, file, opts = {} ) click to toggle source

Enqueue a bash command to verify the specified file using the given hash. This command will fail if the file does not match. The hash :method can be specified in opts (i.e. :sha256) or will be guessed from the provided hex-encoded hash string length. Other opts are as per SyncWrap::Component#sh, though options accept, pipefail and error are ignored. Use of :user can allow this command to be merged.

# File lib/syncwrap/hash_support.rb, line 32
    def hash_verify( hash, file, opts = {} )
      opts = opts.dup
      mth = ( opts.delete(:method) || guess_hash_method( hash ) ) or
        raise( "Hash method unspecified and hash length " +
               "#{hash.length} is non-standard" )

      [ :accept, :pipefail, :error ].each { |k| opts.delete( k ) }

      sh( "        echo "#{hash}  #{file}" | /usr/bin/#{mth}sum -c -
", opts )
    end