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
-
.for_file(path, algorithm: :sha256) ⇒ String
Generates a strong ETag for a file based on its mtime and size.
-
.strong(content, algorithm: :sha256) ⇒ String
Generates a strong ETag from content using the specified algorithm.
-
.weak(content) ⇒ String
Generates a weak ETag from content using MD5.
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.
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.
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.
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 |