resourceloader: Replace SHA1 with 32-bit FNV-1 as hash function
authorOri Livneh <ori@wikimedia.org>
Wed, 22 Jun 2016 22:32:58 +0000 (15:32 -0700)
committerKrinkle <krinklemail@gmail.com>
Tue, 5 Jul 2016 21:14:03 +0000 (21:14 +0000)
commitdfd046412fc94b7e5788c04a24d2db08480f75c6
tree0c02d29c46f97ae02135da969451da81cfeb1aef
parent78c1c6de078e227bc5fab39a89abd3dfcbcf0bd0
resourceloader: Replace SHA1 with 32-bit FNV-1 as hash function

SHA-1 is not secure enough to be used as a cryptographic hash function, and its
implementation in JavaScript is too long and too slow for it to be a good
general-purpose hash function. And we currently throw away most of the work:
SHA-1 produces 160-bit hash values, of which we keep 48.

Although the JavaScript implementation is not exported, SHA-1 is a well-known
hash function, and I'm willing to bet that sooner or later someone will move to
make it accessible to other modules, at which point usage will start to spread.

For ResourceLoader, the qualities we're looking for in a hash function are:

* Already implemented in PHP
* Easy to implement in JavaScript
* Fast
* Collision-resistant

The requirement that hashes be cheap to compute in JavaScript narrows the field
to 32-bit hash functions, because in JavaScript bitwise operators treat their
operands as 32 bits, and arithmetic uses double-precision floats, which have a
total precision of 53 bits. It's possible to work around these limitations, but
it's a lot of extra work.

The best match I found is the 32-bit variant of FNV-1, which is available in
PHP as of version 5.4 (as 'fnv1a32'). The fnv132 JavaScript function is
around ten times faster and eight times shorter than sha1.

Change-Id: I1e4fb08d17948538d96f241b2464d594fdc14578
includes/resourceloader/ResourceLoader.php
resources/Resources.php
resources/lib/phpjs-sha1/sha1.js [deleted file]
resources/src/mediawiki/mediawiki.js
tests/phpunit/includes/resourceloader/ResourceLoaderStartUpModuleTest.php
tests/phpunit/structure/ResourcesTest.php