Class: Lutaml::Store::Integrity

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/store/integrity.rb

Defined Under Namespace

Classes: ChecksumMismatchError, CorruptionError, IntegrityError

Class Method Summary collapse

Class Method Details

.calculate_checksum(data, algorithm = "sha256") ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lutaml/store/integrity.rb', line 12

def self.calculate_checksum(data, algorithm = "sha256")
  # Convert data to string if it's not already
  string_data = data.is_a?(String) ? data : data.to_s

  case algorithm.to_s.downcase
  when "md5"
    Digest::MD5.hexdigest(string_data)
  when "sha1"
    Digest::SHA1.hexdigest(string_data)
  when "sha256"
    Digest::SHA256.hexdigest(string_data)
  when "sha512"
    Digest::SHA512.hexdigest(string_data)
  else
    raise ArgumentError, "Unsupported checksum algorithm: #{algorithm}"
  end
end

.create_integrity_metadata(data, algorithm = "sha256") ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/lutaml/store/integrity.rb', line 39

def self.(data, algorithm = "sha256")
  string_data = data.is_a?(String) ? data : data.to_s
  {
    checksum: calculate_checksum(data, algorithm),
    algorithm: algorithm,
    size: string_data.bytesize,
    created_at: Time.now.utc.iso8601,
    version: "1.0"
  }
end

.repair_data(corrupted_data, backup_data = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/lutaml/store/integrity.rb', line 65

def self.repair_data(corrupted_data, backup_data = nil)
  # Basic repair attempt - in a real implementation, this could be more sophisticated
  return backup_data if backup_data && valid_data?(backup_data)

  # Try to clean up common corruption patterns
  cleaned_data = corrupted_data.dup

  # Remove null bytes that might have been introduced
  cleaned_data.gsub!("\x00", "")

  # Try to fix truncated JSON/YAML
  if cleaned_data.strip.start_with?("{") && !cleaned_data.strip.end_with?("}")
    cleaned_data += "}"
  elsif cleaned_data.strip.start_with?("[") && !cleaned_data.strip.end_with?("]")
    cleaned_data += "]"
  end

  cleaned_data
end

.valid_data?(data) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lutaml/store/integrity.rb', line 85

def self.valid_data?(data)
  return false if data.nil? || data.empty?
  return false if data.include?("\x00") # Contains null bytes

  # Basic validation - data should be valid UTF-8 or binary
  begin
    data.encode("UTF-8")
    true
  rescue Encoding::UndefinedConversionError
    # Might be binary data, which is also valid
    true
  rescue StandardError
    false
  end
end

.verify_checksum(data, expected_checksum, algorithm = "sha256") ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/lutaml/store/integrity.rb', line 30

def self.verify_checksum(data, expected_checksum, algorithm = "sha256")
  actual_checksum = calculate_checksum(data, algorithm)
  unless actual_checksum == expected_checksum
    raise ChecksumMismatchError,
          "Checksum mismatch: expected #{expected_checksum}, got #{actual_checksum}"
  end
  true
end

.verify_integrity_metadata(data, metadata) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lutaml/store/integrity.rb', line 50

def self.(data, )
  string_data = data.is_a?(String) ? data : data.to_s

  # Verify size
  if [:size] && string_data.bytesize != [:size]
    raise CorruptionError,
          "Size mismatch: expected #{[:size]}, got #{string_data.bytesize}"
  end

  # Verify checksum
  verify_checksum(data, [:checksum], [:algorithm]) if [:checksum] && [:algorithm]

  true
end