-
Notifications
You must be signed in to change notification settings - Fork 788
/
digest_utils.rb
213 lines (198 loc) · 5.97 KB
/
digest_utils.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require 'digest/md5'
require 'digest/sha1'
require 'digest/sha2'
require 'set'
module Sprockets
# Internal: Hash functions and digest related utilities. Mixed into
# Environment.
module DigestUtils
extend self
# Internal: Default digest class.
#
# Returns a Digest::Base subclass.
def digest_class
Digest::SHA256
end
# Internal: Maps digest bytesize to the digest class.
DIGEST_SIZES = {
16 => Digest::MD5,
20 => Digest::SHA1,
32 => Digest::SHA256,
48 => Digest::SHA384,
64 => Digest::SHA512
}
# 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.
def detect_digest_class(bytes)
DIGEST_SIZES[bytes.bytesize]
end
ADD_VALUE_TO_DIGEST = {
String => ->(val, digest) { digest << val },
FalseClass => ->(val, digest) { digest << 'FalseClass'.freeze },
TrueClass => ->(val, digest) { digest << 'TrueClass'.freeze },
NilClass => ->(val, digest) { digest << 'NilClass'.freeze },
Symbol => ->(val, digest) {
digest << 'Symbol'.freeze
digest << val.to_s
},
Integer => ->(val, digest) {
digest << 'Integer'.freeze
digest << val.to_s
},
Array => ->(val, digest) {
digest << 'Array'.freeze
val.each do |element|
ADD_VALUE_TO_DIGEST[element.class].call(element, digest)
end
},
Hash => ->(val, digest) {
digest << 'Hash'.freeze
val.sort.each do |array|
ADD_VALUE_TO_DIGEST[Array].call(array, digest)
end
},
Set => ->(val, digest) {
digest << 'Set'.freeze
ADD_VALUE_TO_DIGEST[Array].call(val.to_a, digest)
},
Encoding => ->(val, digest) {
digest << 'Encoding'.freeze
digest << val.name
},
}
if 0.class != Integer # Ruby < 2.4
ADD_VALUE_TO_DIGEST[Fixnum] = ->(val, digest) {
digest << 'Integer'.freeze
digest << val.to_s
}
ADD_VALUE_TO_DIGEST[Bignum] = ->(val, digest) {
digest << 'Integer'.freeze
digest << val.to_s
}
end
ADD_VALUE_TO_DIGEST.default_proc = ->(_, val) {
raise TypeError, "couldn't digest #{ val }"
}
private_constant :ADD_VALUE_TO_DIGEST
def a_v_t_d(c, v, d)
case c
when 'String'.freeze then d << v
when 'FalseClass'.freeze then d << 'FalseClass'.freeze
when 'TrueClass'.freeze then d << 'TrueClass'.freeze
when 'NilClass'.freeze then d << 'NilClass'.freeze
when 'Symbol'.freeze
d << 'Symbol'.freeze
d << v.to_s
when 'Integer'.freeze
d << 'Integer'.freeze
d << v.to_s
when 'Array'.freeze
d << 'Array'.freeze
v.each { |element| a_v_t_d(element.class.name, element, d) }
when 'Hash'.freeze
d << 'Hash'.freeze
v.sort.each { |array| a_v_t_d('Array', array, d) }
when 'Set'.freeze
d << 'Set'.freeze
a_v_t_d('Array',v.to_a, d)
when 'Encoding'.freeze
d << 'Encoding'.freeze
d << v.name
when 'Fixnum'.freeze
d << 'Integer'.freeze
d << v.to_s
when 'Bignum'.freeze
d << 'Integer'.freeze
d << v.to_s
else raise TypeError, "couldn't digest #{c} - #{c.class} -- #{v}"
end
end
# 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.
def digest(obj)
digest = digest_class.new
a_v_t_d(obj.class.name, obj, digest)
# ADD_VALUE_TO_DIGEST[obj.class].call(obj, digest)
digest.digest
end
# Internal: Pack a binary digest to a hex encoded string.
#
# bin - String bytes
#
# Returns hex String.
def pack_hexdigest(bin)
bin.unpack('H*').first
end
# Internal: Unpack a hex encoded digest string into binary bytes.
#
# hex - String hex
#
# Returns binary String.
def unpack_hexdigest(hex)
[hex].pack('H*')
end
# Internal: Pack a binary digest to a base64 encoded string.
#
# bin - String bytes
#
# Returns base64 String.
def pack_base64digest(bin)
[bin].pack('m0')
end
# Internal: Pack a binary digest to a urlsafe base64 encoded string.
#
# bin - String bytes
#
# Returns urlsafe base64 String.
def pack_urlsafe_base64digest(bin)
str = pack_base64digest(bin)
str.tr!('+/'.freeze, '-_'.freeze)
str.tr!('='.freeze, ''.freeze)
str
end
# Internal: Maps digest class to the CSP hash algorithm name.
HASH_ALGORITHMS = {
Digest::SHA256 => 'sha256'.freeze,
Digest::SHA384 => 'sha384'.freeze,
Digest::SHA512 => 'sha512'.freeze
}
# 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.
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
# 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.
def hexdigest_integrity_uri(hexdigest)
integrity_uri(unpack_hexdigest(hexdigest))
end
end
end