Skip to content

Commit 047d322

Browse files
authored
feat: move buildStorageKeySuffix function to common and bump release (#207)
* move function to common and bump release * hash the API key for added obscurity * lint
1 parent abd99b7 commit 047d322

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
Variation,
6565
Environment,
6666
} from './interfaces';
67+
import { buildStorageKeySuffix } from './obfuscation';
6768
import {
6869
AttributeType,
6970
Attributes,
@@ -155,4 +156,7 @@ export {
155156

156157
// Test helpers
157158
decodePrecomputedFlag,
159+
160+
// Utilities
161+
buildStorageKeySuffix,
158162
};

src/obfuscation.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { IPrecomputedBandit } from './interfaces';
2-
import { decodeBase64, encodeBase64, obfuscatePrecomputedBanditMap } from './obfuscation';
2+
import {
3+
buildStorageKeySuffix,
4+
decodeBase64,
5+
encodeBase64,
6+
obfuscatePrecomputedBanditMap,
7+
} from './obfuscation';
38

49
describe('obfuscation', () => {
510
it('encodes strings to base64', () => {
@@ -10,6 +15,14 @@ describe('obfuscation', () => {
1015
expect(decodeBase64('NS4w')).toEqual('5.0');
1116
});
1217

18+
it('hashes API keys for storage key suffixes', () => {
19+
expect(buildStorageKeySuffix('MYKEY')).toEqual('ea89bd7e7594e5be');
20+
expect(buildStorageKeySuffix('MYKEY2')).toEqual('3fc4c6f1358c630f');
21+
expect(buildStorageKeySuffix('fwezo8v7nsotfizw3rtw===.3t wtw4ztwe3tjw8')).toEqual(
22+
'cf6c7dcee0987554',
23+
);
24+
});
25+
1326
it('encodes/decodes regex', () => {
1427
const regexes = ['.*@example.com', '.*@.*.com$', 'hello world'];
1528

src/obfuscation.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ export function getMD5Hash(input: string, salt = ''): string {
88
return new SparkMD5().append(salt).append(input).end();
99
}
1010

11+
/**
12+
* Builds a storage key suffix from an API key.
13+
* @param apiKey - The API key to build the suffix from
14+
* @returns A string suffix for storage keys
15+
* @public
16+
*/
17+
export function buildStorageKeySuffix(apiKey: string): string {
18+
// Note that we hash the API key and use the first 16 characters of the digest.
19+
const hashed = getMD5Hash(apiKey);
20+
return hashed.slice(0, 16);
21+
}
22+
1123
export function encodeBase64(input: string) {
1224
return base64.encode(input);
1325
}

0 commit comments

Comments
 (0)