Module: Google::Cloud::Storage::File::Verifier

Defined in:
lib/google/cloud/storage/file/verifier.rb

Overview

Verifies downloaded files by creating an MD5 or CRC32c hash digest and comparing the value to the one from the Storage API.

Class Method Summary collapse

Class Method Details

.crc32c_for(local_file) ⇒ Object



60
61
62
63
64
# File 'lib/google/cloud/storage/file/verifier.rb', line 60

def self.crc32c_for local_file
  ::File.open(Pathname(local_file).to_path, "rb") do |f|
    ::Digest::CRC32c.file(f).base64digest
  end
end

.md5_for(local_file) ⇒ Object



54
55
56
57
58
# File 'lib/google/cloud/storage/file/verifier.rb', line 54

def self.md5_for local_file
  ::File.open(Pathname(local_file).to_path, "rb") do |f|
    ::Digest::MD5.file(f).base64digest
  end
end

.verify_crc32c(gcloud_file, local_file) ⇒ Object



50
51
52
# File 'lib/google/cloud/storage/file/verifier.rb', line 50

def self.verify_crc32c gcloud_file, local_file
  gcloud_file.crc32c == crc32c_for(local_file)
end

.verify_crc32c!(gcloud_file, local_file) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/google/cloud/storage/file/verifier.rb', line 38

def self.verify_crc32c! gcloud_file, local_file
  gcloud_digest = gcloud_file.crc32c
  local_digest = crc32c_for local_file
  if gcloud_digest != local_digest
    fail FileVerificationError.for_crc32c(gcloud_digest, local_digest)
  end
end

.verify_md5(gcloud_file, local_file) ⇒ Object



46
47
48
# File 'lib/google/cloud/storage/file/verifier.rb', line 46

def self.verify_md5 gcloud_file, local_file
  gcloud_file.md5 == md5_for(local_file)
end

.verify_md5!(gcloud_file, local_file) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/google/cloud/storage/file/verifier.rb', line 30

def self.verify_md5! gcloud_file, local_file
  gcloud_digest = gcloud_file.md5
  local_digest = md5_for local_file
  if gcloud_digest != local_digest
    fail FileVerificationError.for_md5(gcloud_digest, local_digest)
  end
end