Class: Lutaml::Store::Integrity
- Inherits:
-
Object
- Object
- Lutaml::Store::Integrity
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")
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
|
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/lutaml/store/integrity.rb', line 39
def self.create_integrity_metadata(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)
return backup_data if backup_data && valid_data?(backup_data)
cleaned_data = corrupted_data.dup
cleaned_data.gsub!("\x00", "")
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
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")
begin
data.encode("UTF-8")
true
rescue Encoding::UndefinedConversionError
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
|
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/lutaml/store/integrity.rb', line 50
def self.verify_integrity_metadata(data, metadata)
string_data = data.is_a?(String) ? data : data.to_s
if metadata[:size] && string_data.bytesize != metadata[:size]
raise CorruptionError,
"Size mismatch: expected #{metadata[:size]}, got #{string_data.bytesize}"
end
verify_checksum(data, metadata[:checksum], metadata[:algorithm]) if metadata[:checksum] && metadata[:algorithm]
true
end
|