Files
C-Plus-Plus/d5/d96/md5_8cpp.html
2025-07-09 10:53:16 +00:00

761 lines
64 KiB
HTML

<!-- HTML header for doxygen 1.12.0-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.14.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>TheAlgorithms/C++: hashing/md5.cpp File Reference</title>
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@xpack-3rd-party/doxygen-awesome-css@2.2.0-1/doxygen-awesome-darkmode-toggle.js"></script>
<script type="text/javascript">
DoxygenAwesomeDarkModeToggle.init()
</script>
<script type="text/javascript" src="../../clipboard.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<script type="text/javascript" src="../../cookie.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/javascript">
window.MathJax = {
options: {
ignoreHtmlClass: 'tex2jax_ignore',
processHtmlClass: 'tex2jax_process'
},
loader: {
load: ['[tex]/ams']
},
tex: {
macros: {},
packages: ['base','configmacros','ams']
}
};
</script>
<script type="text/javascript" id="MathJax-script" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../doxygen-awesome.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="../../project_logo.png"/></td>
<td id="projectalign">
<div id="projectname">TheAlgorithms/C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">All the algorithms implemented in C++</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.14.0 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
</script>
<script type="text/javascript">
$(function() { codefold.init(); });
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('../../',true,false,'search.php','Search',true);
$(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(function(){initNavTree('d5/d96/md5_8cpp.html','../../',''); });
</script>
<div id="container">
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="headertitle"><div class="title">md5.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>Simple C++ implementation of the <a href="https://en.wikipedia.org/wiki/MD5" target="_blank">MD5 Hashing Algorithm</a>
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;algorithm&gt;</code><br />
<code>#include &lt;array&gt;</code><br />
<code>#include &lt;cassert&gt;</code><br />
<code>#include &lt;cstdint&gt;</code><br />
<code>#include &lt;cstring&gt;</code><br />
<code>#include &lt;iostream&gt;</code><br />
<code>#include &lt;string&gt;</code><br />
<code>#include &lt;vector&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for md5.cpp:</div>
<div class="dyncontent">
<div class="center"><div class="zoom"><iframe scrolling="no" loading="lazy" frameborder="0" src="../../dd/d0f/md5_8cpp__incl.svg" width="100%" height="383"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div></div>
</div>
</div>
<p><a href="../../d5/d96/md5_8cpp_source.html">Go to the source code of this file.</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 id="header-namespaces" class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:hashing" id="r_hashing"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d0c/namespacehashing.html">hashing</a></td></tr>
<tr class="memdesc:d6/d0c/namespacehashing"><td class="mdescLeft">&#160;</td><td class="mdescRight">Used for assert. <br /></td></tr>
<tr class="memitem:MD5" id="r_MD5"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d43/namespace_m_d5.html">MD5</a></td></tr>
<tr class="memdesc:dd/d43/namespace_m_d5"><td class="mdescLeft">&#160;</td><td class="mdescRight">Functions for the <a href="https://en.wikipedia.org/wiki/MD5" target="_blank">MD5</a> algorithm implementation. <br /></td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 id="header-func-members" class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:add2bf982d5dd64a45d13cba52c2b371d" id="r_add2bf982d5dd64a45d13cba52c2b371d"><td class="memItemLeft" align="right" valign="top">uint32_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#add2bf982d5dd64a45d13cba52c2b371d">hashing::md5::leftRotate32bits</a> (uint32_t n, std::size_t rotate)</td></tr>
<tr class="memdesc:add2bf982d5dd64a45d13cba52c2b371d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Rotates the bits of a 32-bit unsigned integer. <br /></td></tr>
<tr class="memitem:af8e96bde0183c4b0a7ff04668f11e446" id="r_af8e96bde0183c4b0a7ff04668f11e446"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#af8e96bde0183c4b0a7ff04668f11e446">hashing::md5::isBigEndian</a> ()</td></tr>
<tr class="memdesc:af8e96bde0183c4b0a7ff04668f11e446"><td class="mdescLeft">&#160;</td><td class="mdescRight">Checks whether integers are stored as big endian or not. <br /></td></tr>
<tr class="memitem:a694712c9665051ba52b686387b87a689" id="r_a694712c9665051ba52b686387b87a689"><td class="memItemLeft" align="right" valign="top">uint32_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a694712c9665051ba52b686387b87a689">hashing::md5::toLittleEndian32</a> (uint32_t n)</td></tr>
<tr class="memdesc:a694712c9665051ba52b686387b87a689"><td class="mdescLeft">&#160;</td><td class="mdescRight">Sets 32-bit integer to little-endian if needed. <br /></td></tr>
<tr class="memitem:a6be48c1e6e742f9bd329f501d61dcaef" id="r_a6be48c1e6e742f9bd329f501d61dcaef"><td class="memItemLeft" align="right" valign="top">uint64_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a6be48c1e6e742f9bd329f501d61dcaef">hashing::md5::toLittleEndian64</a> (uint64_t n)</td></tr>
<tr class="memdesc:a6be48c1e6e742f9bd329f501d61dcaef"><td class="mdescLeft">&#160;</td><td class="mdescRight">Sets 64-bit integer to little-endian if needed. <br /></td></tr>
<tr class="memitem:aaee69c6136a841043f956de32116e348" id="r_aaee69c6136a841043f956de32116e348"><td class="memItemLeft" align="right" valign="top">std::string&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#aaee69c6136a841043f956de32116e348">hashing::md5::sig2hex</a> (void *sig)</td></tr>
<tr class="memdesc:aaee69c6136a841043f956de32116e348"><td class="mdescLeft">&#160;</td><td class="mdescRight">Transforms the 128-bit <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> signature into a 32 char hex string. <br /></td></tr>
<tr class="memitem:a5341431cef7fcfc33794326e1deb2425" id="r_a5341431cef7fcfc33794326e1deb2425"><td class="memItemLeft" align="right" valign="top">void *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a5341431cef7fcfc33794326e1deb2425">hashing::md5::hash_bs</a> (const void *input_bs, uint64_t input_size)</td></tr>
<tr class="memdesc:a5341431cef7fcfc33794326e1deb2425"><td class="mdescLeft">&#160;</td><td class="mdescRight">The <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> algorithm itself, taking in a bytestring. <br /></td></tr>
<tr class="memitem:a88705fab5f92adf3427b4cf76ff33edb" id="r_a88705fab5f92adf3427b4cf76ff33edb"><td class="memItemLeft" align="right" valign="top">void *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a88705fab5f92adf3427b4cf76ff33edb">hashing::md5::hash</a> (const std::string &amp;message)</td></tr>
<tr class="memdesc:a88705fab5f92adf3427b4cf76ff33edb"><td class="mdescLeft">&#160;</td><td class="mdescRight">Converts the string to bytestring and calls the main algorithm. <br /></td></tr>
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d" id="r_aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self-test implementations of well-known <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> hashes. <br /></td></tr>
<tr class="memitem:ac9e1a11f44135b890dd10a00e73b5661" id="r_ac9e1a11f44135b890dd10a00e73b5661"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ac9e1a11f44135b890dd10a00e73b5661">interactive</a> ()</td></tr>
<tr class="memdesc:ac9e1a11f44135b890dd10a00e73b5661"><td class="mdescLeft">&#160;</td><td class="mdescRight">Puts user in a loop where inputs can be given and <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> hash will be computed and printed. <br /></td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <br /></td></tr>
</table>
<a name="details" id="details"></a><h2 id="header-details" class="groupheader">Detailed Description</h2>
<div class="textblock"><p>Simple C++ implementation of the <a href="https://en.wikipedia.org/wiki/MD5" target="_blank">MD5 Hashing Algorithm</a> </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/tGautot" target="_blank">tGautot</a></dd></dl>
<p>The <a href="https://en.wikipedia.org/wiki/MD5" target="_blank">MD5 Algorithm</a> is a hashing algorithm which was designed in 1991 by <a href="https://en.wikipedia.org/wiki/Ron_Rivest" target="_blank">Ronal Rivest</a>.</p>
<p><a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> is one of the most used hashing algorithm there is. Some of its use cases are:</p><ol type="1">
<li>Providing checksum for downloaded software</li>
<li>Store salted password</li>
</ol>
<p>However <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> has be know to be cryptographically weak for quite some time, yet it is still widely used. This weakness was exploited by the <a href="https://en.wikipedia.org/wiki/Flame_(malware)" target="_blank">Flame Malware</a> in 2012</p>
<h3 class="doxsection"><a class="anchor" id="autotoc_md83"></a>
Algorithm</h3>
<p>First of all, all values are expected to be in [little endian] (<a href="https://en.wikipedia.org/wiki/Endianness">https://en.wikipedia.org/wiki/Endianness</a>). This is especially important when using part of the bytestring as an integer.</p>
<p>The first step of the algorithm is to pad the message for its length to be a multiple of 64 (bytes). This is done by first adding 0x80 (10000000) and then only zeroes until the last 8 bytes must be filled, where then the 64 bit size of the input will be added</p>
<p>Once this is done, the algo breaks down this padded message into 64 bytes chunks. Each chunk is used for one <em>round</em>, a round breaks the chunk into 16 blocks of 4 bytes. During these rounds the algorithm will update its 128 bit state (represented by 4 ints: A,B,C,D) For more precisions on these operations please see the <a href="https://en.wikipedia.org/wiki/MD5#Algorithm" target="_blank">Wikipedia aritcle</a>. The signature given by <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> is its 128 bit state once all rounds are done. </p><dl class="section note"><dt>Note</dt><dd>This is a simple implementation for a byte string but some implmenetations can work on bytestream, messages of unknown length. </dd></dl>
<p class="definition">Definition in file <a class="el" href="../../d5/d96/md5_8cpp_source.html">md5.cpp</a>.</p>
</div><a name="doc-func-members" id="doc-func-members"></a><h2 id="header-doc-func-members" class="groupheader">Function Documentation</h2>
<a id="a88705fab5f92adf3427b4cf76ff33edb" name="a88705fab5f92adf3427b4cf76ff33edb"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a88705fab5f92adf3427b4cf76ff33edb">&#9670;&#160;</a></span>hash()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void * hashing::md5::hash </td>
<td>(</td>
<td class="paramtype">const std::string &amp;</td> <td class="paramname"><span class="paramname"><em>message</em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Converts the string to bytestring and calls the main algorithm. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">message</td><td>Plain character message to hash </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>void* Pointer to the <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> signature </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../d5/d96/md5_8cpp_source.html#l00288">288</a> of file <a class="el" href="../../d5/d96/md5_8cpp_source.html">md5.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 288</span> {</div>
<div class="line"><span class="lineno"> 289</span> <span class="keywordflow">return</span> <a class="code hl_function" href="#a5341431cef7fcfc33794326e1deb2425">hash_bs</a>(&amp;message[0], message.size());</div>
<div class="line"><span class="lineno"> 290</span>}</div>
<div class="ttc" id="amd5_8cpp_html_a5341431cef7fcfc33794326e1deb2425"><div class="ttname"><a href="#a5341431cef7fcfc33794326e1deb2425">hashing::md5::hash_bs</a></div><div class="ttdeci">void * hash_bs(const void *input_bs, uint64_t input_size)</div><div class="ttdoc">The MD5 algorithm itself, taking in a bytestring.</div><div class="ttdef"><b>Definition</b> <a href="../../d5/d96/md5_8cpp_source.html#l00139">md5.cpp:139</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a5341431cef7fcfc33794326e1deb2425" name="a5341431cef7fcfc33794326e1deb2425"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a5341431cef7fcfc33794326e1deb2425">&#9670;&#160;</a></span>hash_bs()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void * hashing::md5::hash_bs </td>
<td>(</td>
<td class="paramtype">const void *</td> <td class="paramname"><span class="paramname"><em>input_bs</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint64_t</td> <td class="paramname"><span class="paramname"><em>input_size</em></span>&#160;)</td>
</tr>
</table>
</div><div class="memdoc">
<p>The <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> algorithm itself, taking in a bytestring. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">input_bs</td><td>The bytestring to hash </td></tr>
<tr><td class="paramname">input_size</td><td>The size (in BYTES) of the input </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>void* Pointer to the 128-bit signature </dd></dl>
<p>Values of K are pseudo-random and used to "salt" each round The values can be obtained by the following python code </p><div class="fragment"><div class="line"><span class="keyword">from</span> math <span class="keyword">import</span> floor, sin</div>
<div class="line"> </div>
<div class="line"><span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(64):</div>
<div class="line"> print(floor(2**32 * abs(sin(i+1))))</div>
</div><!-- fragment -->
<p class="definition">Definition at line <a class="el" href="../../d5/d96/md5_8cpp_source.html#l00139">139</a> of file <a class="el" href="../../d5/d96/md5_8cpp_source.html">md5.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 139</span> {</div>
<div class="line"><span class="lineno"> 140</span> <span class="keyword">auto</span>* input = <span class="keyword">static_cast&lt;</span><span class="keyword">const </span>uint8_t*<span class="keyword">&gt;</span>(input_bs);</div>
<div class="line"><span class="lineno"> 141</span> </div>
<div class="line"><span class="lineno"> 142</span> <span class="comment">// Step 0: Initial Data (Those are decided in the MD5 protocol)</span></div>
<div class="line"><span class="lineno"> 143</span> <span class="comment">// s is the shift used in the leftrotate each round</span></div>
<div class="line"><span class="lineno"> 144</span> std::array&lt;uint32_t, 64&gt; s = {</div>
<div class="line"><span class="lineno"> 145</span> 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,</div>
<div class="line"><span class="lineno"> 146</span> 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,</div>
<div class="line"><span class="lineno"> 147</span> 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,</div>
<div class="line"><span class="lineno"> 148</span> 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};</div>
<div class="line"><span class="lineno"> 149</span> <span class="comment">// K is pseudo-random values used each round</span></div>
<div class="line"><span class="lineno"> 150</span> <span class="comment">// The values can be obtained by the following python code:</span></div>
<div class="line"><span class="lineno"> 151</span></div>
<div class="line"><span class="lineno"> 162</span> std::array&lt;uint32_t, 64&gt; K = {</div>
<div class="line"><span class="lineno"> 163</span> 3614090360, 3905402710, 606105819, 3250441966, 4118548399, 1200080426,</div>
<div class="line"><span class="lineno"> 164</span> 2821735955, 4249261313, 1770035416, 2336552879, 4294925233, 2304563134,</div>
<div class="line"><span class="lineno"> 165</span> 1804603682, 4254626195, 2792965006, 1236535329, 4129170786, 3225465664,</div>
<div class="line"><span class="lineno"> 166</span> 643717713, 3921069994, 3593408605, 38016083, 3634488961, 3889429448,</div>
<div class="line"><span class="lineno"> 167</span> 568446438, 3275163606, 4107603335, 1163531501, 2850285829, 4243563512,</div>
<div class="line"><span class="lineno"> 168</span> 1735328473, 2368359562, 4294588738, 2272392833, 1839030562, 4259657740,</div>
<div class="line"><span class="lineno"> 169</span> 2763975236, 1272893353, 4139469664, 3200236656, 681279174, 3936430074,</div>
<div class="line"><span class="lineno"> 170</span> 3572445317, 76029189, 3654602809, 3873151461, 530742520, 3299628645,</div>
<div class="line"><span class="lineno"> 171</span> 4096336452, 1126891415, 2878612391, 4237533241, 1700485571, 2399980690,</div>
<div class="line"><span class="lineno"> 172</span> 4293915773, 2240044497, 1873313359, 4264355552, 2734768916, 1309151649,</div>
<div class="line"><span class="lineno"> 173</span> 4149444226, 3174756917, 718787259, 3951481745};</div>
<div class="line"><span class="lineno"> 174</span> </div>
<div class="line"><span class="lineno"> 175</span> <span class="comment">// The initial 128-bit state</span></div>
<div class="line"><span class="lineno"> 176</span> uint32_t a0 = 0x67452301, A = 0;</div>
<div class="line"><span class="lineno"> 177</span> uint32_t b0 = 0xefcdab89, B = 0;</div>
<div class="line"><span class="lineno"> 178</span> uint32_t c0 = 0x98badcfe, C = 0;</div>
<div class="line"><span class="lineno"> 179</span> uint32_t d0 = 0x10325476, D = 0;</div>
<div class="line"><span class="lineno"> 180</span> </div>
<div class="line"><span class="lineno"> 181</span> <span class="comment">// Step 1: Processing the bytestring</span></div>
<div class="line"><span class="lineno"> 182</span> </div>
<div class="line"><span class="lineno"> 183</span> <span class="comment">// First compute the size the padded message will have</span></div>
<div class="line"><span class="lineno"> 184</span> <span class="comment">// so it is possible to allocate the right amount of memory</span></div>
<div class="line"><span class="lineno"> 185</span> uint64_t padded_message_size = 0;</div>
<div class="line"><span class="lineno"> 186</span> <span class="keywordflow">if</span> (input_size % 64 &lt; 56) {</div>
<div class="line"><span class="lineno"> 187</span> padded_message_size = input_size + 64 - (input_size % 64);</div>
<div class="line"><span class="lineno"> 188</span> } <span class="keywordflow">else</span> {</div>
<div class="line"><span class="lineno"> 189</span> padded_message_size = input_size + 128 - (input_size % 64);</div>
<div class="line"><span class="lineno"> 190</span> }</div>
<div class="line"><span class="lineno"> 191</span> </div>
<div class="line"><span class="lineno"> 192</span> std::vector&lt;uint8_t&gt; padded_message(padded_message_size);</div>
<div class="line"><span class="lineno"> 193</span> </div>
<div class="line"><span class="lineno"> 194</span> <span class="comment">// Beginning of the padded message is the original message</span></div>
<div class="line"><span class="lineno"> 195</span> std::copy(input, input + input_size, padded_message.begin());</div>
<div class="line"><span class="lineno"> 196</span> </div>
<div class="line"><span class="lineno"> 197</span> <span class="comment">// Afterwards comes a single 1 bit and then only zeroes</span></div>
<div class="line"><span class="lineno"> 198</span> padded_message[input_size] = 1 &lt;&lt; 7; <span class="comment">// 10000000</span></div>
<div class="line"><span class="lineno"> 199</span> <span class="keywordflow">for</span> (uint64_t i = input_size; i % 64 != 56; i++) {</div>
<div class="line"><span class="lineno"> 200</span> <span class="keywordflow">if</span> (i == input_size) {</div>
<div class="line"><span class="lineno"> 201</span> <span class="keywordflow">continue</span>; <span class="comment">// pass first iteration</span></div>
<div class="line"><span class="lineno"> 202</span> }</div>
<div class="line"><span class="lineno"> 203</span> padded_message[i] = 0;</div>
<div class="line"><span class="lineno"> 204</span> }</div>
<div class="line"><span class="lineno"> 205</span> </div>
<div class="line"><span class="lineno"> 206</span> <span class="comment">// We then have to add the 64-bit size of the message at the end</span></div>
<div class="line"><span class="lineno"> 207</span> <span class="comment">// When there is a conversion from int to bytestring or vice-versa</span></div>
<div class="line"><span class="lineno"> 208</span> <span class="comment">// We always need to make sure it is little endian</span></div>
<div class="line"><span class="lineno"> 209</span> uint64_t input_bitsize_le = <a class="code hl_function" href="#a6be48c1e6e742f9bd329f501d61dcaef">toLittleEndian64</a>(input_size * 8);</div>
<div class="line"><span class="lineno"> 210</span> <span class="keywordflow">for</span> (uint8_t i = 0; i &lt; 8; i++) {</div>
<div class="line"><span class="lineno"> 211</span> padded_message[padded_message_size - 8 + i] =</div>
<div class="line"><span class="lineno"> 212</span> (input_bitsize_le &gt;&gt; (56 - 8 * i)) &amp; 0xFF;</div>
<div class="line"><span class="lineno"> 213</span> }</div>
<div class="line"><span class="lineno"> 214</span> </div>
<div class="line"><span class="lineno"> 215</span> <span class="comment">// Already allocate memory for blocks</span></div>
<div class="line"><span class="lineno"> 216</span> std::array&lt;uint32_t, 16&gt; blocks{};</div>
<div class="line"><span class="lineno"> 217</span> </div>
<div class="line"><span class="lineno"> 218</span> <span class="comment">// Rounds</span></div>
<div class="line"><span class="lineno"> 219</span> <span class="keywordflow">for</span> (uint64_t chunk = 0; chunk * 64 &lt; padded_message_size; chunk++) {</div>
<div class="line"><span class="lineno"> 220</span> <span class="comment">// First, build the 16 32-bits blocks from the chunk</span></div>
<div class="line"><span class="lineno"> 221</span> <span class="keywordflow">for</span> (uint8_t bid = 0; bid &lt; 16; bid++) {</div>
<div class="line"><span class="lineno"> 222</span> blocks[bid] = 0;</div>
<div class="line"><span class="lineno"> 223</span> </div>
<div class="line"><span class="lineno"> 224</span> <span class="comment">// Having to build a 32-bit word from 4-bit words</span></div>
<div class="line"><span class="lineno"> 225</span> <span class="comment">// Add each and shift them to the left</span></div>
<div class="line"><span class="lineno"> 226</span> <span class="keywordflow">for</span> (uint8_t cid = 0; cid &lt; 4; cid++) {</div>
<div class="line"><span class="lineno"> 227</span> blocks[bid] = (blocks[bid] &lt;&lt; 8) +</div>
<div class="line"><span class="lineno"> 228</span> padded_message[chunk * 64 + bid * 4 + cid];</div>
<div class="line"><span class="lineno"> 229</span> }</div>
<div class="line"><span class="lineno"> 230</span> }</div>
<div class="line"><span class="lineno"> 231</span> </div>
<div class="line"><span class="lineno"> 232</span> A = a0;</div>
<div class="line"><span class="lineno"> 233</span> B = b0;</div>
<div class="line"><span class="lineno"> 234</span> C = c0;</div>
<div class="line"><span class="lineno"> 235</span> D = d0;</div>
<div class="line"><span class="lineno"> 236</span> </div>
<div class="line"><span class="lineno"> 237</span> <span class="comment">// Main &quot;hashing&quot; loop</span></div>
<div class="line"><span class="lineno"> 238</span> <span class="keywordflow">for</span> (uint8_t i = 0; i &lt; 64; i++) {</div>
<div class="line"><span class="lineno"> 239</span> uint32_t F = 0, <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a2f67508d5f392b0321772169342c98ad">g</a> = 0;</div>
<div class="line"><span class="lineno"> 240</span> <span class="keywordflow">if</span> (i &lt; 16) {</div>
<div class="line"><span class="lineno"> 241</span> F = (B &amp; C) | ((~B) &amp; D);</div>
<div class="line"><span class="lineno"> 242</span> <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a2f67508d5f392b0321772169342c98ad">g</a> = i;</div>
<div class="line"><span class="lineno"> 243</span> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (i &lt; 32) {</div>
<div class="line"><span class="lineno"> 244</span> F = (D &amp; B) | ((~D) &amp; C);</div>
<div class="line"><span class="lineno"> 245</span> <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a2f67508d5f392b0321772169342c98ad">g</a> = (5 * i + 1) % 16;</div>
<div class="line"><span class="lineno"> 246</span> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (i &lt; 48) {</div>
<div class="line"><span class="lineno"> 247</span> F = B ^ C ^ D;</div>
<div class="line"><span class="lineno"> 248</span> <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a2f67508d5f392b0321772169342c98ad">g</a> = (3 * i + 5) % 16;</div>
<div class="line"><span class="lineno"> 249</span> } <span class="keywordflow">else</span> {</div>
<div class="line"><span class="lineno"> 250</span> F = C ^ (B | (~D));</div>
<div class="line"><span class="lineno"> 251</span> <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a2f67508d5f392b0321772169342c98ad">g</a> = (7 * i) % 16;</div>
<div class="line"><span class="lineno"> 252</span> }</div>
<div class="line"><span class="lineno"> 253</span> </div>
<div class="line"><span class="lineno"> 254</span> <span class="comment">// Update the accumulators</span></div>
<div class="line"><span class="lineno"> 255</span> F += A + K[i] + <a class="code hl_function" href="#a694712c9665051ba52b686387b87a689">toLittleEndian32</a>(blocks[g]);</div>
<div class="line"><span class="lineno"> 256</span> </div>
<div class="line"><span class="lineno"> 257</span> A = D;</div>
<div class="line"><span class="lineno"> 258</span> D = C;</div>
<div class="line"><span class="lineno"> 259</span> C = B;</div>
<div class="line"><span class="lineno"> 260</span> B += <a class="code hl_function" href="#add2bf982d5dd64a45d13cba52c2b371d">leftRotate32bits</a>(F, s[i]);</div>
<div class="line"><span class="lineno"> 261</span> }</div>
<div class="line"><span class="lineno"> 262</span> <span class="comment">// Update the state with this chunk&#39;s hash</span></div>
<div class="line"><span class="lineno"> 263</span> a0 += A;</div>
<div class="line"><span class="lineno"> 264</span> b0 += B;</div>
<div class="line"><span class="lineno"> 265</span> c0 += C;</div>
<div class="line"><span class="lineno"> 266</span> d0 += D;</div>
<div class="line"><span class="lineno"> 267</span> }</div>
<div class="line"><span class="lineno"> 268</span> </div>
<div class="line"><span class="lineno"> 269</span> <span class="comment">// Build signature from state</span></div>
<div class="line"><span class="lineno"> 270</span> <span class="comment">// Note, any type could be used for the signature</span></div>
<div class="line"><span class="lineno"> 271</span> <span class="comment">// uint8_t was used to make the 16 bytes obvious</span></div>
<div class="line"><span class="lineno"> 272</span> <span class="comment">// The sig needs to be little endian</span></div>
<div class="line"><span class="lineno"> 273</span> <span class="keyword">auto</span>* sig = <span class="keyword">new</span> uint8_t[16];</div>
<div class="line"><span class="lineno"> 274</span> <span class="keywordflow">for</span> (uint8_t i = 0; i &lt; 4; i++) {</div>
<div class="line"><span class="lineno"> 275</span> sig[i] = (a0 &gt;&gt; (8 * i)) &amp; 0xFF;</div>
<div class="line"><span class="lineno"> 276</span> sig[i + 4] = (b0 &gt;&gt; (8 * i)) &amp; 0xFF;</div>
<div class="line"><span class="lineno"> 277</span> sig[i + 8] = (c0 &gt;&gt; (8 * i)) &amp; 0xFF;</div>
<div class="line"><span class="lineno"> 278</span> sig[i + 12] = (d0 &gt;&gt; (8 * i)) &amp; 0xFF;</div>
<div class="line"><span class="lineno"> 279</span> }</div>
<div class="line"><span class="lineno"> 280</span> </div>
<div class="line"><span class="lineno"> 281</span> <span class="keywordflow">return</span> sig;</div>
<div class="line"><span class="lineno"> 282</span>}</div>
<div class="ttc" id="acomposite__simpson__rule_8cpp_html_a2f67508d5f392b0321772169342c98ad"><div class="ttname"><a href="../../d4/d18/composite__simpson__rule_8cpp.html#a2f67508d5f392b0321772169342c98ad">numerical_methods::simpson_method::g</a></div><div class="ttdeci">double g(double x)</div><div class="ttdoc">Another test function.</div><div class="ttdef"><b>Definition</b> <a href="../../d4/d18/composite__simpson__rule_8cpp_source.html#l00115">composite_simpson_rule.cpp:115</a></div></div>
<div class="ttc" id="amd5_8cpp_html_a694712c9665051ba52b686387b87a689"><div class="ttname"><a href="#a694712c9665051ba52b686387b87a689">hashing::md5::toLittleEndian32</a></div><div class="ttdeci">uint32_t toLittleEndian32(uint32_t n)</div><div class="ttdoc">Sets 32-bit integer to little-endian if needed.</div><div class="ttdef"><b>Definition</b> <a href="../../d5/d96/md5_8cpp_source.html#l00090">md5.cpp:90</a></div></div>
<div class="ttc" id="amd5_8cpp_html_a6be48c1e6e742f9bd329f501d61dcaef"><div class="ttname"><a href="#a6be48c1e6e742f9bd329f501d61dcaef">hashing::md5::toLittleEndian64</a></div><div class="ttdeci">uint64_t toLittleEndian64(uint64_t n)</div><div class="ttdoc">Sets 64-bit integer to little-endian if needed.</div><div class="ttdef"><b>Definition</b> <a href="../../d5/d96/md5_8cpp_source.html#l00103">md5.cpp:103</a></div></div>
<div class="ttc" id="amd5_8cpp_html_add2bf982d5dd64a45d13cba52c2b371d"><div class="ttname"><a href="#add2bf982d5dd64a45d13cba52c2b371d">hashing::md5::leftRotate32bits</a></div><div class="ttdeci">uint32_t leftRotate32bits(uint32_t n, std::size_t rotate)</div><div class="ttdoc">Rotates the bits of a 32-bit unsigned integer.</div><div class="ttdef"><b>Definition</b> <a href="../../d5/d96/md5_8cpp_source.html#l00067">md5.cpp:67</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="ac9e1a11f44135b890dd10a00e73b5661" name="ac9e1a11f44135b890dd10a00e73b5661"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ac9e1a11f44135b890dd10a00e73b5661">&#9670;&#160;</a></span>interactive()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">void interactive </td>
<td>(</td>
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel static">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Puts user in a loop where inputs can be given and <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> hash will be computed and printed. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../d5/d96/md5_8cpp_source.html#l00352">352</a> of file <a class="el" href="../../d5/d96/md5_8cpp_source.html">md5.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 352</span> {</div>
<div class="line"><span class="lineno"> 353</span> <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div>
<div class="line"><span class="lineno"> 354</span> std::string input;</div>
<div class="line"><span class="lineno"> 355</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Enter a message to be hashed (Ctrl-C to exit): &quot;</span></div>
<div class="line"><span class="lineno"> 356</span> &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 357</span> std::getline(std::cin, input);</div>
<div class="line"><span class="lineno"> 358</span> <span class="keywordtype">void</span>* sig = <a class="code hl_function" href="#a88705fab5f92adf3427b4cf76ff33edb">hashing::md5::hash</a>(input);</div>
<div class="line"><span class="lineno"> 359</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Hash is: &quot;</span> &lt;&lt; <a class="code hl_function" href="#aaee69c6136a841043f956de32116e348">hashing::md5::sig2hex</a>(sig) &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 360</span> </div>
<div class="line"><span class="lineno"> 361</span> <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div>
<div class="line"><span class="lineno"> 362</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Want to enter another message? (y/n) &quot;</span>;</div>
<div class="line"><span class="lineno"> 363</span> std::getline(std::cin, input);</div>
<div class="line"><span class="lineno"> 364</span> <span class="keywordflow">if</span> (input.compare(<span class="stringliteral">&quot;y&quot;</span>) == 0) {</div>
<div class="line"><span class="lineno"> 365</span> <span class="keywordflow">break</span>;</div>
<div class="line"><span class="lineno"> 366</span> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (input.compare(<span class="stringliteral">&quot;n&quot;</span>) == 0) {</div>
<div class="line"><span class="lineno"> 367</span> <span class="keywordflow">return</span>;</div>
<div class="line"><span class="lineno"> 368</span> }</div>
<div class="line"><span class="lineno"> 369</span> }</div>
<div class="line"><span class="lineno"> 370</span> }</div>
<div class="line"><span class="lineno"> 371</span>}</div>
<div class="ttc" id="amd5_8cpp_html_a88705fab5f92adf3427b4cf76ff33edb"><div class="ttname"><a href="#a88705fab5f92adf3427b4cf76ff33edb">hashing::md5::hash</a></div><div class="ttdeci">void * hash(const std::string &amp;message)</div><div class="ttdoc">Converts the string to bytestring and calls the main algorithm.</div><div class="ttdef"><b>Definition</b> <a href="../../d5/d96/md5_8cpp_source.html#l00288">md5.cpp:288</a></div></div>
<div class="ttc" id="amd5_8cpp_html_aaee69c6136a841043f956de32116e348"><div class="ttname"><a href="#aaee69c6136a841043f956de32116e348">hashing::md5::sig2hex</a></div><div class="ttdeci">std::string sig2hex(void *sig)</div><div class="ttdoc">Transforms the 128-bit MD5 signature into a 32 char hex string.</div><div class="ttdef"><b>Definition</b> <a href="../../d5/d96/md5_8cpp_source.html#l00123">md5.cpp:123</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="af8e96bde0183c4b0a7ff04668f11e446" name="af8e96bde0183c4b0a7ff04668f11e446"></a>
<h2 class="memtitle"><span class="permalink"><a href="#af8e96bde0183c4b0a7ff04668f11e446">&#9670;&#160;</a></span>isBigEndian()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">bool hashing::md5::isBigEndian </td>
<td>(</td>
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Checks whether integers are stored as big endian or not. </p>
<dl class="section note"><dt>Note</dt><dd>Taken from <a href="https://stackoverflow.com/a/1001373" target="_blank">this</a> StackOverflow post </dd></dl>
<dl class="section return"><dt>Returns</dt><dd>true IF integers are detected to work as big-endian </dd>
<dd>
false IF integers are detected to work as little-endian </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../d5/d96/md5_8cpp_source.html#l00077">77</a> of file <a class="el" href="../../d5/d96/md5_8cpp_source.html">md5.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 77</span> {</div>
<div class="line"><span class="lineno"> 78</span> <span class="keyword">union </span>{</div>
<div class="line"><span class="lineno"> 79</span> uint32_t i;</div>
<div class="line"><span class="lineno"> 80</span> std::array&lt;char, 4&gt; c;</div>
<div class="line"><span class="lineno"> 81</span> } bint = {0x01020304};</div>
<div class="line"><span class="lineno"> 82</span> </div>
<div class="line"><span class="lineno"> 83</span> <span class="keywordflow">return</span> bint.c[0] == 1;</div>
<div class="line"><span class="lineno"> 84</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="add2bf982d5dd64a45d13cba52c2b371d" name="add2bf982d5dd64a45d13cba52c2b371d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#add2bf982d5dd64a45d13cba52c2b371d">&#9670;&#160;</a></span>leftRotate32bits()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">uint32_t hashing::md5::leftRotate32bits </td>
<td>(</td>
<td class="paramtype">uint32_t</td> <td class="paramname"><span class="paramname"><em>n</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">std::size_t</td> <td class="paramname"><span class="paramname"><em>rotate</em></span>&#160;)</td>
</tr>
</table>
</div><div class="memdoc">
<p>Rotates the bits of a 32-bit unsigned integer. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">n</td><td>Integer to rotate </td></tr>
<tr><td class="paramname">rotate</td><td>How many bits for the rotation </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>uint32_t The rotated integer </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../d5/d96/md5_8cpp_source.html#l00067">67</a> of file <a class="el" href="../../d5/d96/md5_8cpp_source.html">md5.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 67</span> {</div>
<div class="line"><span class="lineno"> 68</span> <span class="keywordflow">return</span> (n &lt;&lt; rotate) | (n &gt;&gt; (32 - rotate));</div>
<div class="line"><span class="lineno"> 69</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">void</td> <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function. </p>
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../d5/d96/md5_8cpp_source.html#l00377">377</a> of file <a class="el" href="../../d5/d96/md5_8cpp_source.html">md5.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 377</span> {</div>
<div class="line"><span class="lineno"> 378</span> <a class="code hl_function" href="#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><span class="lineno"> 379</span> </div>
<div class="line"><span class="lineno"> 380</span> <span class="comment">// Launch interactive mode where user can input messages and see</span></div>
<div class="line"><span class="lineno"> 381</span> <span class="comment">// their hash</span></div>
<div class="line"><span class="lineno"> 382</span> <a class="code hl_function" href="#ac9e1a11f44135b890dd10a00e73b5661">interactive</a>();</div>
<div class="line"><span class="lineno"> 383</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 384</span>}</div>
<div class="ttc" id="amd5_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Self-test implementations of well-known MD5 hashes.</div><div class="ttdef"><b>Definition</b> <a href="../../d5/d96/md5_8cpp_source.html#l00298">md5.cpp:298</a></div></div>
<div class="ttc" id="amd5_8cpp_html_ac9e1a11f44135b890dd10a00e73b5661"><div class="ttname"><a href="#ac9e1a11f44135b890dd10a00e73b5661">interactive</a></div><div class="ttdeci">static void interactive()</div><div class="ttdoc">Puts user in a loop where inputs can be given and MD5 hash will be computed and printed.</div><div class="ttdef"><b>Definition</b> <a href="../../d5/d96/md5_8cpp_source.html#l00352">md5.cpp:352</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="aaee69c6136a841043f956de32116e348" name="aaee69c6136a841043f956de32116e348"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aaee69c6136a841043f956de32116e348">&#9670;&#160;</a></span>sig2hex()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">std::string hashing::md5::sig2hex </td>
<td>(</td>
<td class="paramtype">void *</td> <td class="paramname"><span class="paramname"><em>sig</em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Transforms the 128-bit <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> signature into a 32 char hex string. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">sig</td><td>The <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> signature (Expected 16 bytes) </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>std::string The hex signature </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../d5/d96/md5_8cpp_source.html#l00123">123</a> of file <a class="el" href="../../d5/d96/md5_8cpp_source.html">md5.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 123</span> {</div>
<div class="line"><span class="lineno"> 124</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* hexChars = <span class="stringliteral">&quot;0123456789abcdef&quot;</span>;</div>
<div class="line"><span class="lineno"> 125</span> <span class="keyword">auto</span>* intsig = <span class="keyword">static_cast&lt;</span>uint8_t*<span class="keyword">&gt;</span>(sig);</div>
<div class="line"><span class="lineno"> 126</span> std::string hex = <span class="stringliteral">&quot;&quot;</span>;</div>
<div class="line"><span class="lineno"> 127</span> <span class="keywordflow">for</span> (uint8_t i = 0; i &lt; 16; i++) {</div>
<div class="line"><span class="lineno"> 128</span> hex.push_back(hexChars[(intsig[i] &gt;&gt; 4) &amp; 0xF]);</div>
<div class="line"><span class="lineno"> 129</span> hex.push_back(hexChars[(intsig[i]) &amp; 0xF]);</div>
<div class="line"><span class="lineno"> 130</span> }</div>
<div class="line"><span class="lineno"> 131</span> <span class="keywordflow">return</span> hex;</div>
<div class="line"><span class="lineno"> 132</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="aa8dca7b867074164d5f45b0f3851269d" name="aa8dca7b867074164d5f45b0f3851269d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">&#9670;&#160;</a></span>test()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">void test </td>
<td>(</td>
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel static">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Self-test implementations of well-known <a class="el" href="../../dd/d43/namespace_m_d5.html" title="Functions for the MD5 algorithm implementation.">MD5</a> hashes. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../d5/d96/md5_8cpp_source.html#l00298">298</a> of file <a class="el" href="../../d5/d96/md5_8cpp_source.html">md5.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 298</span> {</div>
<div class="line"><span class="lineno"> 299</span> <span class="comment">// Hashes empty string and stores signature</span></div>
<div class="line"><span class="lineno"> 300</span> <span class="keywordtype">void</span>* sig = <a class="code hl_function" href="#a88705fab5f92adf3427b4cf76ff33edb">hashing::md5::hash</a>(<span class="stringliteral">&quot;&quot;</span>);</div>
<div class="line"><span class="lineno"> 301</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Hashing empty string&quot;</span> &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 302</span> <span class="comment">// Prints signature hex representation</span></div>
<div class="line"><span class="lineno"> 303</span> std::cout &lt;&lt; <a class="code hl_function" href="#aaee69c6136a841043f956de32116e348">hashing::md5::sig2hex</a>(sig) &lt;&lt; std::endl &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 304</span> <span class="comment">// Test with cassert whether sig is correct from the expected value</span></div>
<div class="line"><span class="lineno"> 305</span> assert(<a class="code hl_function" href="#aaee69c6136a841043f956de32116e348">hashing::md5::sig2hex</a>(sig).<a class="code hl_function" href="../../d4/d7a/shell__sort2_8cpp.html#a7eb77daed2cf1513f6d68c47a1c2db1c">compare</a>(</div>
<div class="line"><span class="lineno"> 306</span> <span class="stringliteral">&quot;d41d8cd98f00b204e9800998ecf8427e&quot;</span>) == 0);</div>
<div class="line"><span class="lineno"> 307</span> </div>
<div class="line"><span class="lineno"> 308</span> <span class="comment">// Hashes &quot;The quick brown fox jumps over the lazy dog&quot; and stores signature</span></div>
<div class="line"><span class="lineno"> 309</span> <span class="keywordtype">void</span>* sig2 =</div>
<div class="line"><span class="lineno"> 310</span> <a class="code hl_function" href="#a88705fab5f92adf3427b4cf76ff33edb">hashing::md5::hash</a>(<span class="stringliteral">&quot;The quick brown fox jumps over the lazy dog&quot;</span>);</div>
<div class="line"><span class="lineno"> 311</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Hashing The quick brown fox jumps over the lazy dog&quot;</span></div>
<div class="line"><span class="lineno"> 312</span> &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 313</span> <span class="comment">// Prints signature hex representation</span></div>
<div class="line"><span class="lineno"> 314</span> std::cout &lt;&lt; <a class="code hl_function" href="#aaee69c6136a841043f956de32116e348">hashing::md5::sig2hex</a>(sig2) &lt;&lt; std::endl &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 315</span> <span class="comment">// Test with cassert whether sig is correct from the expected value</span></div>
<div class="line"><span class="lineno"> 316</span> assert(<a class="code hl_function" href="#aaee69c6136a841043f956de32116e348">hashing::md5::sig2hex</a>(sig2).<a class="code hl_function" href="../../d4/d7a/shell__sort2_8cpp.html#a7eb77daed2cf1513f6d68c47a1c2db1c">compare</a>(</div>
<div class="line"><span class="lineno"> 317</span> <span class="stringliteral">&quot;9e107d9d372bb6826bd81d3542a419d6&quot;</span>) == 0);</div>
<div class="line"><span class="lineno"> 318</span> </div>
<div class="line"><span class="lineno"> 319</span> <span class="comment">// Hashes &quot;The quick brown fox jumps over the lazy dog.&quot; (notice the</span></div>
<div class="line"><span class="lineno"> 320</span> <span class="comment">// additional period) and stores signature</span></div>
<div class="line"><span class="lineno"> 321</span> <span class="keywordtype">void</span>* sig3 =</div>
<div class="line"><span class="lineno"> 322</span> <a class="code hl_function" href="#a88705fab5f92adf3427b4cf76ff33edb">hashing::md5::hash</a>(<span class="stringliteral">&quot;The quick brown fox jumps over the lazy dog.&quot;</span>);</div>
<div class="line"><span class="lineno"> 323</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Hashing &quot;</span></div>
<div class="line"><span class="lineno"> 324</span> <span class="stringliteral">&quot;The quick brown fox jumps over the lazy dog.&quot;</span></div>
<div class="line"><span class="lineno"> 325</span> &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 326</span> <span class="comment">// Prints signature hex representation</span></div>
<div class="line"><span class="lineno"> 327</span> std::cout &lt;&lt; <a class="code hl_function" href="#aaee69c6136a841043f956de32116e348">hashing::md5::sig2hex</a>(sig3) &lt;&lt; std::endl &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 328</span> <span class="comment">// Test with cassert whether sig is correct from the expected value</span></div>
<div class="line"><span class="lineno"> 329</span> assert(<a class="code hl_function" href="#aaee69c6136a841043f956de32116e348">hashing::md5::sig2hex</a>(sig3).<a class="code hl_function" href="../../d4/d7a/shell__sort2_8cpp.html#a7eb77daed2cf1513f6d68c47a1c2db1c">compare</a>(</div>
<div class="line"><span class="lineno"> 330</span> <span class="stringliteral">&quot;e4d909c290d0fb1ca068ffaddf22cbd0&quot;</span>) == 0);</div>
<div class="line"><span class="lineno"> 331</span> </div>
<div class="line"><span class="lineno"> 332</span> <span class="comment">// Hashes &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&quot;</span></div>
<div class="line"><span class="lineno"> 333</span> <span class="comment">// and stores signature</span></div>
<div class="line"><span class="lineno"> 334</span> <span class="keywordtype">void</span>* sig4 = <a class="code hl_function" href="#a88705fab5f92adf3427b4cf76ff33edb">hashing::md5::hash</a>(</div>
<div class="line"><span class="lineno"> 335</span> <span class="stringliteral">&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&quot;</span>);</div>
<div class="line"><span class="lineno"> 336</span> std::cout</div>
<div class="line"><span class="lineno"> 337</span> &lt;&lt; <span class="stringliteral">&quot;Hashing &quot;</span></div>
<div class="line"><span class="lineno"> 338</span> <span class="stringliteral">&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&quot;</span></div>
<div class="line"><span class="lineno"> 339</span> &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 340</span> <span class="comment">// Prints signature hex representation</span></div>
<div class="line"><span class="lineno"> 341</span> std::cout &lt;&lt; <a class="code hl_function" href="#aaee69c6136a841043f956de32116e348">hashing::md5::sig2hex</a>(sig4) &lt;&lt; std::endl &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 342</span> <span class="comment">// Test with cassert whether sig is correct from the expected value</span></div>
<div class="line"><span class="lineno"> 343</span> assert(<a class="code hl_function" href="#aaee69c6136a841043f956de32116e348">hashing::md5::sig2hex</a>(sig4).<a class="code hl_function" href="../../d4/d7a/shell__sort2_8cpp.html#a7eb77daed2cf1513f6d68c47a1c2db1c">compare</a>(</div>
<div class="line"><span class="lineno"> 344</span> <span class="stringliteral">&quot;d174ab98d277d9f5a5611c2c9f419d9f&quot;</span>) == 0);</div>
<div class="line"><span class="lineno"> 345</span>}</div>
<div class="ttc" id="ashell__sort2_8cpp_html_a7eb77daed2cf1513f6d68c47a1c2db1c"><div class="ttname"><a href="../../d4/d7a/shell__sort2_8cpp.html#a7eb77daed2cf1513f6d68c47a1c2db1c">compare</a></div><div class="ttdeci">int compare(const void *a, const void *b)</div><div class="ttdef"><b>Definition</b> <a href="../../d4/d7a/shell__sort2_8cpp_source.html#l00087">shell_sort2.cpp:87</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a694712c9665051ba52b686387b87a689" name="a694712c9665051ba52b686387b87a689"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a694712c9665051ba52b686387b87a689">&#9670;&#160;</a></span>toLittleEndian32()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">uint32_t hashing::md5::toLittleEndian32 </td>
<td>(</td>
<td class="paramtype">uint32_t</td> <td class="paramname"><span class="paramname"><em>n</em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Sets 32-bit integer to little-endian if needed. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">n</td><td>Number to set to little-endian (uint32_t) </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>uint32_t param n with binary representation as little-endian </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../d5/d96/md5_8cpp_source.html#l00090">90</a> of file <a class="el" href="../../d5/d96/md5_8cpp_source.html">md5.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 90</span> {</div>
<div class="line"><span class="lineno"> 91</span> <span class="keywordflow">if</span> (!<a class="code hl_function" href="#af8e96bde0183c4b0a7ff04668f11e446">isBigEndian</a>()) {</div>
<div class="line"><span class="lineno"> 92</span> <span class="keywordflow">return</span> ((n &lt;&lt; 24) &amp; 0xFF000000) | ((n &lt;&lt; 8) &amp; 0x00FF0000) |</div>
<div class="line"><span class="lineno"> 93</span> ((n &gt;&gt; 8) &amp; 0x0000FF00) | ((n &gt;&gt; 24) &amp; 0x000000FF);</div>
<div class="line"><span class="lineno"> 94</span> }</div>
<div class="line"><span class="lineno"> 95</span> <span class="comment">// Machine works on little endian, no need to change anything</span></div>
<div class="line"><span class="lineno"> 96</span> <span class="keywordflow">return</span> n;</div>
<div class="line"><span class="lineno"> 97</span>}</div>
<div class="ttc" id="amd5_8cpp_html_af8e96bde0183c4b0a7ff04668f11e446"><div class="ttname"><a href="#af8e96bde0183c4b0a7ff04668f11e446">hashing::md5::isBigEndian</a></div><div class="ttdeci">bool isBigEndian()</div><div class="ttdoc">Checks whether integers are stored as big endian or not.</div><div class="ttdef"><b>Definition</b> <a href="../../d5/d96/md5_8cpp_source.html#l00077">md5.cpp:77</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a6be48c1e6e742f9bd329f501d61dcaef" name="a6be48c1e6e742f9bd329f501d61dcaef"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a6be48c1e6e742f9bd329f501d61dcaef">&#9670;&#160;</a></span>toLittleEndian64()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">uint64_t hashing::md5::toLittleEndian64 </td>
<td>(</td>
<td class="paramtype">uint64_t</td> <td class="paramname"><span class="paramname"><em>n</em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Sets 64-bit integer to little-endian if needed. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">n</td><td>Number to set to little-endian (uint64_t) </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>uint64_t param n with binary representation as little-endian </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../d5/d96/md5_8cpp_source.html#l00103">103</a> of file <a class="el" href="../../d5/d96/md5_8cpp_source.html">md5.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 103</span> {</div>
<div class="line"><span class="lineno"> 104</span> <span class="keywordflow">if</span> (!<a class="code hl_function" href="#af8e96bde0183c4b0a7ff04668f11e446">isBigEndian</a>()) {</div>
<div class="line"><span class="lineno"> 105</span> <span class="keywordflow">return</span> ((n &lt;&lt; 56) &amp; 0xFF00000000000000) |</div>
<div class="line"><span class="lineno"> 106</span> ((n &lt;&lt; 40) &amp; 0x00FF000000000000) |</div>
<div class="line"><span class="lineno"> 107</span> ((n &lt;&lt; 24) &amp; 0x0000FF0000000000) |</div>
<div class="line"><span class="lineno"> 108</span> ((n &lt;&lt; 8) &amp; 0x000000FF00000000) |</div>
<div class="line"><span class="lineno"> 109</span> ((n &gt;&gt; 8) &amp; 0x00000000FF000000) |</div>
<div class="line"><span class="lineno"> 110</span> ((n &gt;&gt; 24) &amp; 0x0000000000FF0000) |</div>
<div class="line"><span class="lineno"> 111</span> ((n &gt;&gt; 40) &amp; 0x000000000000FF00) |</div>
<div class="line"><span class="lineno"> 112</span> ((n &gt;&gt; 56) &amp; 0x00000000000000FF);</div>
<div class="line"><span class="lineno"> 113</span> ;</div>
<div class="line"><span class="lineno"> 114</span> }</div>
<div class="line"><span class="lineno"> 115</span> <span class="comment">// Machine works on little endian, no need to change anything</span></div>
<div class="line"><span class="lineno"> 116</span> <span class="keywordflow">return</span> n;</div>
<div class="line"><span class="lineno"> 117</span>}</div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<div id="page-nav" class="page-nav-panel">
<div id="page-nav-resize-handle"></div>
<div id="page-nav-tree">
<div id="page-nav-contents">
</div><!-- page-nav-contents -->
</div><!-- page-nav-tree -->
</div><!-- page-nav -->
</div><!-- container -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a href="../../dir_ece9b94c107bbaa1dd68197a8c9983b9.html">hashing</a></li><li class="navelem"><a href="../../d5/d96/md5_8cpp.html">md5.cpp</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.14.0 </li>
</ul>
</div>
</body>
</html>