Class: Omnizip::Checksums::Verifier

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/checksums/verifier.rb

Overview

Checksum verification utilities for XZ format

XZ supports multiple check types:

  • 0: None (no checksum)
  • 1: CRC32 (4 bytes)
  • 4: CRC64 (8 bytes)
  • 10: SHA256 (32 bytes)

Constant Summary collapse

CHECK_NONE =

Check type constants (from XZ spec)

0
CHECK_CRC32 =
1
CHECK_CRC64 =
4
CHECK_SHA256 =
10
CHECK_SIZES =

Check sizes in bytes

{
  CHECK_NONE => 0,
  CHECK_CRC32 => 4,
  CHECK_CRC64 => 8,
  CHECK_SHA256 => 32,
}.freeze

Class Method Summary collapse

Class Method Details

.calculate(data, check_type) ⇒ String

Calculate checksum for data based on check type

Parameters:

  • data (String)

    Data to checksum

  • check_type (Integer)

    Check type

Returns:

  • (String)

    Checksum in binary format



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/omnizip/checksums/verifier.rb', line 114

def self.calculate(data, check_type)
  case check_type
  when CHECK_NONE
    ""
  when CHECK_CRC32
    [Zlib.crc32(data)].pack("V")
  when CHECK_CRC64
    [Crc64.calculate(data)].pack("Q<")
  when CHECK_SHA256
    Digest::SHA256.digest(data)
  else
    raise "Unknown check type: #{check_type}"
  end
end

.check_size(check_type) ⇒ Integer

Get check size in bytes

Parameters:

  • check_type (Integer)

    Check type

Returns:

  • (Integer)

    Size of checksum in bytes



105
106
107
# File 'lib/omnizip/checksums/verifier.rb', line 105

def self.check_size(check_type)
  CHECK_SIZES.fetch(check_type, 0)
end

.verify(data, expected, check_type) ⇒ Boolean

Generic verify based on check type

Parameters:

  • data (String)

    Data to verify

  • expected (String)

    Expected checksum (binary format)

  • check_type (Integer)

    Check type (0=None, 1=CRC32, 4=CRC64, 10=SHA256)

Returns:

  • (Boolean)

    True if checksum matches or check type is None

Raises:

  • (ArgumentError)

    If check type is unknown



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/omnizip/checksums/verifier.rb', line 84

def self.verify(data, expected, check_type)
  case check_type
  when CHECK_NONE
    true # No checksum to verify
  when CHECK_CRC32
    expected_crc = expected.unpack1("V")
    verify_crc32(data, expected_crc)
  when CHECK_CRC64
    expected_crc = expected.unpack1("Q<")
    verify_crc64(data, expected_crc)
  when CHECK_SHA256
    verify_sha256(data, expected)
  else
    raise "Unknown check type: #{check_type}"
  end
end

.verify_crc32(data, expected) ⇒ Boolean

Verify CRC32 checksum

Parameters:

  • data (String)

    Data to verify

  • expected (Integer)

    Expected CRC32 value

Returns:

  • (Boolean)

    True if checksum matches



55
56
57
# File 'lib/omnizip/checksums/verifier.rb', line 55

def self.verify_crc32(data, expected)
  Zlib.crc32(data) == expected
end

.verify_crc64(data, expected) ⇒ Boolean

Verify CRC64 checksum

Parameters:

  • data (String)

    Data to verify

  • expected (Integer)

    Expected CRC64 value

Returns:

  • (Boolean)

    True if checksum matches



64
65
66
# File 'lib/omnizip/checksums/verifier.rb', line 64

def self.verify_crc64(data, expected)
  Crc64.calculate(data) == expected
end

.verify_sha256(data, expected) ⇒ Boolean

Verify SHA256 checksum

Parameters:

  • data (String)

    Data to verify

  • expected (String)

    Expected SHA256 digest (binary, 32 bytes)

Returns:

  • (Boolean)

    True if checksum matches



73
74
75
# File 'lib/omnizip/checksums/verifier.rb', line 73

def self.verify_sha256(data, expected)
  Digest::SHA256.digest(data) == expected
end