Skip to content

Instantly share code, notes, and snippets.

@gsuberland
Created July 17, 2021 16:37
Show Gist options
  • Save gsuberland/4451025285403968a813a492e581e5db to your computer and use it in GitHub Desktop.
Save gsuberland/4451025285403968a813a492e581e5db to your computer and use it in GitHub Desktop.
Excel reserved file sharing hash function (C#)
/*
This computes the 16-bit hash used to protect Excel workbooks through most versions.
When trying to open such a sheet for editing, you'll get a message such as:
blah.xlsx is reserved by [user]. Enter password for write access, or open read only.
In xl\workbook.xml the hash is stored as follows: <fileSharing userName="user" reservationPassword="ABCD" />
In that case, you'd compare the output of the hash function to 0xABCD.
For 99% of cases you can just throw a wordlist like rockyou at any hash and it'll spit out hundreds of collisions.
*/
ushort Hash(string password)
{
if (password.Length > 15)
{
return 0;
}
ushort hash = 0;
byte[] pb = Encoding.ASCII.GetBytes(password);
for (int i = pb.Length - 1; i >= 0; i--)
{
byte pch = pb[i];
hash = (ushort)(((hash >> 14) & 0x01) | ((hash << 1) & 0x7fff));
hash ^= pch;
}
hash = (ushort)(((wPasswordHash >> 14) & 0x01) | ((hash << 1) & 0x7fff));
hash ^= (0x8000 | ('N' << 8) | 'K');
hash ^= (ushort)hash.Length;
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment