mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-10 09:53:48 +00:00
refactor: obfuscated pincode in transit
This commit is contained in:
committed by
Carlos Valente
parent
3fdb1bd1c8
commit
4e6b833e10
@@ -0,0 +1,18 @@
|
||||
import { obfuscate, unobfuscate } from '../generic.js';
|
||||
|
||||
describe('obfuscate and unobfuscate', () => {
|
||||
it('should return the obfuscated string', () => {
|
||||
const str = 'abc123';
|
||||
const obfuscated = obfuscate(str);
|
||||
expect(obfuscated).not.toBe(str);
|
||||
expect(obfuscated.startsWith('_')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return the original string after obfuscating and unobfuscating', () => {
|
||||
const str = 'abc123';
|
||||
const obfuscated = obfuscate(str);
|
||||
const unobfuscated = unobfuscate(obfuscated);
|
||||
expect(unobfuscated).toBe(str);
|
||||
expect(unobfuscated.startsWith('_')).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -4,3 +4,39 @@ export function unpackError(error: unknown): string {
|
||||
}
|
||||
return String(error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obfuscate a string
|
||||
* Uses a variation of ROT13 that handles numeric values
|
||||
* @param str
|
||||
* @returns
|
||||
*/
|
||||
export function obfuscate(str: string): string {
|
||||
const obfuscated = str.replace(/[a-zA-Z0-9]/g, (c) => {
|
||||
if (/[a-zA-Z]/.test(c)) {
|
||||
// @ts-expect-error -- we use some javascript magic here
|
||||
return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
|
||||
} else {
|
||||
// @ts-expect-error -- we use some javascript magic here
|
||||
|
||||
return String.fromCharCode((c <= '4' ? 57 : 48) >= (c = c.charCodeAt(0) + 5) ? c : c - 10);
|
||||
}
|
||||
});
|
||||
if (str.startsWith('_')) {
|
||||
return obfuscated.replace('_', '');
|
||||
}
|
||||
return `_${obfuscated}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unobfoscate a string
|
||||
* Uses a variation of ROT13 that handles numeric values
|
||||
* @param str
|
||||
* @returns
|
||||
*/
|
||||
export function unobfuscate(str: string): string {
|
||||
if (str.startsWith('_')) {
|
||||
return obfuscate(str);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user