Created
February 12, 2025 16:21
-
-
Save Diniboy1123/20c23b82696703a0429afc122cd0b0bd to your computer and use it in GitHub Desktop.
Computes the file hash just how Plex would do it. All credits for the logic go to: https://forums.plex.tv/t/how-is-plex-hash-calculated/904178/5
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
import hashlib | |
import sys | |
import os | |
def compute_plex_hash(file_path): | |
file_size = os.path.getsize(file_path) | |
with open(file_path, "rb") as f: | |
first_chunk = f.read(65536) | |
last_chunk = b"" | |
if file_size > 65536: | |
with open(file_path, "rb") as f: | |
f.seek(-65536, os.SEEK_END) | |
last_chunk = f.read(65536) | |
sha1_first = hashlib.sha1(first_chunk).hexdigest() | |
sha1_last = hashlib.sha1(last_chunk).hexdigest() if last_chunk else "" | |
input_string = f"{file_size}{sha1_first}{sha1_last}".encode() | |
final_hash = hashlib.sha1(input_string).hexdigest() | |
return final_hash | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
print("Usage: python plex_hash.py <file_path>") | |
sys.exit(1) | |
file_path = sys.argv[1] | |
result = compute_plex_hash(file_path) | |
print(f"Plex SHA-1: {result}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment