Release v0.4.1 #118
|
@ -21,8 +21,30 @@ const getEncodingKey = ():string => {
|
||||||
return process.env.ENCODING_KEY ?? DEFAULT_ENCODING_KEY;
|
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 {
|
export function encodeUrl(url: string): string {
|
||||||
const key = getEncodingKey()
|
const key = getEncodingKey();
|
||||||
|
|
||||||
// Convert the URL and key to UTF-8 bytes
|
// Convert the URL and key to UTF-8 bytes
|
||||||
const urlBytes = new TextEncoder().encode(url);
|
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
|
// XOR the bytes of the URL with the key bytes
|
||||||
const encodedBytes = urlBytes.map((byte, index) => byte ^ keyBytes[index % keyBytes.length]);
|
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
|
//@ts-ignore
|
||||||
return btoa(String.fromCharCode(...encodedBytes));
|
return base64UrlEncode(String.fromCharCode(...encodedBytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function decodeUrl(encodedUrl: string): string {
|
export function decodeUrl(encodedUrl: string): string {
|
||||||
const key = getEncodingKey()
|
const key = getEncodingKey();
|
||||||
|
|
||||||
// Decode the base64 string to get the XORed bytes
|
// Decode the base64 URL-safe string to get the XORed bytes
|
||||||
const encodedBytes = Uint8Array.from(atob(encodedUrl), char => char.charCodeAt(0));
|
const encodedBytes = Uint8Array.from(base64UrlDecode(encodedUrl), char => char.charCodeAt(0));
|
||||||
const keyBytes = new TextEncoder().encode(key);
|
const keyBytes = new TextEncoder().encode(key);
|
||||||
|
|
||||||
// XOR the encoded bytes with the key bytes to get the original URL bytes
|
// XOR the encoded bytes with the key bytes to get the original URL bytes
|
||||||
|
|
Loading…
Reference in New Issue