Skip to content

Instantly share code, notes, and snippets.

@Jiiks
Last active May 10, 2025 21:25
Show Gist options
  • Save Jiiks/6c3e6391ed236568c3d047bea1e6953c to your computer and use it in GitHub Desktop.
Save Jiiks/6c3e6391ed236568c3d047bea1e6953c to your computer and use it in GitHub Desktop.
Youtube sapisid hash gen
async function generateSapisidHash(datasyncId, full = true, origin = 'https://www.youtube.com') {
const sapisid = document.cookie.split('; ').find(cookie => cookie.startsWith('SAPISID')).split('=')[1];
const dsid = datasyncId.split('||')[1];
const ts = Math.floor(new Date().getTime() / 1E3);
const input = [dsid, ts, sapisid, origin].join(' ');
const digest = await window.crypto.subtle.digest('SHA-1', new TextEncoder('utf-8').encode(input)).then(buf => {
return Array.prototype.map.call(new Uint8Array(buf), x=>(('00'+x.toString(16)).slice(-2))).join('');
});
const hash = `${ts}_${digest}_u`;
if(full) return `SAPISIDHASH ${hash} SAPISID1PHASH ${hash} SAPISID3PHASH ${hash}`;
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment