1 | /**
|
2 | * Created by clx on 2017/11/17.
|
3 | */
|
4 | /**
|
5 | * Calculate a 32 bit FNV-1a hash
|
6 | * Found here: https://gist.github.com/vaiorabbit/5657561
|
7 | * Ref.: http://isthe.com/chongo/tech/comp/fnv/
|
8 | *
|
9 | * @param {string} str the input value
|
10 | * @param {boolean} [asString=false] set to true to return the hash value as
|
11 | * 8-digit hex string instead of an integer
|
12 | * @param {integer} [seed] optionally pass the hash of the previous chunk
|
13 | * @returns {integer | string}
|
14 | */
|
15 | module.exports = function(str, asString, seed) {
|
16 | /*jshint bitwise:false */
|
17 | var i, l,
|
18 | hval = (seed === undefined) ? 0x811c9dc5 : seed;
|
19 |
|
20 | for (i = 0, l = str.length; i < l; i++) {
|
21 | hval ^= str.charCodeAt(i);
|
22 | hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
|
23 | }
|
24 | if( asString ){
|
25 | // Convert to 8 digit hex string
|
26 | return ("0000000" + (hval >>> 0).toString(16)).substr(-8);
|
27 | }
|
28 | return hval >>> 0;
|
29 | };
|