Module: Sprockets::DigestUtils

Extended by:
DigestUtils
Included in:
Base, Dependencies, DigestUtils, Loader, PathDigestUtils
Defined in:
lib/sprockets/digest_utils.rb

Overview

Internal: Hash functions and digest related utilities. Mixed into Environment.

Constant Summary collapse

DIGEST_SIZES =

Internal: Maps digest bytesize to the digest class.

{
  16 => Digest::MD5,
  20 => Digest::SHA1,
  32 => Digest::SHA256,
  48 => Digest::SHA384,
  64 => Digest::SHA512
}
HASH_ALGORITHMS =

Internal: Maps digest class to the CSP hash algorithm name.

{
  Digest::SHA256 => 'sha256'.freeze,
  Digest::SHA384 => 'sha384'.freeze,
  Digest::SHA512 => 'sha512'.freeze
}

Instance Method Summary collapse

Instance Method Details

#detect_digest_class(bytes) ⇒ Object

Internal: Detect digest class hash algorithm for digest bytes.

While not elegant, all the supported digests have a unique bytesize.

Returns Digest::Base or nil.



34
35
36
# File 'lib/sprockets/digest_utils.rb', line 34

def detect_digest_class(bytes)
  DIGEST_SIZES[bytes.bytesize]
end

#digest(obj) ⇒ Object

Internal: Generate a hexdigest for a nested JSON serializable object.

This is used for generating cache keys, so its pretty important its wicked fast. Microbenchmarks away!

obj - A JSON serializable object.

Returns a String digest of the object.



99
100
101
# File 'lib/sprockets/digest_utils.rb', line 99

def digest(obj)
  build_digest(obj).digest
end

#digest_classObject

Internal: Default digest class.

Returns a Digest::Base subclass.



16
17
18
# File 'lib/sprockets/digest_utils.rb', line 16

def digest_class
  Digest::SHA256
end

#hexdigest(obj) ⇒ Object

Internal: Generate a hexdigest for a nested JSON serializable object.

The same as `pack_hexdigest(digest(obj))`.

obj - A JSON serializable object.

Returns a String digest of the object.



110
111
112
# File 'lib/sprockets/digest_utils.rb', line 110

def hexdigest(obj)
  build_digest(obj).hexdigest!
end

#hexdigest_integrity_uri(hexdigest) ⇒ Object

Public: Generate hash for use in the `integrity` attribute of an asset tag as per the subresource integrity specification.

digest - The String hexbyte digest of the asset content.

Returns a String or nil if hash algorithm is incompatible.



188
189
190
# File 'lib/sprockets/digest_utils.rb', line 188

def hexdigest_integrity_uri(hexdigest)
  integrity_uri(unpack_hexdigest(hexdigest))
end

#integrity_uri(digest) ⇒ Object

Public: Generate hash for use in the `integrity` attribute of an asset tag as per the subresource integrity specification.

digest - The String byte digest of the asset content.

Returns a String or nil if hash algorithm is incompatible.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/sprockets/digest_utils.rb', line 166

def integrity_uri(digest)
  case digest
  when Digest::Base
    digest_class = digest.class
    digest = digest.digest
  when String
    digest_class = DIGEST_SIZES[digest.bytesize]
  else
    raise TypeError, "unknown digest: #{digest.inspect}"
  end

  if hash_name = HASH_ALGORITHMS[digest_class]
    "#{hash_name}-#{pack_base64digest(digest)}"
  end
end

#pack_base64digest(bin) ⇒ Object

Internal: Pack a binary digest to a base64 encoded string.

bin - String bytes

Returns base64 String.



137
138
139
# File 'lib/sprockets/digest_utils.rb', line 137

def pack_base64digest(bin)
  [bin].pack('m0')
end

#pack_hexdigest(bin) ⇒ Object

Internal: Pack a binary digest to a hex encoded string.

bin - String bytes

Returns hex String.



119
120
121
# File 'lib/sprockets/digest_utils.rb', line 119

def pack_hexdigest(bin)
  bin.unpack('H*'.freeze).first
end

#pack_urlsafe_base64digest(bin) ⇒ Object

Internal: Pack a binary digest to a urlsafe base64 encoded string.

bin - String bytes

Returns urlsafe base64 String.



146
147
148
149
150
151
# File 'lib/sprockets/digest_utils.rb', line 146

def pack_urlsafe_base64digest(bin)
  str = pack_base64digest(bin)
  str.tr!('+/'.freeze, '-_'.freeze)
  str.tr!('='.freeze, ''.freeze)
  str
end

#unpack_hexdigest(hex) ⇒ Object

Internal: Unpack a hex encoded digest string into binary bytes.

hex - String hex

Returns binary String.



128
129
130
# File 'lib/sprockets/digest_utils.rb', line 128

def unpack_hexdigest(hex)
  [hex].pack('H*')
end