🐛 fix: Refactor toB64 function to toB64Url for URL-safe base64 encoding in VlessX25519Pair generation

This commit is contained in:
web 2025-09-15 21:12:25 -07:00
parent ebcebd7ddf
commit 8700cf6a91

View File

@ -1,11 +1,13 @@
import { x25519 } from '@noble/curves/ed25519'; import { x25519 } from '@noble/curves/ed25519';
const toB64 = (u8: Uint8Array): string => { const toB64Url = (u8: Uint8Array) =>
if (typeof Buffer !== 'undefined') return Buffer.from(u8).toString('base64'); (typeof Buffer !== 'undefined'
let s = ''; ? Buffer.from(u8).toString('base64')
for (const b of u8) s += String.fromCharCode(b); : btoa(String.fromCharCode(...u8))
return btoa(s); )
}; .replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/g, '');
export type VlessX25519Pair = { export type VlessX25519Pair = {
passwordB64: string; passwordB64: string;
@ -15,7 +17,7 @@ export type VlessX25519Pair = {
export function generateVlessX25519Pair(): VlessX25519Pair { export function generateVlessX25519Pair(): VlessX25519Pair {
const { secretKey, publicKey } = x25519.keygen(); const { secretKey, publicKey } = x25519.keygen();
return { return {
passwordB64: toB64(publicKey), passwordB64: toB64Url(publicKey),
privateKeyB64: toB64(secretKey), privateKeyB64: toB64Url(secretKey),
}; };
} }