|
1 | 1 | import hash from 'sha.js'; |
2 | 2 | import anyBase from 'any-base'; |
3 | 3 |
|
4 | | -const hexToBase52 = ((alphabet) => |
5 | | - anyBase(anyBase.HEX, alphabet + alphabet.toUpperCase()) |
6 | | -)('abcdefghijklmnopqrstuvwxyz'); |
| 4 | +const alphabeticHash = ((alphabet) => { |
| 5 | + // we use `hexToBase52` so generated identities are exclusively alphabetic |
| 6 | + // as this causes them to fly under parsers' radars more effectively |
| 7 | + const hexToBase52 = anyBase(anyBase.HEX, alphabet + alphabet.toUpperCase()); |
| 8 | + return (content) => hexToBase52(hash('sha256').update(content, 'utf-8').digest('hex')); |
| 9 | +})('abcdefghijklmnopqrstuvwxyz'); |
7 | 10 |
|
8 | 11 | const noOpReplacer = (thing) => thing; |
9 | 12 |
|
10 | 13 | export default class StringReplacementCache { |
11 | | - constructor(expression, outputReplacer = noOpReplacer, identityReplacer = noOpReplacer, algorithm = 'sha256') { |
| 14 | + constructor(expression, outputReplacer = noOpReplacer, identityReplacer = noOpReplacer) { |
12 | 15 | this.expression = expression; |
13 | 16 | this.outputReplacer = outputReplacer; |
14 | 17 | this.identityReplacer = identityReplacer; |
15 | | - this.algorithm = algorithm; |
16 | 18 | this._cache = {}; |
17 | 19 | } |
18 | 20 |
|
19 | 21 | load(body) { |
20 | 22 | const processed = body |
21 | 23 | .replace(this.expression, (match, ...values) => { |
22 | | - // we use `hexToBase52` so generated identities are exclusively alphabetic |
23 | | - // as this causes them to fly under parsers' radars more effectively |
24 | | - const identityHash = hexToBase52( |
25 | | - hash(this.algorithm) |
26 | | - .update(match, 'utf-8') |
27 | | - .digest('hex') |
28 | | - ); |
| 24 | + const identityHash = alphabeticHash(match); |
29 | 25 |
|
30 | 26 | const identity = this.identityReplacer( |
31 | 27 | identityHash, |
|
0 commit comments