feat: 新增包文件

This commit is contained in:
duanshuwen
2025-11-15 22:41:50 +08:00
parent 7b65193e5c
commit 7ada85f175
104 changed files with 11273 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
import { createHash } from 'node:crypto'
/**
* Converts a normal hexadecimal string into the alphabet used by extensions.
* We use the characters 'a'-'p' instead of '0'-'f' to avoid ever having a
* completely numeric host, since some software interprets that as an IP address.
*
* @param id - The hexadecimal string to convert. This is modified in place.
*/
export function convertHexadecimalToIDAlphabet(id: string) {
let result = ''
for (const ch of id) {
const val = parseInt(ch, 16)
if (!isNaN(val)) {
result += String.fromCharCode('a'.charCodeAt(0) + val)
} else {
result += 'a'
}
}
return result
}
function generateIdFromHash(hash: Buffer): string {
const hashedId = hash.subarray(0, 16).toString('hex')
return convertHexadecimalToIDAlphabet(hashedId)
}
export function generateId(input: string): string {
const hash = createHash('sha256').update(input, 'base64').digest()
return generateIdFromHash(hash)
}