Last active
August 14, 2026 14:06
-
-
Save randomaccess3/dadb4a79369a59a33cf868f151e2af04 to your computer and use it in GitHub Desktop.
A vibe coded version of Microsoft's QuickXOR hash example code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://learn.microsoft.com/en-us/onedrive/developer/code-snippets/quickxorhash?view=odsp-graph-online | |
| # Usage: python C:\tools\quickxor.py -f file --base64 | |
| # This will present the hash of the file as you would see it in the OneDrive databases. | |
| import argparse | |
| import base64 | |
| MASK64 = 0xFFFFFFFFFFFFFFFF | |
| class QuickXorHash: | |
| BitsInLastCell = 32 | |
| Shift = 11 | |
| Threshold = 600 | |
| WidthInBits = 160 | |
| def __init__(self): | |
| self.initialize() | |
| def initialize(self): | |
| self._data = [0] * ((self.WidthInBits - 1) // 64 + 1) | |
| self._shiftSoFar = 0 | |
| self._lengthSoFar = 0 | |
| def hash_core(self, array: bytes, ibStart: int, cbSize: int): | |
| currentShift = self._shiftSoFar | |
| vectorArrayIndex = currentShift // 64 | |
| vectorOffset = currentShift % 64 | |
| iterations = min(cbSize, self.WidthInBits) | |
| for i in range(iterations): | |
| isLastCell = (vectorArrayIndex == len(self._data) - 1) | |
| bitsInVectorCell = self.BitsInLastCell if isLastCell else 64 | |
| if vectorOffset <= bitsInVectorCell - 8: | |
| j = ibStart + i | |
| while j < ibStart + cbSize: | |
| self._data[vectorArrayIndex] ^= (array[j] << vectorOffset) | |
| self._data[vectorArrayIndex] &= MASK64 | |
| j += self.WidthInBits | |
| else: | |
| index1 = vectorArrayIndex | |
| index2 = 0 if isLastCell else (vectorArrayIndex + 1) | |
| low = bitsInVectorCell - vectorOffset | |
| xoredByte = 0 | |
| j = ibStart + i | |
| while j < ibStart + cbSize: | |
| xoredByte ^= array[j] | |
| j += self.WidthInBits | |
| self._data[index1] ^= (xoredByte << vectorOffset) | |
| self._data[index1] &= MASK64 | |
| self._data[index2] ^= (xoredByte >> low) | |
| self._data[index2] &= MASK64 | |
| vectorOffset += self.Shift | |
| while vectorOffset >= bitsInVectorCell: | |
| vectorArrayIndex = 0 if isLastCell else (vectorArrayIndex + 1) | |
| vectorOffset -= bitsInVectorCell | |
| self._shiftSoFar = (self._shiftSoFar + | |
| self.Shift * (cbSize % self.WidthInBits)) % self.WidthInBits | |
| self._lengthSoFar += cbSize | |
| def hash_final(self): | |
| rgb = bytearray((self.WidthInBits - 1) // 8 + 1) | |
| for i in range(len(self._data) - 1): | |
| rgb[i * 8:(i + 1) * 8] = self._data[i].to_bytes(8, "little") | |
| last_index = len(self._data) - 1 | |
| last_bytes = self._data[last_index].to_bytes(8, "little") | |
| rgb[last_index * 8:] = last_bytes[:len(rgb) - last_index * 8] | |
| length_bytes = self._lengthSoFar.to_bytes(8, "little") | |
| start = (self.WidthInBits // 8) - len(length_bytes) | |
| for i in range(len(length_bytes)): | |
| rgb[start + i] ^= length_bytes[i] | |
| return bytes(rgb) | |
| def compute_hash(self, data: bytes): | |
| self.initialize() | |
| self.hash_core(data, 0, len(data)) | |
| return self.hash_final() | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Compute QuickXorHash of a file") | |
| parser.add_argument("-f", "--file", required=True, help="Path to input file") | |
| # ⭐ NEW OPTION: --base64 output | |
| parser.add_argument("--base64", action="store_true", | |
| help="Output hash in Base64 instead of hex") | |
| args = parser.parse_args() | |
| with open(args.file, "rb") as f: | |
| data = f.read() | |
| h = QuickXorHash() | |
| digest = h.compute_hash(data) | |
| if args.base64: | |
| print(base64.b64encode(digest).decode("ascii")) | |
| else: | |
| print(digest.hex()) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment