<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MD5 | LogikDevelopment</title>
	<atom:link href="https://www.logikdev.com/tag/md5/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.logikdev.com</link>
	<description>&#34;Il n&#039;y a pas de problème, il n&#039;y a que des solutions. L&#039;esprit de l&#039;homme invente ensuite le problème.&#34; André Gide</description>
	<lastBuildDate>Tue, 25 Apr 2017 13:57:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>
	<item>
		<title>MD5 generates 31 bytes instead of 32</title>
		<link>https://www.logikdev.com/2012/12/12/md5-generates-31-bytes-instead-of-32/</link>
					<comments>https://www.logikdev.com/2012/12/12/md5-generates-31-bytes-instead-of-32/#respond</comments>
		
		<dc:creator><![CDATA[smoreau]]></dc:creator>
		<pubDate>Wed, 12 Dec 2012 12:12:25 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[32 bytes]]></category>
		<category><![CDATA[BigInteger]]></category>
		<category><![CDATA[leading zero]]></category>
		<category><![CDATA[MD5]]></category>
		<guid isPermaLink="false">https://www.logikdev.com/?p=1904</guid>

					<description><![CDATA[<p>I wrote the following method to generate MD5 hashes: This method takes a String and outputs a 32 character hash. However, depending on the value of the parameter, this method outputs a 31 character hash instead of 32. Why is that? It appears that it is because the leading zero is missing. But the question [&#8230;]</p>
<p>The post <a href="https://www.logikdev.com/2012/12/12/md5-generates-31-bytes-instead-of-32/">MD5 generates 31 bytes instead of 32</a> first appeared on <a href="https://www.logikdev.com">LogikDevelopment</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>I wrote the following method to generate MD5 hashes:</p>
<pre class="brush: java; title: ; notranslate">
private static String md5(String input) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(&quot;MD5&quot;);
    byte&#x5B;] messageDigest = md.digest(input.getBytes());
    BigInteger number = new BigInteger(1, messageDigest);
    return number.toString(16);
}
</pre>
<p>This method takes a <code>String</code> and outputs a 32 character hash.</p>
<p>However, depending on the value of the parameter, this method outputs a 31 character hash instead of 32.<br />
Why is that?</p>
<p>It appears that it is because the leading zero is missing. But the question remains! Why?<br />
This is simply because the <code>toString</code> method doesn&#8217;t know that we are expecting a leading zero. <code>BigInteger</code> stores just a number; it doesn&#8217;t know how many leading zeroes we need.</p>
<p>For example, how many leading zero would you expect with the following code?</p>
<pre class="brush: java; light: true; title: ; notranslate">
new BigInteger(&quot;33&quot;).toString()
</pre>
<p>In order to be sure MD5 generates 32 bytes, we could replace the <code>number.toString(16)</code> by:</p>
<pre class="brush: java; light: true; title: ; notranslate">
String.format(&quot;%032x&quot;, number)
</pre>
<p>So, the method will look like the following:</p>
<pre class="brush: java; title: ; notranslate">
private static String md5(String input) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(&quot;MD5&quot;);
    byte&#x5B;] messageDigest = md.digest(input.getBytes());
    BigInteger number = new BigInteger(1, messageDigest);
    return String.format(&quot;%032x&quot;, number);
}
</pre><p>The post <a href="https://www.logikdev.com/2012/12/12/md5-generates-31-bytes-instead-of-32/">MD5 generates 31 bytes instead of 32</a> first appeared on <a href="https://www.logikdev.com">LogikDevelopment</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.logikdev.com/2012/12/12/md5-generates-31-bytes-instead-of-32/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
