Module: Pray::Hashing

Defined in:
lib/pray/hashing.rb

Class Method Summary collapse

Class Method Details

.checksum_managed_body_line_refs(body_lines) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/pray/hashing.rb', line 35

def checksum_managed_body_line_refs(body_lines)
  lines = trim_trailing_empty_lines(body_lines)
  digest = OpenSSL::Digest::SHA256.new
  lines.each_with_index do |line, index|
    digest << "\n" if index.positive?
    update_line_endings_normalized(digest, line)
  end
  prefixed_hex_digest(digest.digest)
end

.checksum_managed_span_content(body) ⇒ Object



30
31
32
33
# File 'lib/pray/hashing.rb', line 30

def checksum_managed_span_content(body)
  normalized = normalize_line_endings(body).sub(/\n+\z/, "")
  sha256_prefixed(normalized)
end

.marker_id(seed) ⇒ Object



22
23
24
# File 'lib/pray/hashing.rb', line 22

def marker_id(seed)
  sha256_hex(seed)[0, 8]
end

.normalize_line_endings(text) ⇒ Object



26
27
28
# File 'lib/pray/hashing.rb', line 26

def normalize_line_endings(text)
  text.gsub("\r\n", "\n").tr("\r", "\n")
end

.prefixed_hex_digest(digest_bytes) ⇒ Object



18
19
20
# File 'lib/pray/hashing.rb', line 18

def prefixed_hex_digest(digest_bytes)
  "sha256:#{digest_bytes.unpack1("H*")}"
end

.sha256_hex(bytes) ⇒ Object



10
11
12
# File 'lib/pray/hashing.rb', line 10

def sha256_hex(bytes)
  Digest::SHA256.hexdigest(bytes)
end

.sha256_prefixed(bytes) ⇒ Object



14
15
16
# File 'lib/pray/hashing.rb', line 14

def sha256_prefixed(bytes)
  prefixed_hex_digest(Digest::SHA256.digest(bytes))
end

.trim_trailing_empty_lines(lines) ⇒ Object



45
46
47
48
49
# File 'lib/pray/hashing.rb', line 45

def trim_trailing_empty_lines(lines)
  trimmed = lines.dup
  trimmed.pop while trimmed.last == ""
  trimmed
end

.update_line_endings_normalized(digest, line) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/pray/hashing.rb', line 51

def update_line_endings_normalized(digest, line)
  digest << if line.include?("\r")
    normalize_line_endings(line)
  else
    line
  end
end