2
I was looking at a new specification W3C SRI (Subresource Integrity) which roughly means Integrity of the Sub-resource that promises to bring more security to resources hosted on third-party servers (or in the own) already in use by Bootstrapcdn, Cloudflare, Github and jQuery although it is still new and does not have a wide implementation by browsers.
Basically the new specification adds the tag script
two new attributes:
- Integrity: containing the
hash
cryptographic file - crossorigin: by default "Anonymous"
The (simplistic) documentation describes the use of OpenSSL
via command line to create the hash
:
Command line instruction to generate hash
openssl dgst -sha384 -binary FILENAME.js | openssl base64 -A
My question is: how to use the functions openssl
of PHP
to get the same result (generate the hash of a file)?
References:
- SRI Hash Generator: online tool
- W3C Subresource Integrity: specification
- caniuse with.: support
- Mozila Docs: documentation
- Mozila Hacks: article
It is worth mentioning the correction: the specification
SRI
requires the decriminator of the algorithm used to belowercase
and the value inbase64
be abinário
.– Lauro Moraes
As for use: the
scripts
and its attributes are mounted onPHP
and curly withOPcache
to be used on other pages without waste of resources... regarding unauthorized access or good hack, this specification does not mitigate the damage arising from this.– Lauro Moraes
I corrected to force the minuscule letter. About the "invasions" what I meant is that it reduces the damage range. Imagine that you use the Jquery of Googleapis. If Google "is hacked" (or MITM) and changes the Jquery library, your website may display malicious javascript instead. With the use of this feature the hash of both files will be different, the malicious file will have a different hash than the original file, thus preventing visitors to your site from using malicious JS, so we can say that in case of invasions on THIRD PARTY SERVERS it reduces the damage.
– Inkeliz
Wouldn’t it have been easier to change SHA512 for sha512 rather than the use of
strtolower()
? Add a third argument to the functionhash_file()
({boolean} true)
for her to return a binary. Would you know what function (openssl_digest()
,hash()
orhash_file()
) has less processing cost? Grateful– Lauro Moraes
The way your example is making a mistake
– Lauro Moraes
I made the correction, I did not understand this detail. O
hash()
is for texts, you use thefile_get_contents
should be slower than thehash_file
, that must do the same task in C, much faster than PHP. I do not know, I personally avoid using it, but I believe it should be as fast as. I used thestrtolower
because it avoids human error, if it depends on someone typing in minuscule or capital case the chance of failure would be higher, someone could fall here and not notice that it has to be minuscule, using thestrtolower
either way will be minuscule.– Inkeliz