topkat-utils
Version:
A comprehensive collection of TypeScript/JavaScript utility functions for common programming tasks. Includes validation, object manipulation, date handling, string formatting, and more. Zero dependencies, fully typed, and optimized for performance.
58 lines • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.simpleEncryptionDecode = exports.simpleEncryptionEncode = void 0;
const possibleChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_$';
const getCharAt = (n) => {
if (n >= possibleChars.length)
return possibleChars[n - possibleChars.length];
if (n < 0)
return possibleChars[n + possibleChars.length];
else
return possibleChars[n];
};
const getCharIndex = (char) => {
return possibleChars.indexOf(char);
};
/** simple and quick encoding, this is meant to obfuscate JWT encoding so we can
not decode it with something like https://jwt.io/
* * ⚠️🚸 special characters are actually not supported
* * string will be obfuscated so the characters are not in order
*/
function simpleEncryption(strR, secret, decode = false) {
const secretNb = secret.split('').map(char => char.charCodeAt(0) % 16 || -1);
const secretLength = secretNb.length;
const str = decode ? strR : strR.replace(/\./g, '$');
const parsed = str.split('').map((char, i) => {
const charI = getCharIndex(char); // convert character to number
const newIndex = charI + (secretNb[i % secretLength] * (decode ? -1 : 1)); // obfuscate
return getCharAt(newIndex);
}).join('');
return decode ? parsed.replace(/\$/g, '.') : parsed;
}
function simpleEncryptionEncode(str, secret) {
const strFromStart = [];
const strFromEnd = [];
str.split('').forEach((char, i) => {
if (i % 2)
strFromStart.push(char);
else
strFromEnd.push(char);
});
return simpleEncryption(strFromStart.join('') + strFromEnd.reverse().join(''), secret, false);
}
exports.simpleEncryptionEncode = simpleEncryptionEncode;
function simpleEncryptionDecode(str, secret) {
const decoded = simpleEncryption(str, secret, true);
const newStr = [];
const decodedArr = decoded.split('');
let i = 0;
while (decodedArr.length) {
if (i++ % 2)
newStr.push(decodedArr.shift());
else
newStr.push(decodedArr.pop());
}
return newStr.join('');
}
exports.simpleEncryptionDecode = simpleEncryptionDecode;
//# sourceMappingURL=encryptionUtils.js.map