class Digest
The digest
header provides a digest of the message body for integrity verification.
This header allows servers to send cryptographic hashes of the response body, enabling clients to verify data integrity. Multiple digest algorithms can be specified, and the header is particularly useful as a trailer since the digest can only be computed after the entire message body is available.
Examples
digest = Digest.new("sha-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=")
digest << "md5=9bb58f26192e4ba00f01e2e7b136bbd8"
puts digest.to_s
# => "sha-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=, md5=9bb58f26192e4ba00f01e2e7b136bbd8"
Definitions
ENTRY = /\A(?<algorithm>[a-zA-Z0-9][a-zA-Z0-9\-]*)\s*=\s*(?<value>.*)\z/
https://tools.ietf.org/html/rfc3230#section-4.3.2
Entry
A single digest entry in the Digest header.
Implementation
Entry = Struct.new(:algorithm, :value) do
# Create a new digest entry.
#
# @parameter algorithm [String] the digest algorithm (e.g., "sha-256", "md5").
# @parameter value [String] the base64-encoded or hex-encoded digest value.
def initialize(algorithm, value)
super(algorithm.downcase, value)
end
# Convert the entry to its string representation.
#
# @returns [String] the formatted digest string.
def to_s
"#{algorithm}=#{value}"
end
end
def entries
Parse the digest
header value into a list of digest entries.
Signature
-
returns
Array(Entry)
the list of digest entries with their algorithms and values.
Implementation
def entries
self.map do |value|
if match = value.match(ENTRY)
Entry.new(match[:algorithm], match[:value])
else
raise ParseError.new("Could not parse digest value: #{value.inspect}")
end
end
end
def self.trailer?
Whether this header is acceptable in HTTP trailers.
Signature
-
returns
Boolean
true
, as digest headers contain integrity hashes that can only be calculated after the entire message body is available.
Implementation
def self.trailer?
true
end