Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/hash.js

Issue 29714638: Issue 6446 - Ignore emulated selectors if unchanged (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Use a hash function Created March 7, 2018, 3:59 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« include.preload.js ('K') | « include.preload.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /** @module hash */
19
20 "use strict";
21
22 // This is an implementation of the SuperFastHash algorithm.
23 // See http://www.azillionmonkeys.com/qed/hash.html
24 function SuperFastHash(message)
Manish Jethani 2018/03/07 16:03:22 This hash function is 4 times as fast as MD5 on No
kzar 2018/03/19 20:49:31 Cool, but please add some unit tests.
Sebastian Noack 2018/03/19 21:13:11 I'd rather not add this hash function. It just mit
Manish Jethani 2018/03/20 11:54:22 Yeah, I have to agree with Sebastian that it's not
Manish Jethani 2018/03/20 11:57:43 Also one more thing: I'm beginning to think that t
Manish Jethani 2018/03/21 16:20:12 By now I am convinced that this will have to be ta
25 {
26 let {length} = message;
27
28 if (length == 0)
29 return 0;
30
31 let index = 0;
32
33 let nextUint8 = () => message[index++];
34 let nextUint16 = () => nextUint8() | nextUint8() << 8;
35
36 let digest = length;
37
38 for (let n = length >> 2; n > 0; n--)
39 {
40 digest = digest + nextUint16() >>> 0;
41 digest ^= digest << 16 >>> 0 ^ nextUint16() << 11;
42 digest = digest + (digest >>> 11) >>> 0;
43 }
44
45 switch (length & 3)
46 {
47 case 3:
48 digest = digest + nextUint16() >>> 0;
49 digest ^= digest << 16 >>> 0;
50 digest ^= nextUint8() << 18;
51 digest = digest + (digest >>> 11) >>> 0;
52 break;
53 case 2:
54 digest = digest + nextUint16() >>> 0;
55 digest ^= digest << 11 >>> 0;
56 digest = digest + (digest >>> 17) >>> 0;
57 break;
58 case 1:
59 digest = digest + nextUint8() >>> 0;
60 digest ^= digest << 10 >>> 0;
61 digest = digest + (digest >>> 1) >>> 0;
62 }
63
64 digest ^= digest << 3 >>> 0;
65 digest = digest + (digest >>> 5) >>> 0;
66 digest ^= digest << 4 >>> 0;
67 digest = digest + (digest >>> 17) >>> 0;
68 digest ^= digest << 25 >>> 0;
69 digest = digest + (digest >>> 6) >>> 0;
70
71 return digest;
72 }
73
74 /**
75 * Calculates the digest for a message.
76 *
77 * The hashing algorithm used is "SuperFastHash"
78 * (see http://www.azillionmonkeys.com/qed/hash.html). If no encoding is
79 * specified, the return value is a 32-bit unsigned integer.
80 *
81 * @param {number[]} message The message. This must be an array-like object of
82 * "bytes" (i.e. only the lowest 8 bits are relevant). In order to hash
83 * a string, it must be converted into UTF-8 or some other 8-bit
84 * encoding first.
85 * @param {Object} [options]
86 * @param {string} [options.encoding] The encoding of the digest. This may be
87 * set to "hex". If no encoding is specified, the preferred default
88 * encoding for the hashing algorithm is used.
89 * @return {number|string} The digest.
90 */
91 function hash(message, {encoding} = {})
92 {
93 let digest = SuperFastHash(message);
94
95 if (encoding == "hex")
96 return digest.toString(16);
97
98 return digest;
99 }
100
101 exports.hash = hash;
OLDNEW
« include.preload.js ('K') | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld