<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Hashing on Daniel Mejía R.</title>
		<link>https://daniel-m.github.io/tags/hashing/</link>
		<description>Recent content in Hashing on Daniel Mejía R.</description>
		<generator>Hugo</generator>
		<language>en</language>
		
		
		
		
			<lastBuildDate>Thu, 19 Nov 2026 19:28:36 -0500</lastBuildDate>
		
			<atom:link href="https://daniel-m.github.io/tags/hashing/index.xml" rel="self" type="application/rss+xml" />
			<item>
				<title>A mystery using Python hashlib to calculate checksums</title>
				<link>https://daniel-m.github.io/2020/02/a-mystery-using-python-hashlib-to-calculate-checksums/</link>
				<pubDate>Tue, 04 Feb 2020 16:17:42 -0500</pubDate>
				<guid>https://daniel-m.github.io/2020/02/a-mystery-using-python-hashlib-to-calculate-checksums/</guid>
				<description>&lt;p&gt;Recently I was working with hashing some files in order to take checksums to avoid&#xA;working with data-corrupted files received at the endpoint of a web server.&#xA;It sounds easy, receive the file, calculate the hash, and emit the response.&#xA;I only needed something like &lt;code&gt;hash = calculate_hash(file)&lt;/code&gt;.&lt;br&gt;&#xA;A five minute research on &lt;a href=&#34;http://duckduckgo.com&#34;  class=&#34;external-link&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;duckduckgo&lt;/a&gt; lead me to write the following code,&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;import hashlib&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;def calculate_hash(filepath, chunk_size=4096, hash_method=hashlib.md5()):&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    hasher = hash_method&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    with open(filepath, &amp;#39;rb&amp;#39;) as fp:&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        buffer = fp.read(chunk_size)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        while len(buffer) &amp;gt; 0:&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            hasher.update(buffer)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            buffer = fp.read(chunk_size)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    return hasher.hexdigest()&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Open a file in binary mode and read it chunk-wise, updating a hash&#xA;with each chunk until EOF. Easy, right? WRONG!&lt;/p&gt;</description>
			</item>
	</channel>
</rss>
