* Generates `2 ** rounds` distinct strings that all share the same * `Immutable.hash()`, by concatenating the classic "Aa"/"BB" collision blocks * (both equal `65 * 31 + 97 === 66 * 31 + 66 === 2112` under the JVM-style * `31 * h + c` string hash). Inserting these into a Map forces them all into
(rounds: number)
| 9 | * single HashCollisionNode — the hash-flooding scenario this code guards. |
| 10 | */ |
| 11 | function collisionKeys(rounds: number): Array<string> { |
| 12 | let keys = ['']; |
| 13 | for (let i = 0; i < rounds; i++) { |
| 14 | const next: Array<string> = []; |
| 15 | for (const k of keys) { |
| 16 | next.push(k + 'Aa'); |
| 17 | next.push(k + 'BB'); |
| 18 | } |
| 19 | keys = next; |
| 20 | } |
| 21 | return keys; |
| 22 | } |
| 23 | |
| 24 | describe('Map hash collisions', () => { |
| 25 | it('the generated keys really do collide (test is meaningful)', () => { |