|
1 | | - |
| 1 | +import { sha256 } from 'ohash'; |
2 | 2 | /** |
3 | | - * This is a very fast hash method |
4 | | - * but it is not cryptographically secure. |
5 | | - * For each run it will append a number between 0 and 2147483647 (=biggest 32 bit int). |
6 | | - * @link http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery |
7 | | - * @return a string as hash-result |
8 | | - */ |
9 | | -export function fastUnsecureHash( |
10 | | - inputString: string, |
11 | | - // used to test the polyfill |
12 | | - doNotUseTextEncoder?: boolean |
13 | | -): string { |
14 | | - let hashValue = 0, |
15 | | - i, chr, len; |
16 | | - |
17 | | - /** |
18 | | - * For better performance we first transform all |
19 | | - * chars into their ascii numbers at once. |
20 | | - * |
21 | | - * This is what makes the murmurhash implementation such fast. |
22 | | - * @link https://github.com/perezd/node-murmurhash/blob/master/murmurhash.js#L4 |
23 | | - */ |
24 | | - let encoded: Uint8Array | number[]; |
25 | | - |
26 | | - /** |
27 | | - * All modern browsers support the TextEncoder |
28 | | - * @link https://caniuse.com/textencoder |
29 | | - * But to make RxDB work in other JavaScript runtimes, |
30 | | - * like when using it in flutter or QuickJS, we need to |
31 | | - * make it work even when there is no TextEncoder. |
32 | | - * |
33 | | - * TODO is a text encoder really faster then using charCodeAt? |
34 | | - */ |
35 | | - if (typeof TextEncoder !== 'undefined' && !doNotUseTextEncoder) { |
36 | | - encoded = new TextEncoder().encode(inputString); |
37 | | - } else { |
38 | | - encoded = []; |
39 | | - for (let j = 0; j < inputString.length; j++) { |
40 | | - encoded.push(inputString.charCodeAt(j)); |
41 | | - } |
42 | | - } |
43 | | - |
44 | | - for (i = 0, len = inputString.length; i < len; i++) { |
45 | | - chr = encoded[i]; |
46 | | - hashValue = ((hashValue << 5) - hashValue) + chr; |
47 | | - hashValue |= 0; // Convert to 32bit integer |
48 | | - } |
49 | | - if (hashValue < 0) { |
50 | | - hashValue = hashValue * -1; |
51 | | - } |
52 | | - |
53 | | - /** |
54 | | - * To make the output smaller |
55 | | - * but still have it to represent the same value, |
56 | | - * we use the biggest radix of 36 instead of just |
57 | | - * transforming it into a hex string. |
58 | | - */ |
59 | | - return hashValue.toString(36); |
60 | | -} |
61 | | - |
62 | | -/** |
63 | | - * Default hash method used to create revision hashes |
64 | | - * that do not have to be cryptographically secure. |
| 3 | + * Default hash method used to hash |
| 4 | + * strings and do equal comparisons. |
| 5 | + * |
65 | 6 | * IMPORTANT: Changing the default hashing method |
66 | 7 | * requires a BREAKING change! |
67 | 8 | */ |
68 | | -export function defaultHashFunction(input: string): string { |
69 | | - return fastUnsecureHash(input); |
| 9 | +export function defaultHashSha256(input: string): string { |
| 10 | + return sha256(input); |
70 | 11 | } |
0 commit comments