<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged hashing</title>
<link>https://coderlegion.com/tag/hashing</link>
<description>Powered by Question2Answer</description>
<item>
<title>Why Git Blob IDs Mismatch Your File Checksums</title>
<link>https://coderlegion.com/31609/why-git-blob-ids-mismatch-your-file-checksums</link>
<description>&lt;p&gt;Comparing a raw file digest with a Git object identifier often reveals unexpected mismatches. Developers frequently notice that running a standard local hashing tool on a file produces a different checksum than the one returned by Git or the &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.github.com/en/rest/git/blobs&quot;&gt;GitHub Git blobs REST API&lt;/a&gt;. This discrepancy occurs because Git does not hash raw file bytes directly. Instead, Git constructs an object header that includes the object type and byte length, prepends it to the content, and hashes the combined payload.&lt;/p&gt;
&lt;p&gt;Quick solution:&lt;br&gt;
To calculate the exact Git blob object ID locally, prepend the object type and byte length header followed by a null byte before hashing the raw content.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;printf &quot;blob %d\0%s&quot; &quot;$(wc -c &amp;lt; file.txt)&quot; &quot;$(cat file.txt)&quot; | shasum
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Prerequisites include a standard Unix shell environment with &lt;code&gt;shasum&lt;/code&gt; or &lt;code&gt;sha1sum&lt;/code&gt;, and &lt;code&gt;wc&lt;/code&gt; for counting exact byte lengths. Be aware that trailing newlines or varying shell implementations of &lt;code&gt;printf&lt;/code&gt; can alter the generated byte stream.&lt;/p&gt;
&lt;h3 id=&quot;why-does-hashing-raw-file-bytes-produce-another-value-&quot;&gt;Why Does Hashing Raw File Bytes Produce Another Value?&lt;/h3&gt;
&lt;p&gt;When you run a command like &lt;code&gt;openssl dgst -sha1 file.txt&lt;/code&gt; or &lt;code&gt;shasum file.txt&lt;/code&gt;, the utility reads the raw byte sequence of the file and feeds it directly into the cryptographic hash function. This yields a pure content digest. Git, however, treats files as version-controlled objects rather than flat storage blocks. According to the &lt;a rel=&quot;nofollow&quot; href=&quot;https://git-scm.com/docs/git-hash-object&quot;&gt;Git hash-object documentation&lt;/a&gt;, every stored object receives a header composed of its type (such as &lt;code&gt;blob&lt;/code&gt;), a space, the size of the file in bytes, and a null byte (&lt;code&gt;\u0000&lt;/code&gt;) acting as a separator. &lt;/p&gt;
&lt;p&gt;For example, if a file contains the exact text &lt;code&gt;hello&lt;/code&gt;, its byte length is 5. Git constructs the internal byte sequence by joining the string &lt;code&gt;blob 5&lt;/code&gt;, a null byte, and the five characters of content. Hashing this combined stream produces the unique Git object identifier. Because the raw file digest lacks the header, metadata, and null separator, the resulting checksum diverges completely from the Git blob ID. Developers optimizing performance workflows often review prior indexing patterns, such as those detailed in our guide on &lt;a rel=&quot;nofollow&quot; href=&quot;https://raylabs.app/articles/google-search-console-content-prioritization/&quot;&gt;Google Search Console Content Prioritization&lt;/a&gt;, to ensure metadata indices align properly with storage identifiers.&lt;/p&gt;
&lt;h3 id=&quot;how-is-utf-8-byte-length-different-from-string-length-&quot;&gt;How Is UTF-8 Byte Length Different from String Length?&lt;/h3&gt;
&lt;p&gt;Calculating correct object identifiers requires precise byte counting rather than character or string length measurements. In ASCII text, one character equals one byte. However, modern codebases frequently utilize multi-byte UTF-8 characters, emojis, or specialized symbols. &lt;/p&gt;
&lt;p&gt;Consider a text file containing a single multi-byte character, such as the euro symbol (€). In UTF-8 encoding, this character occupies three bytes. If an application measures string length by counting logical characters, it might report a length of 1. If Git then calculates the header using that incorrect length, the resulting object hash will fail to match the repository database or API response. Engineers must always measure exact byte counts using tools like &lt;code&gt;wc -c&lt;/code&gt; or native byte-length methods in programming languages rather than relying on character-length functions.&lt;/p&gt;
&lt;h3 id=&quot;when-must-application-integrity-use-a-separate-digest-&quot;&gt;When Must Application Integrity Use a Separate Digest?&lt;/h3&gt;
&lt;p&gt;Relying on Git object IDs for external application security or file verification introduces architectural risks. Because Git object identifiers depend on the internal repository header and the chosen hashing algorithm (such as SHA-1 or newer experimental formats like SHA-256), they are tightly coupled to version control internals rather than raw file content.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option &lt;/th&gt;&lt;th align=&quot;left&quot;&gt;When to Use It &lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Trade-off or Failure Mode &lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Recommendation&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Git Blob ID&lt;/strong&gt; &lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Verifying repository object storage and API blob parity &lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Tied to Git metadata headers and vulnerable to SHA-1 collision concerns &lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use strictly for Git repository tooling and GitHub REST API integration&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Raw File Digest&lt;/strong&gt; &lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Verifying independent file downloads, caches, or releases &lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Ignores repository metadata and commit context &lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use standard SHA-256 or BLAKE2 for general application integrity checks&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Applications that require independent file verification, artifact signing, or secure distribution must rely on a dedicated raw digest computed directly from the file bytes. Treating Git object IDs as universal file checksums creates fragile verification pipelines that break when files are checked out with different line ending configurations, such as CRLF versus LF normalization.&lt;/p&gt;
&lt;h3 id=&quot;additional-scenarios-and-failure-modes&quot;&gt;Additional Scenarios and Failure Modes&lt;/h3&gt;
&lt;p&gt;In addition to the differences between raw file digests and Git blob IDs, there are other scenarios where the two may diverge. For instance, when a file is modified, the Git blob ID will change, but the raw file digest may not if the modification does not affect the file's content. Similarly, if a file is renamed or moved, the Git blob ID will remain the same, but the raw file digest may change if the file's content is modified during the rename or move operation.&lt;/p&gt;
&lt;h3 id=&quot;execution-and-verification-checklist&quot;&gt;Execution and Verification Checklist&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;[ ] Verify empty content matches git hash-object by running an empty file through the header generation command and comparing it against &lt;code&gt;git hash-object --stdin&lt;/code&gt;. Expected result: hashes match the known empty blob identifier &lt;code&gt;e69de29bb2d1d6434b8b29ae775ad8c2e48c5391&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;[ ] Test non-ASCII text using exact byte length calculations rather than character counts to ensure multi-byte symbols generate matching object IDs.&lt;/li&gt;
&lt;li&gt;[ ] Confirm a one-byte modification alters both the raw digest and the Git blob identifier, validating that minor edits propagate correctly through the header generation step.&lt;/li&gt;
&lt;li&gt;[ ] Document environment specifics, shell versions, and encoding parameters before deploying any automated script that compares raw file checksums against Git repository objects.&lt;/li&gt;
&lt;li&gt;[ ] Verify the behavior of Git blob IDs and raw file digests in different encoding scenarios, such as UTF-8, UTF-16, and UTF-32.&lt;/li&gt;
&lt;li&gt;[ ] Test the performance impact of using Git blob IDs versus raw file digests for large files and repositories.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;In conclusion, the discrepancy between raw file digests and Git blob IDs arises from the inclusion of a header in the Git object identifier. This header contains the object type, byte length, and a null separator, which are not present in the raw file digest. Understanding the differences between these two types of identifiers is crucial for ensuring data integrity and security in version control systems and application development. By using the correct type of identifier for the specific use case, developers can avoid potential pitfalls and ensure the reliability of their workflows.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/31609/why-git-blob-ids-mismatch-your-file-checksums</guid>
<pubDate>Mon, 19 Oct 2026 02:25:56 +0000</pubDate>
</item>
<item>
<title>Detailed Explanation of Hashing</title>
<link>https://coderlegion.com/4378/detailed-explanation-of-hashing</link>
<description>&lt;h3 id=&quot;the-library-card-revelation&quot;&gt;The Library Card Revelation&lt;/h3&gt;
&lt;p&gt;Picture this: You walk into a massive library containing millions of books. You need to find &quot;The Great Gatsby&quot; by F. Scott Fitzgerald. Without any organization system, you'd have to check every single book until you found it - potentially examining millions of volumes! But instead, you walk to a simple card catalog, look up &quot;Fitzgerald,&quot; and instantly get the exact shelf location: &lt;strong&gt;813.52 FIT&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This everyday library experience reveals the core magic of &lt;strong&gt;hashing&lt;/strong&gt; - one of computer science's most elegant and powerful concepts. Just as the library transforms &quot;F. Scott Fitzgerald&quot; into &quot;813.52 FIT&quot; for instant book location, hashing transforms any piece of data into a compact, fixed-size &quot;address&quot; that enables lightning-fast storage and retrieval.&lt;/p&gt;
&lt;p&gt;But here's where it gets fascinating: hashing isn't just about finding books. It's the invisible force securing your passwords, powering Bitcoin mining, enabling Google's instant search results, and ensuring the evidence in criminal trials hasn't been tampered with. Let's explore why this mathematical transformation has become one of the most important concepts in our digital world.&lt;/p&gt;
&lt;h3 id=&quot;what-is-hashing-&quot;&gt;What is Hashing?&lt;/h3&gt;
&lt;p&gt;At its heart, hashing is like creating a &lt;strong&gt;digital fingerprint&lt;/strong&gt; for any piece of information. Just as your fingerprint uniquely identifies you while being much smaller than your entire body, a hash value uniquely identifies data while being much smaller than the original information.&lt;/p&gt;
&lt;p&gt;Think of hashing as a special kind of &lt;strong&gt;one-way mathematical recipe&lt;/strong&gt;. You put ingredients (your data) into this recipe (the hash function), and it produces a specific dish (the hash value). The remarkable property? The same ingredients always produce the same dish, but you can't reverse-engineer the ingredients from the final dish.&lt;/p&gt;
&lt;p&gt;Here's a simple example using a basic hash function on the word &quot;HELLO&quot;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Input&lt;/strong&gt;: &quot;HELLO&quot;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hash Function Process&lt;/strong&gt;: H(72) + E(69) + L(76) + L(76) + O(79) = 372&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Final Hash&lt;/strong&gt;: 372 % 10 = &lt;strong&gt;2&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So &quot;HELLO&quot; gets the hash value &lt;strong&gt;2&lt;/strong&gt;. Every time we hash &quot;HELLO&quot; with this function, we'll always get &lt;strong&gt;2&lt;/strong&gt;. But if we know the answer is &lt;strong&gt;2&lt;/strong&gt;, we can't easily determine the original word was &quot;HELLO.&quot;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=17377332045595577418&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h4 id=&quot;the-three-essential-components&quot;&gt;The Three Essential Components&lt;/h4&gt;
&lt;p&gt;Every hashing operation consists of three fundamental parts, much like our library example:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Input&lt;/strong&gt; represents your original data - whether it's a word like &quot;HELLO,&quot; a password like &quot;MySecret123,&quot; or an entire file containing thousands of pages. This is the raw information you want to transform into a hash.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Hash Function&lt;/strong&gt; acts as the mathematical transformation engine. It's the &quot;recipe&quot; that converts your input into a hash value. Different hash functions use different mathematical approaches, from simple addition to complex cryptographic algorithms like SHA-256.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Hash Output&lt;/strong&gt; (or hash value/hash digest) is the fixed-size result produced by the hash function. Regardless of whether your input is a single character or a massive file, the hash output is always the same size for a given hash function - SHA-256 always produces exactly 256 bits (64 hexadecimal characters).&lt;/p&gt;
&lt;h3 id=&quot;the-four-fundamental-properties&quot;&gt;The Four Fundamental Properties&lt;/h3&gt;
&lt;p&gt;What makes hash functions mathematically fascinating? They possess three crucial properties that define their core nature.&lt;/p&gt;
&lt;h4 id=&quot;1-deterministic-same-input-always-produces-same-hash&quot;&gt;1. Deterministic: Same Input Always Produces Same Hash&lt;/h4&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=10342741628950529102&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;A hash function must be completely &lt;strong&gt;deterministic&lt;/strong&gt; - like a mathematical equation that always gives the same result. If you hash &quot;password123&quot; today and get the result &quot;7a58db2e,&quot; you must get exactly &quot;7a58db2e&quot; every time you hash &quot;password123&quot; in the future, on any computer, anywhere in the world.&lt;/p&gt;
&lt;p&gt;This isn't just convenient - it's mathematically essential. Without determinism, hash functions would be useless for verification, data integrity, or any practical application.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;SHA-256(&quot;Hello&quot;) = 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969&lt;/li&gt;
&lt;li&gt;&lt;em&gt;This result is exactly the same every single time, forever&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;2-irreversible-cannot-retrieve-input-from-hash-output&quot;&gt;2. Irreversible: Cannot Retrieve Input from Hash Output&lt;/h4&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=3039456990943485084&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;Hash functions are &lt;strong&gt;one-way mathematical transformations&lt;/strong&gt;. Given a hash output, it is computationally impossible to determine what the original input was. This isn't just difficult - it's mathematically designed to be irreversible.&lt;/p&gt;
&lt;p&gt;Think of it like this: if you blend a fruit smoothie, you can't &quot;un-blend&quot; it back into separate fruits. Similarly, hash functions &quot;blend&quot; input data in a way that cannot be undone.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why this matters:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Security&lt;/strong&gt;: Even if someone steals a database of password hashes, they can't directly recover the original passwords&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Privacy&lt;/strong&gt;: You can prove you know something without revealing what it is&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrity&lt;/strong&gt;: You can detect changes without storing the original data&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;3-collision-resistance-mathematically-impossible-to-avoid-all-collisions&quot;&gt;3. Collision Resistance: Mathematically Impossible to Avoid All Collisions&lt;/h4&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=7161267289101239660&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;Here's the fascinating mathematical reality: &lt;strong&gt;collisions are inevitable&lt;/strong&gt;. No hash function can avoid them entirely, and this isn't a design flaw - it's a mathematical certainty.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Pigeonhole Principle&lt;/strong&gt; proves this: If you have an infinite number of possible inputs (any data of any size) but only a finite number of possible hash outputs (say, 2^256 for SHA-256), then multiple inputs must eventually produce the same hash output.&lt;/p&gt;
&lt;p&gt;However, good hash functions make finding collisions practically impossible:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Weak Collision Resistance&lt;/strong&gt;: Given one input and its hash, it should be computationally infeasible to find a different input that produces the same hash&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Strong Collision Resistance&lt;/strong&gt;: It should be computationally infeasible to find any two different inputs that produce the same hash&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Real-world impact:&lt;/strong&gt; While collisions must exist mathematically, finding them in SHA-256 would require more computational power than exists on Earth.&lt;/p&gt;
&lt;h4 id=&quot;4-avalanche-effect&quot;&gt;4. Avalanche Effect&lt;/h4&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=3572740625406238028&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;While not one of the core mathematical requirements, the &lt;strong&gt;avalanche effect&lt;/strong&gt; makes hash functions particularly powerful for security applications. A tiny change in input should produce a dramatically different hash output.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;SHA-256(&quot;Hello World&quot;) = a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e&lt;/li&gt;
&lt;li&gt;SHA-256(&quot;Hello World!&quot;) = 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Adding one exclamation mark completely scrambled the entire hash - this makes tampering detection extremely reliable.&lt;/p&gt;
&lt;h3 id=&quot;building-your-first-hash-function&quot;&gt;Building Your First Hash Function&lt;/h3&gt;
&lt;p&gt;Let's create a simple hash function together to understand the underlying mechanics. We'll build something educational rather than cryptographically secure - think of it as learning to ride a bicycle before attempting motorcycle stunts.&lt;/p&gt;
&lt;h4 id=&quot;the-mid-square-method&quot;&gt;The Mid-Square Method&lt;/h4&gt;
&lt;p&gt;The &lt;strong&gt;mid-square method&lt;/strong&gt; provides an excellent learning example because you can calculate it by hand and observe how it &quot;mixes&quot; the input data:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;def mid_square_hash(key, table_size):
    &quot;&quot;&quot;
    A simple educational hash function using the mid-square method
    
    Step 1: Square the key
    Step 2: Extract middle digits
    Step 3: Take modulo of table size
    &quot;&quot;&quot;
    # Convert key to number if it's a string
    if isinstance(key, str):
        # Sum ASCII values of characters
        key = sum(ord(char) for char in key)
    
    # Step 1: Square the key
    squared = key * key
    print(f&quot;Step 1 - Squared: {key}² = {squared}&quot;)
    
    # Step 2: Extract middle digits
    # Convert to string to manipulate digits
    squared_str = str(squared)
    length = len(squared_str)
    
    # Take middle 3 digits (or available digits if shorter)
    if length &amp;gt;= 3:
        start = (length - 3) // 2
        middle = squared_str[start:start + 3]
    else:
        middle = squared_str
    
    middle_value = int(middle)
    print(f&quot;Step 2 - Middle digits: {middle_value}&quot;)
    
    # Step 3: Take modulo of table size
    hash_value = middle_value % table_size
    print(f&quot;Step 3 - Final hash: {middle_value} % {table_size} = {hash_value}&quot;)
    
    return hash_value

# Let's try it with the word &quot;CAT&quot;
result = mid_square_hash(&quot;CAT&quot;, 100)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Walking through &quot;CAT&quot;:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;C(67) + A(65) + T(84) = 216&lt;/li&gt;
&lt;li&gt;216² = 46,656&lt;/li&gt;
&lt;li&gt;Middle digits: 665&lt;/li&gt;
&lt;li&gt;665 % 100 = &lt;strong&gt;65&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So &quot;CAT&quot; hashes to index 65 in a table of size 100.&lt;/p&gt;
&lt;h4 id=&quot;understanding-why-this-works&quot;&gt;Understanding Why This Works&lt;/h4&gt;
&lt;p&gt;The mid-square method works because &lt;strong&gt;squaring amplifies differences&lt;/strong&gt; between inputs. Two similar numbers like 216 and 217 become 46,656 and 47,089 when squared - much more different than the original values. The middle digits capture this amplified difference while discarding potentially predictable patterns in the first and last digits.&lt;/p&gt;
&lt;p&gt;Notice the beautiful transformation: we started with a variable-length input (&quot;CAT&quot;) and produced a fixed-size hash output (65). This consistent output size is crucial - whether we hash a single word or an entire novel, our hash function produces the same size result.&lt;/p&gt;
&lt;p&gt;Try this yourself: What happens when you hash &quot;DOG&quot; using the same method? Can you predict the result before calculating?&lt;/p&gt;
&lt;h3 id=&quot;real-world-hashing-where-the-magic-happens&quot;&gt;Real-World Hashing: Where the Magic Happens&lt;/h3&gt;
&lt;p&gt;Now that you understand the mechanics, let's explore how hashing powers the digital world around us.&lt;/p&gt;
&lt;h4 id=&quot;password-security-your-digital-guardian&quot;&gt;Password Security: Your Digital Guardian&lt;/h4&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=12837003012514213284&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;Every time you log into a website, hashing protects your password through a sophisticated process. Websites don't actually store your password - they store its hash value instead.&lt;/p&gt;
&lt;p&gt;Here's how it works with &lt;strong&gt;bcrypt&lt;/strong&gt;, the gold standard for password hashing:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When you create an account:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You enter password: &quot;MySecret123&quot;&lt;/li&gt;
&lt;li&gt;bcrypt adds random salt: &quot;MySecret123&quot; + &quot;a8f5f167f44f4964e6c998dee827110c&quot;&lt;/li&gt;
&lt;li&gt;bcrypt hashes the result: &quot;$2b$10$3euPcmQFCiblsZeEu5s7p.9OVHgeHWFDk9nhMqZ0m/3pd/lhwZgES&quot;&lt;/li&gt;
&lt;li&gt;Only the hash gets stored in the database&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;When you log in:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You enter password: &quot;MySecret123&quot;&lt;/li&gt;
&lt;li&gt;System retrieves stored hash and extracts the salt&lt;/li&gt;
&lt;li&gt;System applies same process: hash(&quot;MySecret123&quot; + salt)&lt;/li&gt;
&lt;li&gt;If the new hash matches the stored hash, access granted&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The beautiful security here? Even if hackers steal the database, they only get meaningless hash values. They can't reverse-engineer your actual passwords because of hashing's one-way nature.&lt;/p&gt;
&lt;h4 id=&quot;bitcoin-mining-the-computational-race&quot;&gt;Bitcoin Mining: The Computational Race&lt;/h4&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=4962446084623898143&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;Bitcoin provides perhaps the most dramatic example of hashing in action. Bitcoin miners are essentially competing in a massive computational race to find specific hash values.&lt;/p&gt;
&lt;p&gt;Here's the challenge Bitcoin sets: Find a number (called a &lt;strong&gt;nonce&lt;/strong&gt;) that, when combined with transaction data and hashed with SHA-256, produces a hash value starting with a specific number of zeros.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example Bitcoin Mining Problem:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Block data: &quot;TransactionData + PreviousBlockHash + 1847563&quot;&lt;/li&gt;
&lt;li&gt;SHA-256 hash: 00000000a2c4ff82c4d4c3e2e1b8e8d7f6e5d4c3b2a1908f7e6d5c4b3a29180&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The hash starts with eight zeros! Finding such a hash requires trying millions or billions of different nonce values until one produces the required pattern. The current Bitcoin network performs over &lt;strong&gt;150 exahashes per second&lt;/strong&gt; - that's 150,000,000,000,000,000,000 hash operations every second globally.&lt;/p&gt;
&lt;h4 id=&quot;digital-forensics-the-truth-detector&quot;&gt;Digital Forensics: The Truth Detector&lt;/h4&gt;
&lt;p&gt;Law enforcement relies on hashing to ensure evidence integrity. When investigators seize a computer, they immediately calculate hash values for all files. These digital fingerprints prove that evidence hasn't been tampered with during analysis.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example forensic process:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Seize laptop containing potential evidence&lt;/li&gt;
&lt;li&gt;Calculate SHA-256 hash: &quot;4d186321c1a7f0f354b297e8914ab240c7e8a4c3a321b2c4d5e6f7a8b9c0d1e2&quot;&lt;/li&gt;
&lt;li&gt;Perform analysis on copied data&lt;/li&gt;
&lt;li&gt;Recalculate hash after analysis&lt;/li&gt;
&lt;li&gt;If hashes match, evidence integrity proven in court&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Any modification - even changing a single bit—would produce a completely different hash, immediately revealing tampering.&lt;/p&gt;
&lt;h3 id=&quot;the-collision-when-different-inputs-produce-same-hash-outputs&quot;&gt;The Collision: When Different Inputs Produce Same Hash Outputs&lt;/h3&gt;
&lt;p&gt;Now we encounter one of hashing's most fascinating challenges: &lt;strong&gt;hash collisions&lt;/strong&gt;. A collision occurs when two different inputs produce the same hash output. While this might seem like a flaw, it's actually inevitable and leads to some of the most elegant solutions in computer science.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=4517483406651643816&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h4 id=&quot;why-collisions-are-inevitable&quot;&gt;Why Collisions Are Inevitable&lt;/h4&gt;
&lt;p&gt;The &lt;strong&gt;pigeonhole principle&lt;/strong&gt; guarantees that collisions must occur. If you have more items than containers, at least one container must hold multiple items. Hash functions map an infinite input space (any possible data) to a finite output space (fixed-size hash values). Therefore, collisions are mathematically inevitable.&lt;/p&gt;
&lt;p&gt;Consider a simple example: if our hash function produces values from 0 to 99 (100 possibilities), and we hash 101 different inputs, at least two must produce the same hash output.&lt;/p&gt;
&lt;h4 id=&quot;the-birthday-paradox-in-action&quot;&gt;The Birthday Paradox in Action&lt;/h4&gt;
&lt;p&gt;The timing of collisions often surprises people due to the &lt;strong&gt;birthday paradox&lt;/strong&gt;. In a room of just 23 people, there's a 50% chance that two people share the same birthday, despite 365 possible days. Similarly, for a hash function that produces 1,000 different possible outputs, you'll likely encounter your first collision after hashing only about 37 different inputs - much sooner than intuition suggests.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;import math

def collision_probability(num_inputs, output_space_size):
    &quot;&quot;&quot;Calculate probability of at least one collision&quot;&quot;&quot;
    # Using birthday paradox formula
    probability = 1.0
    for i in range(num_inputs):
        probability *= (output_space_size - i) / output_space_size
    return 1.0 - probability

# For a hash function with 1000 possible outputs
output_space = 1000
for inputs in [10, 20, 30, 40, 50]:
    prob = collision_probability(inputs, output_space)
    print(f&quot;{inputs} inputs: {prob:.2%} collision probability&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;understanding-hash-collisions&quot;&gt;Understanding Hash Collisions&lt;/h4&gt;
&lt;p&gt;When collisions occur in cryptographic contexts, they represent potential security vulnerabilities. For example, if an attacker can find two different files that produce the same hash output, they might be able to substitute a malicious file for a legitimate one without detection.&lt;/p&gt;
&lt;p&gt;However, for most practical purposes, well-designed hash functions make finding collisions computationally infeasible. Modern cryptographic hash functions like SHA-256 are specifically designed so that finding two inputs that produce the same hash output would require astronomical computational resources.&lt;/p&gt;
&lt;h3 id=&quot;a-brief-history&quot;&gt;A Brief History&lt;/h3&gt;
&lt;p&gt;The evolution of hashing reflects humanity's growing need to organize and secure information.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=3630021403094321462&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Foundation Era (1950s-1960s)&lt;/strong&gt; Hans Peter Luhn at IBM pioneered hashing concepts while developing the KWIC (Key Word in Context) indexing system. His insight that data could be &quot;chopped up&quot; and reorganized for faster access laid the groundwork for modern hash tables.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=18146369295434555798&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Cryptographic Awakening (1990s)&lt;/strong&gt; Ronald Rivest's MD5 algorithm brought hashing into the security spotlight. Suddenly, hashing wasn't just about data storage - it was about protecting information integrity and authenticity. However, as computing power grew, MD5's vulnerabilities became apparent.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=7845876011914335569&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=14943703003305013983&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Security Arms Race (2000s-Present)&lt;/strong&gt; The development of SHA-1, then SHA-2, then SHA-3 represents an ongoing arms race between cryptographic innovation and attack sophistication. The 2017 &lt;strong&gt;SHAttered attack&lt;/strong&gt; demonstrated practical SHA-1 collisions, forcing widespread migration to stronger algorithms.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=9294116531704810794&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=4675970327265622669&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;Today's landscape includes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;SHA-256&lt;/strong&gt;: The current security standard&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SHA-3&lt;/strong&gt;: Mathematical diversity insurance&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;BLAKE3&lt;/strong&gt;: Performance breakthrough&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Argon2&lt;/strong&gt;: Memory-hard password protection&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;modern-applications-hashing-everywhere&quot;&gt;Modern Applications: Hashing Everywhere&lt;/h3&gt;
&lt;p&gt;Once you understand hashing, you'll start noticing it everywhere in modern technology.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Database Performance&lt;/strong&gt;: PostgreSQL uses hash indexes to transform &quot;WHERE user_id = 12345&quot; queries from linear scans through millions of records into instant O(1) lookups.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Load Balancing&lt;/strong&gt;: Companies like Netflix use consistent hashing to distribute billions of requests across thousands of servers. When you stream a movie, hashing determines which server handles your request.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Content Distribution&lt;/strong&gt;: Blockchain networks use hashing to create tamper-evident ledgers. Each block contains the hash of the previous block, creating an unbreakable chain of trust.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Data Deduplication&lt;/strong&gt;: Cloud storage services use hashing to detect duplicate files. If a million users upload the same photo, the service stores it once and uses hash values to reference it.&lt;/p&gt;
</description>
<category>Cybersecurity</category>
<guid isPermaLink="true">https://coderlegion.com/4378/detailed-explanation-of-hashing</guid>
<pubDate>Fri, 01 Aug 2025 21:59:58 +0000</pubDate>
</item>
</channel>
</rss>