Skip to content

Instantly share code, notes, and snippets.

@guest271314
Created September 25, 2024 23:51
Show Gist options
  • Save guest271314/b6a74d63a3dcf53bc658b2825ba1d881 to your computer and use it in GitHub Desktop.
Save guest271314/b6a74d63a3dcf53bc658b2825ba1d881 to your computer and use it in GitHub Desktop.
Generate Chrome extension ID from path
// Generate Chrome extension ID from absolute path
// https://stackoverflow.com/a/26058672
// https://stackoverflow.com/a/61448618
// https://gist.github.com/dfkaye/84feac3688b110e698ad3b81713414a9
// generateIdForPath("/home/user/javascript").then(console.log).catch(console.error);
async function generateIdForPath(path) {
return [
...[
...new Uint8Array(
await (globalThis?.webcrypto?.subtle || globalThis?.crypto?.subtle).digest(
"SHA-256",
new TextEncoder().encode(path),
),
),
].map((u8) => u8.toString(16).padStart(2, "0")).join("").slice(0, 32),
]
.map((hex) => String.fromCharCode(parseInt(hex, 16) + "a".charCodeAt(0)))
.join(
"",
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment