module Sprockets::DigestUtils
def detect_digest_class(bytes)
While not elegant, all the supported digests have a unique bytesize.
Internal: Detect digest class hash algorithm for digest bytes.
def detect_digest_class(bytes) DIGEST_SIZES[bytes.bytesize] end
def digest(obj)
obj - A JSON serializable object.
wicked fast. Microbenchmarks away!
This is used for generating cache keys, so its pretty important its
Internal: Generate a hexdigest for a nested JSON serializable object.
def digest(obj) digest = digest_class.new queue = [obj] while queue.length > 0 obj = queue.shift klass = obj.class if klass == String digest << obj elsif klass == Symbol digest << 'Symbol' digest << obj.to_s elsif klass == Fixnum digest << 'Fixnum' digest << obj.to_s elsif klass == Bignum digest << 'Bignum' digest << obj.to_s elsif klass == TrueClass digest << 'TrueClass' elsif klass == FalseClass digest << 'FalseClass' elsif klass == NilClass digest << 'NilClass' elsif klass == Array digest << 'Array' queue.concat(obj) elsif klass == Hash digest << 'Hash' queue.concat(obj.sort) elsif klass == Set digest << 'Set' queue.concat(obj.to_a) elsif klass == Encoding digest << 'Encoding' digest << obj.name else raise TypeError, "couldn't digest #{klass}" end end digest.digest end
def digest_class
Internal: Default digest class.
def digest_class Digest::SHA256 end
def integrity_uri(digest, content_type = nil)
will block the loading of the asset.
be accurate if provided. Otherwise, subresource integrity
content_type - The content-type the asset will be served with. This *must*
digest - The String byte digest of the asset content.
attribute of an asset tag as per the subresource integrity specification.
Internal: Generate a "named information" URI for use in the `integrity`
def integrity_uri(digest, content_type = nil) 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 = NI_HASH_ALGORITHMS[digest_class] uri = "ni:///#{hash_name};#{pack_urlsafe_base64digest(digest)}" uri << "?ct=#{content_type}" if content_type uri end end
def pack_base64digest(bin)
bin - String bytes
Internal: Pack a binary digest to a base64 encoded string.
def pack_base64digest(bin) [bin].pack('m0') end
def pack_hexdigest(bin)
bin - String bytes
Internal: Pack a binary digest to a hex encoded string.
def pack_hexdigest(bin) bin.unpack('H*').first end
def pack_urlsafe_base64digest(bin)
bin - String bytes
Internal: Pack a binary digest to a urlsafe base64 encoded string.
def pack_urlsafe_base64digest(bin) pack_base64digest(bin).tr('+/', '-_').tr('=', '') end