Skip to content

Commit cea64bc

Browse files
committed
🔤 Use alphabetic hash for string replacements
1 parent db2106d commit cea64bc

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/string-replacement-cache.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
import hash from 'sha.js';
22
import anyBase from 'any-base';
33

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');
710

811
const noOpReplacer = (thing) => thing;
912

1013
export default class StringReplacementCache {
11-
constructor(expression, outputReplacer = noOpReplacer, identityReplacer = noOpReplacer, algorithm = 'sha256') {
14+
constructor(expression, outputReplacer = noOpReplacer, identityReplacer = noOpReplacer) {
1215
this.expression = expression;
1316
this.outputReplacer = outputReplacer;
1417
this.identityReplacer = identityReplacer;
15-
this.algorithm = algorithm;
1618
this._cache = {};
1719
}
1820

1921
load(body) {
2022
const processed = body
2123
.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);
2925

3026
const identity = this.identityReplacer(
3127
identityHash,

0 commit comments

Comments
 (0)