Module: Philiprehberger::Etag::Generator

Defined in:
lib/philiprehberger/etag/generator.rb

Overview

Generates strong and weak ETags from content using cryptographic hashes.

Constant Summary collapse

ALGORITHMS =
{
  sha256: Digest::SHA256,
  sha512: Digest::SHA512,
  md5: Digest::MD5,
  sha1: Digest::SHA1
}.freeze

Class Method Summary collapse

Class Method Details

.for_file(path, algorithm: :sha256) ⇒ String

Generates a strong ETag for a file based on its mtime and size. Does not read file content.

Parameters:

  • path (String)

    the file path

  • algorithm (Symbol) (defaults to: :sha256)

    the hash algorithm (:sha256, :sha512, :md5, :sha1)

Returns:

  • (String)

    a quoted ETag string

Raises:

  • (Errno::ENOENT)

    if the file does not exist

  • (ArgumentError)

    if the algorithm is not supported



44
45
46
47
48
49
# File 'lib/philiprehberger/etag/generator.rb', line 44

def self.for_file(path, algorithm: :sha256)
  stat = File.stat(path)
  fingerprint = "#{stat.mtime.to_i}-#{stat.size}"
  digest = compute_digest(fingerprint, algorithm)
  %("#{digest}")
end

.strong(content, algorithm: :sha256) ⇒ String

Generates a strong ETag from content using the specified algorithm.

Parameters:

  • content (String)

    the content to hash

  • algorithm (Symbol) (defaults to: :sha256)

    the hash algorithm (:sha256, :sha512, :md5, :sha1)

Returns:

  • (String)

    a quoted ETag string, e.g. ‘“"a1b2c3…"”`

Raises:

  • (ArgumentError)

    if the algorithm is not supported



22
23
24
25
# File 'lib/philiprehberger/etag/generator.rb', line 22

def self.strong(content, algorithm: :sha256)
  digest = compute_digest(content, algorithm)
  %("#{digest}")
end

.weak(content) ⇒ String

Generates a weak ETag from content using MD5.

Parameters:

  • content (String)

    the content to hash

Returns:

  • (String)

    a weak ETag string, e.g. ‘“W/"a1b2c3…"”`



31
32
33
34
# File 'lib/philiprehberger/etag/generator.rb', line 31

def self.weak(content)
  digest = Digest::MD5.hexdigest(content.to_s)
  %(W/"#{digest}")
end