mirror of
https://github.com/router-for-me/Cli-Proxy-API-Management-Center.git
synced 2026-02-03 03:10:50 +08:00
refactor(utils): simplify maskApiKey to show only 2 chars at each end
This commit is contained in:
@@ -4,16 +4,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 隐藏 API Key 中间部分
|
* 隐藏 API Key 中间部分,仅保留前后两位
|
||||||
*/
|
*/
|
||||||
export function maskApiKey(key: string, visibleChars: number = 4): string {
|
export function maskApiKey(key: string): string {
|
||||||
if (!key || key.length <= visibleChars * 2) {
|
if (!key) {
|
||||||
return key;
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const visibleChars = 2;
|
||||||
const start = key.slice(0, visibleChars);
|
const start = key.slice(0, visibleChars);
|
||||||
const end = key.slice(-visibleChars);
|
const end = key.slice(-visibleChars);
|
||||||
const maskedLength = Math.min(key.length - visibleChars * 2, 20);
|
const maskedLength = Math.max(key.length - visibleChars * 2, 1);
|
||||||
const masked = '*'.repeat(maskedLength);
|
const masked = '*'.repeat(maskedLength);
|
||||||
|
|
||||||
return `${start}${masked}${end}`;
|
return `${start}${masked}${end}`;
|
||||||
|
|||||||
Reference in New Issue
Block a user