-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathanonymise.ts
53 lines (47 loc) · 1.47 KB
/
anonymise.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { createCipheriv, createHash } from 'crypto';
export function encrypt(s?: string): string {
const key = process.env.UNLEASH_ENCRYPTION_KEY;
const iv = process.env.UNLEASH_ENCRYPTION_IV;
if (!s || !key || !iv) {
return s ?? '';
}
const algorithm = 'aes-256-cbc';
const cipher = createCipheriv(
algorithm,
Buffer.from(key, 'hex'),
Buffer.from(iv, 'hex'),
);
const encrypted = cipher.update(s, 'utf8', 'hex') + cipher.final('hex');
return `${encrypted}@unleash.run`;
}
export function anonymise(s?: string): string {
if (!s) {
return '';
}
const hash = createHash('sha256')
.update(s, 'utf-8')
.digest('hex')
.slice(0, 9);
return `${hash}@unleash.run`;
}
export function anonymiseKeys<T>(object: T, keys: string[]): T {
if (typeof object !== 'object' || object === null) {
return object;
}
if (Array.isArray(object)) {
return object.map((item) => anonymiseKeys(item, keys)) as T;
} else {
return Object.keys(object).reduce((result, key) => {
if (
keys.includes(key) &&
result[key] !== undefined &&
result[key] !== null
) {
result[key] = anonymise(result[key]);
} else if (typeof result[key] === 'object') {
result[key] = anonymiseKeys(result[key], keys);
}
return result;
}, object);
}
}