Release v0.4.1 #118

Merged
lamacchinadesiderante merged 11 commits from develop into main 2024-06-09 17:45:37 +00:00
1 changed files with 28 additions and 6 deletions
Showing only changes of commit 1a8c70cbee - Show all commits

View File

@ -21,8 +21,30 @@ const getEncodingKey = ():string => {
return process.env.ENCODING_KEY ?? DEFAULT_ENCODING_KEY;
}
// Funzione per codifica Base64 URL-safe
const base64UrlEncode = (input: string): string => {
return btoa(input)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}
// Funzione per decodifica Base64 URL-safe
const base64UrlDecode = (input: string): string => {
let base64 = input
.replace(/-/g, '+')
.replace(/_/g, '/');
// Aggiungi padding se necessario
while (base64.length % 4) {
base64 += '=';
}
return atob(base64);
}
export function encodeUrl(url: string): string {
const key = getEncodingKey()
const key = getEncodingKey();
// Convert the URL and key to UTF-8 bytes
const urlBytes = new TextEncoder().encode(url);
@ -31,16 +53,16 @@ export function encodeUrl(url: string): string {
// XOR the bytes of the URL with the key bytes
const encodedBytes = urlBytes.map((byte, index) => byte ^ keyBytes[index % keyBytes.length]);
// Convert the XORed bytes to a base64 string
// Convert the XORed bytes to a base64 URL-safe string
//@ts-ignore
return btoa(String.fromCharCode(...encodedBytes));
return base64UrlEncode(String.fromCharCode(...encodedBytes));
}
export function decodeUrl(encodedUrl: string): string {
const key = getEncodingKey()
const key = getEncodingKey();
// Decode the base64 string to get the XORed bytes
const encodedBytes = Uint8Array.from(atob(encodedUrl), char => char.charCodeAt(0));
// Decode the base64 URL-safe string to get the XORed bytes
const encodedBytes = Uint8Array.from(base64UrlDecode(encodedUrl), char => char.charCodeAt(0));
const keyBytes = new TextEncoder().encode(key);
// XOR the encoded bytes with the key bytes to get the original URL bytes