File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ def integrity(logical_path)
16
16
hash_algorithm = load_path . integrity_hash_algorithm
17
17
18
18
if hash_algorithm && ( asset = find_asset ( logical_path ) )
19
- asset . integrity ( hash_algorithm :)
19
+ generate_integrity ( asset , hash_algorithm :)
20
20
end
21
21
end
22
22
@@ -30,5 +30,25 @@ def read(logical_path, options = {})
30
30
def find_asset ( logical_path )
31
31
load_path . find ( logical_path )
32
32
end
33
+
34
+ def generate_integrity ( asset , hash_algorithm :)
35
+ # Following the Subresource Integrity spec draft
36
+ # https://w3c.github.io/webappsec-subresource-integrity/
37
+ # allowing only sha256, sha384, and sha512
38
+ bitlen = case hash_algorithm
39
+ when "sha256"
40
+ 256
41
+ when "sha384"
42
+ 384
43
+ when "sha512"
44
+ 512
45
+ else
46
+ raise ( StandardError . new ( "Subresource Integrity hash algorithm must be one of SHA2 family (sha256, sha384, sha512)" ) )
47
+ end
48
+
49
+ content = load_path . compilers . compile ( asset )
50
+
51
+ [ hash_algorithm , Digest ::SHA2 . new ( bitlen ) . base64digest ( content ) ] . join ( "-" )
52
+ end
33
53
end
34
54
end
You can’t perform that action at this time.
0 commit comments