class WebPackage::MICE

Merkle Integrity Content Encoding tools.ietf.org/id/draft-thomson-http-mice-03.html

Constants

CHUNK_SIZE

Public Instance Methods

encode(text) click to toggle source
# File lib/web_package/mice.rb, line 9
def encode(text)
  num_parts = text.bytesize.fdiv(CHUNK_SIZE).ceil

  chunks = []
  proofs = []

  num_parts.times do |i|
    delimeter = i.zero? && "\x00" || "\x01"
    ri = num_parts - i - 1

    chunks << force_bin(text.byteslice(ri * CHUNK_SIZE, CHUNK_SIZE))
    proofs << digest("#{chunks.last}#{proofs.last}#{delimeter}")
  end

  chunks << [CHUNK_SIZE].pack('Q>')

  return proofs.pop, chunks.zip(proofs).flatten.reverse!.join
end