# How is Plex hash calculated?

**URL:** <https://forums.plex.tv/t/how-is-plex-hash-calculated/904178>\
**Category:** General Discussions\
**Tags:** server-linux\
**Created:** [January 31, 2025, 11:36am UTC](https://forums.plex.tv/t/how-is-plex-hash-calculated/904178 "2025-01-31T11:36:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![diniboy](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/diniboy/32/155767_2.png) [@diniboy](https://forums.plex.tv/u/diniboy)\
**Post date:** [January 31, 2025, 11:36am UTC](https://forums.plex.tv/t/how-is-plex-hash-calculated/904178/1 "2025-01-31T11:36:14Z")

</div>

Hey,

One of my drives fell and while some data is gone, I could recover most of my data successfully. However the directory structure and filenames were lost. I still have all of them indexed in Plex though, so I thought I could just iterate over the `http://localhost:32400/library/metadata/<number>/tree?X-Plex-Token=redacted` API and that has a `file` and a `hash` value which would be perfect for me.

However I couldn’t figure out how is this hash generated exactly. It looks like SHA1 by the size of it, but it’s probably not for the whole file. I only found [this](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621) thread that mentions what it is, but if I just read the first 4096 bytes, I get a completely different hash, same like the guy in the original post.

If I could figure, that would help me a lot, I wouldn’t have to manually redo my entire library…

Thanks!

---

<div class="post-metadata">

**Author:** ![diniboy](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/diniboy/32/155767_2.png) [@diniboy](https://forums.plex.tv/u/diniboy)\
**Post date:** [January 31, 2025, 11:48am UTC](https://forums.plex.tv/t/how-is-plex-hash-calculated/904178/2 "2025-01-31T11:48:37Z")

</div>

Some more research: I added some media to Plex, grabbed the hash and wrote a quick program that reads one byte at a time, then calculates the hash and there was seemingly no match… So it can’t be simply just taking SHA1… 😢

Then to test the theory found in the other post where they claimed Plex only takes the first 4K block, so I used dd on the original file to produce a file with the same beginning, just cut off after 10 MB and the hash was different… So it can’t be just that. Because then the hash would be the same.

---

<div class="post-metadata">

**Author:** ![OttoKerner](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/ottokerner/32/10079_2.png) [@OttoKerner](https://forums.plex.tv/u/OttoKerner)\
**Post date:** [February 11, 2025, 11:33am UTC](https://forums.plex.tv/t/how-is-plex-hash-calculated/904178/3 "2025-02-11T11:33:11Z")

</div>

> [@diniboy](#):
>
> So it can’t be simply just taking SHA1

Try reading 4 KB from the beginning and 4k from the end. Concatenate these and compute the overall hash.

---

<div class="post-metadata">

**Author:** ![diniboy](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/diniboy/32/155767_2.png) [@diniboy](https://forums.plex.tv/u/diniboy)\
**Post date:** [February 12, 2025, 12:52am UTC](https://forums.plex.tv/t/how-is-plex-hash-calculated/904178/4 "2025-02-12T00:52:01Z")

</div>

I did that when I wrote the post, but missed it out. Thanks for pointing that out. Unfortunately the hash changes every time.  
I even went as far as to read 10 MB from the beginning and the end of the file and put it together, but unfortunately that also produced a different hash. So they must rely on something else too. I assume they hash the duration or the filesize (or both) or whatever as well.

---

<div class="post-metadata">

**Author:** ![gbooker02](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/gbooker02/32/9726_2.png) [@gbooker02](https://forums.plex.tv/u/gbooker02)\
**Post date:** [February 12, 2025, 2:12pm UTC](https://forums.plex.tv/t/how-is-plex-hash-calculated/904178/5 "2025-02-12T14:12:44Z")

</div>

There are a few steps:

- Create a string with the filesize in decimal
- Take the SHA1 hash of the first 64kB (65536 bytes) and append this to the string in hex.
- If the file is greater than 64kB, do the same with the last 64kB
- SHA1 hash this string

As an example, let’s look at a copy of [Big Buck Bunny](https://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_1080p_surround.avi):

- The file size is 928670754 bytes
- The first 64kB has a SHA1 hash of `87a82ca143a5d84ba4ba33f421f25fbac9811f89`
- The last 64kB has a SHA1 hash of `ce2f3dd83c1cc4ffa4deda5588a9118be004ce09`
- Take the SHA1 hash of `92867075487a82ca143a5d84ba4ba33f421f25fbac9811f89ce2f3dd83c1cc4ffa4deda5588a9118be004ce09`
- This is `782e3038c7290470c29320a840e5f92123912e56` which matches the `hash` column in the `media_parts` table if you add this exact file to it.

---

<div class="post-metadata">

**Author:** ![diniboy](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/diniboy/32/155767_2.png) [@diniboy](https://forums.plex.tv/u/diniboy)\
**Post date:** [February 12, 2025, 4:22pm UTC](https://forums.plex.tv/t/how-is-plex-hash-calculated/904178/6 "2025-02-12T16:22:46Z")

</div>

Fantastic. Words can’t express how thankful I am. Here is a simple Python script to hopefully reproduce the algorithm you mentioned: [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 · GitHub](https://gist.github.com/Diniboy1123/20c23b82696703a0429afc122cd0b0bd)

I could indeed get the correct hash for multiple files back.

PS: Seems like I was close to the solution. But I tried reading 4K and hashed the file size too… 😅

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [May 13, 2025, 4:23pm UTC](https://forums.plex.tv/t/how-is-plex-hash-calculated/904178/7 "2025-05-13T16:23:16Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
