light-sentry-sdk/src/utils/hash.ts

21 lines
745 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export function hashString(str: string): number {
let hash = 5381;
let i = str.length;
while (i) {
hash = (hash * 33) ^ str.charCodeAt(--i);
}
return hash >>> 0;
}
export function md5(str: string): string {
// 使用 hashString 替代不安全的 md5
// 用于非加密场景ID生成、指纹计算
return hashString(str).toString(16);
}
export function computeFingerprint(type: string, message: string, frames: { filename: string; lineno?: number }[] = []): string {
const keyFrames = frames.slice(0, 3).map(f => `${f.filename}:${f.lineno || 0}`).join('|');
const normalizedMessage = message.replace(/['"]?\d+['"]?/g, '').replace(/\s+/g, ' ').trim();
return md5(`${type}:${normalizedMessage}:${keyFrames}`);
}