Files
C-Plus-Plus/d8/d7a/sha1_8cpp.html
2022-09-02 14:36:25 +00:00

646 lines
58 KiB
HTML

<!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.9.5"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C++: hashing/sha1.cpp File Reference</title>
<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>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.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/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.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="projectalign">
<div id="projectname">Algorithms_in_C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C++.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.5 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</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">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('d8/d7a/sha1_8cpp.html','../../'); initResizable(); });
/* @license-end */
</script>
<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="summary">
<a href="#namespaces">Namespaces</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">sha1.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>Simple C++ implementation of the <a href="https://en.wikipedia.org/wiki/SHA-1" target="_blank">SHA-1 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;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 sha1.cpp:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d4c/sha1_8cpp__incl.svg" width="595" height="110"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:d6/d0c/namespacehashing"><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">Hashing algorithms. <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:de/dd3/namespace_s_h_a"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../de/dd3/namespace_s_h_a.html">SHA</a></td></tr>
<tr class="memdesc:de/dd3/namespace_s_h_a"><td class="mdescLeft">&#160;</td><td class="mdescRight">Functions for the <a href="https://en.wikipedia.org/wiki/SHA-1" target="_blank">SHA-1</a> algorithm implementation. <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:acf6bd970f29a68702bdbdfe8338e45e0"><td class="memItemLeft" align="right" valign="top">uint32_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d7a/sha1_8cpp.html#acf6bd970f29a68702bdbdfe8338e45e0">hashing::sha1::leftRotate32bits</a> (uint32_t n, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/size_t.html">std::size_t</a> rotate)</td></tr>
<tr class="memdesc:acf6bd970f29a68702bdbdfe8338e45e0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Rotates the bits of a 32-bit unsigned integer. <a href="../../d8/d7a/sha1_8cpp.html#acf6bd970f29a68702bdbdfe8338e45e0">More...</a><br /></td></tr>
<tr class="separator:acf6bd970f29a68702bdbdfe8338e45e0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aada0803ef851d831b7a290a924e3c228"><td class="memItemLeft" align="right" valign="top"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d7a/sha1_8cpp.html#aada0803ef851d831b7a290a924e3c228">hashing::sha1::sig2hex</a> (void *sig)</td></tr>
<tr class="memdesc:aada0803ef851d831b7a290a924e3c228"><td class="mdescLeft">&#160;</td><td class="mdescRight">Transforms the 160-bit SHA-1 signature into a 40 char hex string. <a href="../../d8/d7a/sha1_8cpp.html#aada0803ef851d831b7a290a924e3c228">More...</a><br /></td></tr>
<tr class="separator:aada0803ef851d831b7a290a924e3c228"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a7be3471f7e489d7d0df42b97a48bf141"><td class="memItemLeft" align="right" valign="top">void *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d7a/sha1_8cpp.html#a7be3471f7e489d7d0df42b97a48bf141">hashing::sha1::hash_bs</a> (const void *input_bs, uint64_t input_size)</td></tr>
<tr class="memdesc:a7be3471f7e489d7d0df42b97a48bf141"><td class="mdescLeft">&#160;</td><td class="mdescRight">The SHA-1 algorithm itself, taking in a bytestring. <a href="../../d8/d7a/sha1_8cpp.html#a7be3471f7e489d7d0df42b97a48bf141">More...</a><br /></td></tr>
<tr class="separator:a7be3471f7e489d7d0df42b97a48bf141"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a2397f2444a05e4d1487c67e215410d3c"><td class="memItemLeft" align="right" valign="top">void *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d7a/sha1_8cpp.html#a2397f2444a05e4d1487c67e215410d3c">hashing::sha1::hash</a> (const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> &amp;message)</td></tr>
<tr class="memdesc:a2397f2444a05e4d1487c67e215410d3c"><td class="mdescLeft">&#160;</td><td class="mdescRight">Converts the string to bytestring and calls the main algorithm. <a href="../../d8/d7a/sha1_8cpp.html#a2397f2444a05e4d1487c67e215410d3c">More...</a><br /></td></tr>
<tr class="separator:a2397f2444a05e4d1487c67e215410d3c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d7a/sha1_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self-test implementations of well-known SHA-1 hashes. <a href="../../d8/d7a/sha1_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">More...</a><br /></td></tr>
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ac9e1a11f44135b890dd10a00e73b5661"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d7a/sha1_8cpp.html#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 SHA-1 hash will be computed and printed. <a href="../../d8/d7a/sha1_8cpp.html#ac9e1a11f44135b890dd10a00e73b5661">More...</a><br /></td></tr>
<tr class="separator:ac9e1a11f44135b890dd10a00e73b5661"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d7a/sha1_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <a href="../../d8/d7a/sha1_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">More...</a><br /></td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p >Simple C++ implementation of the <a href="https://en.wikipedia.org/wiki/SHA-1" target="_blank">SHA-1 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><a href="https://en.wikipedia.org/wiki/SHA-1" target="_blank">SHA-1</a> is a cryptographic hash function that was developped by the <a href="https://en.wikipedia.org/wiki/National_Security_Agency" target="_blank">NSA</a> 1995. SHA-1 is not considered secure since around 2010.</p>
<h3><a class="anchor" id="autotoc_md81"></a>
Algorithm</h3>
<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. These 16 blocks are then extended to 80 blocks using <a class="el" href="../../d7/d47/namespace_x_o_r.html" title="Functions for XOR cipher algorithm.">XOR</a> operations on existing blocks (see code for more details). The algorithm will then update its 160-bit state (here represented used 5 32-bits integer) using partial hashes computed using special functions on the blocks previously built. Please take a look at the <a href="https://en.wikipedia.org/wiki/SHA-1#SHA-1_pseudocode" target="_blank">wikipedia article</a> for more precision on these operations </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>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a2397f2444a05e4d1487c67e215410d3c" name="a2397f2444a05e4d1487c67e215410d3c"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a2397f2444a05e4d1487c67e215410d3c">&#9670;&#160;</a></span>hash()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void * hashing::sha1::hash </td>
<td>(</td>
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> &amp;&#160;</td>
<td class="paramname"><em>message</em></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 SHA-1 signature </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 210</span> {</div>
<div class="line"><span class="lineno"> 211</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../d5/d96/md5_8cpp.html#a5341431cef7fcfc33794326e1deb2425">hash_bs</a>(&amp;message[0], message.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">size</a>());</div>
<div class="line"><span class="lineno"> 212</span>}</div>
<div class="ttc" id="amd5_8cpp_html_a5341431cef7fcfc33794326e1deb2425"><div class="ttname"><a href="../../d5/d96/md5_8cpp.html#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> md5.cpp:138</div></div>
<div class="ttc" id="asize_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">std::string::size</a></div><div class="ttdeci">T size(T... args)</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d7a/sha1_8cpp_a2397f2444a05e4d1487c67e215410d3c_cgraph.svg" width="310" height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a7be3471f7e489d7d0df42b97a48bf141" name="a7be3471f7e489d7d0df42b97a48bf141"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a7be3471f7e489d7d0df42b97a48bf141">&#9670;&#160;</a></span>hash_bs()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void * hashing::sha1::hash_bs </td>
<td>(</td>
<td class="paramtype">const void *&#160;</td>
<td class="paramname"><em>input_bs</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">uint64_t&#160;</td>
<td class="paramname"><em>input_size</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>The SHA-1 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 160-bit signature </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 83</span> {</div>
<div class="line"><span class="lineno"> 84</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"> 85</span> </div>
<div class="line"><span class="lineno"> 86</span> <span class="comment">// Step 0: The initial 160-bit state</span></div>
<div class="line"><span class="lineno"> 87</span> uint32_t h0 = 0x67452301, a = 0;</div>
<div class="line"><span class="lineno"> 88</span> uint32_t h1 = 0xEFCDAB89, b = 0;</div>
<div class="line"><span class="lineno"> 89</span> uint32_t h2 = 0x98BADCFE, c = 0;</div>
<div class="line"><span class="lineno"> 90</span> uint32_t h3 = 0x10325476, d = 0;</div>
<div class="line"><span class="lineno"> 91</span> uint32_t h4 = 0xC3D2E1F0, e = 0;</div>
<div class="line"><span class="lineno"> 92</span> </div>
<div class="line"><span class="lineno"> 93</span> <span class="comment">// Step 1: Processing the bytestring</span></div>
<div class="line"><span class="lineno"> 94</span> <span class="comment">// First compute the size the padded message will have</span></div>
<div class="line"><span class="lineno"> 95</span> <span class="comment">// so it is possible to allocate the right amount of memory</span></div>
<div class="line"><span class="lineno"> 96</span> uint64_t padded_message_size = 0;</div>
<div class="line"><span class="lineno"> 97</span> <span class="keywordflow">if</span> (input_size % 64 &lt; 56) {</div>
<div class="line"><span class="lineno"> 98</span> padded_message_size = input_size + 64 - (input_size % 64);</div>
<div class="line"><span class="lineno"> 99</span> } <span class="keywordflow">else</span> {</div>
<div class="line"><span class="lineno"> 100</span> padded_message_size = input_size + 128 - (input_size % 64);</div>
<div class="line"><span class="lineno"> 101</span> }</div>
<div class="line"><span class="lineno"> 102</span> </div>
<div class="line"><span class="lineno"> 103</span> <span class="comment">// Allocate the memory for the padded message</span></div>
<div class="line"><span class="lineno"> 104</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;uint8_t&gt;</a> padded_message(padded_message_size);</div>
<div class="line"><span class="lineno"> 105</span> </div>
<div class="line"><span class="lineno"> 106</span> <span class="comment">// Beginning of the padded message is the original message</span></div>
<div class="line"><span class="lineno"> 107</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/copy.html">std::copy</a>(input, input + input_size, padded_message.begin());</div>
<div class="line"><span class="lineno"> 108</span> </div>
<div class="line"><span class="lineno"> 109</span> <span class="comment">// Afterwards comes a single 1 bit and then only zeroes</span></div>
<div class="line"><span class="lineno"> 110</span> padded_message[input_size] = 1 &lt;&lt; 7; <span class="comment">// 10000000</span></div>
<div class="line"><span class="lineno"> 111</span> <span class="keywordflow">for</span> (uint64_t i = input_size; i % 64 != 56; i++) {</div>
<div class="line"><span class="lineno"> 112</span> <span class="keywordflow">if</span> (i == input_size) {</div>
<div class="line"><span class="lineno"> 113</span> <span class="keywordflow">continue</span>; <span class="comment">// pass first iteration</span></div>
<div class="line"><span class="lineno"> 114</span> }</div>
<div class="line"><span class="lineno"> 115</span> padded_message[i] = 0;</div>
<div class="line"><span class="lineno"> 116</span> }</div>
<div class="line"><span class="lineno"> 117</span> </div>
<div class="line"><span class="lineno"> 118</span> <span class="comment">// We then have to add the 64-bit size of the message in bits (hence the</span></div>
<div class="line"><span class="lineno"> 119</span> <span class="comment">// times 8) in the last 8 bytes</span></div>
<div class="line"><span class="lineno"> 120</span> uint64_t input_bitsize = input_size * 8;</div>
<div class="line"><span class="lineno"> 121</span> <span class="keywordflow">for</span> (uint8_t i = 0; i &lt; 8; i++) {</div>
<div class="line"><span class="lineno"> 122</span> padded_message[padded_message_size - 8 + i] =</div>
<div class="line"><span class="lineno"> 123</span> (input_bitsize &gt;&gt; (56 - 8 * i)) &amp; 0xFF;</div>
<div class="line"><span class="lineno"> 124</span> }</div>
<div class="line"><span class="lineno"> 125</span> </div>
<div class="line"><span class="lineno"> 126</span> <span class="comment">// Already allocate memory for blocks</span></div>
<div class="line"><span class="lineno"> 127</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;uint32_t, 80&gt;</a> blocks{};</div>
<div class="line"><span class="lineno"> 128</span> </div>
<div class="line"><span class="lineno"> 129</span> <span class="comment">// Rounds</span></div>
<div class="line"><span class="lineno"> 130</span> <span class="keywordflow">for</span> (uint64_t chunk = 0; chunk * 64 &lt; padded_message_size; chunk++) {</div>
<div class="line"><span class="lineno"> 131</span> <span class="comment">// First, build 16 32-bits blocks from the chunk</span></div>
<div class="line"><span class="lineno"> 132</span> <span class="keywordflow">for</span> (uint8_t bid = 0; bid &lt; 16; bid++) {</div>
<div class="line"><span class="lineno"> 133</span> blocks[bid] = 0;</div>
<div class="line"><span class="lineno"> 134</span> </div>
<div class="line"><span class="lineno"> 135</span> <span class="comment">// Having to build a 32-bit word from 4-bit words</span></div>
<div class="line"><span class="lineno"> 136</span> <span class="comment">// Add each and shift them to the left</span></div>
<div class="line"><span class="lineno"> 137</span> <span class="keywordflow">for</span> (uint8_t cid = 0; cid &lt; 4; cid++) {</div>
<div class="line"><span class="lineno"> 138</span> blocks[bid] = (blocks[bid] &lt;&lt; 8) +</div>
<div class="line"><span class="lineno"> 139</span> padded_message[chunk * 64 + bid * 4 + cid];</div>
<div class="line"><span class="lineno"> 140</span> }</div>
<div class="line"><span class="lineno"> 141</span> </div>
<div class="line"><span class="lineno"> 142</span> <span class="comment">// Extend the 16 32-bit words into 80 32-bit words</span></div>
<div class="line"><span class="lineno"> 143</span> <span class="keywordflow">for</span> (uint8_t i = 16; i &lt; 80; i++) {</div>
<div class="line"><span class="lineno"> 144</span> blocks[i] =</div>
<div class="line"><span class="lineno"> 145</span> <a class="code hl_function" href="../../d5/d96/md5_8cpp.html#add2bf982d5dd64a45d13cba52c2b371d">leftRotate32bits</a>(blocks[i - 3] ^ blocks[i - 8] ^</div>
<div class="line"><span class="lineno"> 146</span> blocks[i - 14] ^ blocks[i - 16],</div>
<div class="line"><span class="lineno"> 147</span> 1);</div>
<div class="line"><span class="lineno"> 148</span> }</div>
<div class="line"><span class="lineno"> 149</span> }</div>
<div class="line"><span class="lineno"> 150</span> </div>
<div class="line"><span class="lineno"> 151</span> a = h0;</div>
<div class="line"><span class="lineno"> 152</span> b = h1;</div>
<div class="line"><span class="lineno"> 153</span> c = h2;</div>
<div class="line"><span class="lineno"> 154</span> d = h3;</div>
<div class="line"><span class="lineno"> 155</span> e = h4;</div>
<div class="line"><span class="lineno"> 156</span> </div>
<div class="line"><span class="lineno"> 157</span> <span class="comment">// Main &quot;hashing&quot; loop</span></div>
<div class="line"><span class="lineno"> 158</span> <span class="keywordflow">for</span> (uint8_t i = 0; i &lt; 80; i++) {</div>
<div class="line"><span class="lineno"> 159</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"> 160</span> <span class="keywordflow">if</span> (i &lt; 20) {</div>
<div class="line"><span class="lineno"> 161</span> F = (b &amp; c) | ((~b) &amp; d);</div>
<div class="line"><span class="lineno"> 162</span> <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a2f67508d5f392b0321772169342c98ad">g</a> = 0x5A827999;</div>
<div class="line"><span class="lineno"> 163</span> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (i &lt; 40) {</div>
<div class="line"><span class="lineno"> 164</span> F = b ^ c ^ d;</div>
<div class="line"><span class="lineno"> 165</span> <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a2f67508d5f392b0321772169342c98ad">g</a> = 0x6ED9EBA1;</div>
<div class="line"><span class="lineno"> 166</span> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (i &lt; 60) {</div>
<div class="line"><span class="lineno"> 167</span> F = (b &amp; c) | (b &amp; d) | (c &amp; d);</div>
<div class="line"><span class="lineno"> 168</span> <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a2f67508d5f392b0321772169342c98ad">g</a> = 0x8F1BBCDC;</div>
<div class="line"><span class="lineno"> 169</span> } <span class="keywordflow">else</span> {</div>
<div class="line"><span class="lineno"> 170</span> F = b ^ c ^ d;</div>
<div class="line"><span class="lineno"> 171</span> <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a2f67508d5f392b0321772169342c98ad">g</a> = 0xCA62C1D6;</div>
<div class="line"><span class="lineno"> 172</span> }</div>
<div class="line"><span class="lineno"> 173</span> </div>
<div class="line"><span class="lineno"> 174</span> <span class="comment">// Update the accumulators</span></div>
<div class="line"><span class="lineno"> 175</span> uint32_t temp = <a class="code hl_function" href="../../d5/d96/md5_8cpp.html#add2bf982d5dd64a45d13cba52c2b371d">leftRotate32bits</a>(a, 5) + F + e + <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a2f67508d5f392b0321772169342c98ad">g</a> + blocks[i];</div>
<div class="line"><span class="lineno"> 176</span> e = d;</div>
<div class="line"><span class="lineno"> 177</span> d = c;</div>
<div class="line"><span class="lineno"> 178</span> c = <a class="code hl_function" href="../../d5/d96/md5_8cpp.html#add2bf982d5dd64a45d13cba52c2b371d">leftRotate32bits</a>(b, 30);</div>
<div class="line"><span class="lineno"> 179</span> b = a;</div>
<div class="line"><span class="lineno"> 180</span> a = temp;</div>
<div class="line"><span class="lineno"> 181</span> }</div>
<div class="line"><span class="lineno"> 182</span> <span class="comment">// Update the state with this chunk&#39;s hash</span></div>
<div class="line"><span class="lineno"> 183</span> h0 += a;</div>
<div class="line"><span class="lineno"> 184</span> h1 += b;</div>
<div class="line"><span class="lineno"> 185</span> h2 += c;</div>
<div class="line"><span class="lineno"> 186</span> h3 += d;</div>
<div class="line"><span class="lineno"> 187</span> h4 += e;</div>
<div class="line"><span class="lineno"> 188</span> }</div>
<div class="line"><span class="lineno"> 189</span> </div>
<div class="line"><span class="lineno"> 190</span> <span class="comment">// Build signature from state</span></div>
<div class="line"><span class="lineno"> 191</span> <span class="comment">// Note, any type could be used for the signature</span></div>
<div class="line"><span class="lineno"> 192</span> <span class="comment">// uint8_t was used to make the 20 bytes obvious</span></div>
<div class="line"><span class="lineno"> 193</span> <span class="keyword">auto</span>* sig = <span class="keyword">new</span> uint8_t[20];</div>
<div class="line"><span class="lineno"> 194</span> <span class="keywordflow">for</span> (uint8_t i = 0; i &lt; 4; i++) {</div>
<div class="line"><span class="lineno"> 195</span> sig[i] = (h0 &gt;&gt; (24 - 8 * i)) &amp; 0xFF;</div>
<div class="line"><span class="lineno"> 196</span> sig[i + 4] = (h1 &gt;&gt; (24 - 8 * i)) &amp; 0xFF;</div>
<div class="line"><span class="lineno"> 197</span> sig[i + 8] = (h2 &gt;&gt; (24 - 8 * i)) &amp; 0xFF;</div>
<div class="line"><span class="lineno"> 198</span> sig[i + 12] = (h3 &gt;&gt; (24 - 8 * i)) &amp; 0xFF;</div>
<div class="line"><span class="lineno"> 199</span> sig[i + 16] = (h4 &gt;&gt; (24 - 8 * i)) &amp; 0xFF;</div>
<div class="line"><span class="lineno"> 200</span> }</div>
<div class="line"><span class="lineno"> 201</span> </div>
<div class="line"><span class="lineno"> 202</span> <span class="keywordflow">return</span> sig;</div>
<div class="line"><span class="lineno"> 203</span>}</div>
<div class="ttc" id="aarray_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a></div></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> composite_simpson_rule.cpp:115</div></div>
<div class="ttc" id="acopy_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/algorithm/copy.html">std::copy</a></div><div class="ttdeci">T copy(T... args)</div></div>
<div class="ttc" id="amd5_8cpp_html_add2bf982d5dd64a45d13cba52c2b371d"><div class="ttname"><a href="../../d5/d96/md5_8cpp.html#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> md5.cpp:66</div></div>
<div class="ttc" id="avector_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a></div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d7a/sha1_8cpp_a7be3471f7e489d7d0df42b97a48bf141_cgraph.svg" width="342" height="86"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</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">static void interactive </td>
<td>(</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Puts user in a loop where inputs can be given and SHA-1 hash will be computed and printed. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 274</span> {</div>
<div class="line"><span class="lineno"> 275</span> <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div>
<div class="line"><span class="lineno"> 276</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> input;</div>
<div class="line"><span class="lineno"> 277</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &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"> 278</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 279</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/getline.html">std::getline</a>(<a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_istream.html">std::cin</a>, input);</div>
<div class="line"><span class="lineno"> 280</span> <span class="keywordtype">void</span>* sig = <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#a2397f2444a05e4d1487c67e215410d3c">hashing::sha1::hash</a>(input);</div>
<div class="line"><span class="lineno"> 281</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Hash is: &quot;</span> &lt;&lt; <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#aada0803ef851d831b7a290a924e3c228">hashing::sha1::sig2hex</a>(sig) &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 282</span> </div>
<div class="line"><span class="lineno"> 283</span> <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div>
<div class="line"><span class="lineno"> 284</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Want to enter another message? (y/n) &quot;</span>;</div>
<div class="line"><span class="lineno"> 285</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/getline.html">std::getline</a>(<a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_istream.html">std::cin</a>, input);</div>
<div class="line"><span class="lineno"> 286</span> <span class="keywordflow">if</span> (input.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/compare.html">compare</a>(<span class="stringliteral">&quot;y&quot;</span>) == 0) {</div>
<div class="line"><span class="lineno"> 287</span> <span class="keywordflow">break</span>;</div>
<div class="line"><span class="lineno"> 288</span> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (input.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/compare.html">compare</a>(<span class="stringliteral">&quot;n&quot;</span>) == 0) {</div>
<div class="line"><span class="lineno"> 289</span> <span class="keywordflow">return</span>;</div>
<div class="line"><span class="lineno"> 290</span> }</div>
<div class="line"><span class="lineno"> 291</span> }</div>
<div class="line"><span class="lineno"> 292</span> }</div>
<div class="line"><span class="lineno"> 293</span>}</div>
<div class="ttc" id="abasic_istream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_istream.html">std::cin</a></div></div>
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
<div class="ttc" id="abasic_string_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a></div></div>
<div class="ttc" id="acompare_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string/compare.html">std::string::compare</a></div><div class="ttdeci">T compare(T... args)</div></div>
<div class="ttc" id="aendl_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a></div><div class="ttdeci">T endl(T... args)</div></div>
<div class="ttc" id="agetline_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string/getline.html">std::getline</a></div><div class="ttdeci">T getline(T... args)</div></div>
<div class="ttc" id="asha1_8cpp_html_a2397f2444a05e4d1487c67e215410d3c"><div class="ttname"><a href="../../d8/d7a/sha1_8cpp.html#a2397f2444a05e4d1487c67e215410d3c">hashing::sha1::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> sha1.cpp:210</div></div>
<div class="ttc" id="asha1_8cpp_html_aada0803ef851d831b7a290a924e3c228"><div class="ttname"><a href="../../d8/d7a/sha1_8cpp.html#aada0803ef851d831b7a290a924e3c228">hashing::sha1::sig2hex</a></div><div class="ttdeci">std::string sig2hex(void *sig)</div><div class="ttdoc">Transforms the 160-bit SHA-1 signature into a 40 char hex string.</div><div class="ttdef"><b>Definition:</b> sha1.cpp:66</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d7a/sha1_8cpp_ac9e1a11f44135b890dd10a00e73b5661_cgraph.svg" width="280" height="135"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="acf6bd970f29a68702bdbdfe8338e45e0" name="acf6bd970f29a68702bdbdfe8338e45e0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#acf6bd970f29a68702bdbdfe8338e45e0">&#9670;&#160;</a></span>leftRotate32bits()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">uint32_t hashing::sha1::leftRotate32bits </td>
<td>(</td>
<td class="paramtype">uint32_t&#160;</td>
<td class="paramname"><em>n</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/size_t.html">std::size_t</a>&#160;</td>
<td class="paramname"><em>rotate</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></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>
<div class="fragment"><div class="line"><span class="lineno"> 57</span> {</div>
<div class="line"><span class="lineno"> 58</span> <span class="keywordflow">return</span> (n &lt;&lt; rotate) | (n &gt;&gt; (32 - <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/rotate.html">rotate</a>));</div>
<div class="line"><span class="lineno"> 59</span>}</div>
<div class="ttc" id="arotate_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/algorithm/rotate.html">std::rotate</a></div><div class="ttdeci">T rotate(T... args)</div></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&#160;</td>
<td class="paramname"></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>
<div class="fragment"><div class="line"><span class="lineno"> 299</span> {</div>
<div class="line"><span class="lineno"> 300</span> <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><span class="lineno"> 301</span> </div>
<div class="line"><span class="lineno"> 302</span> <span class="comment">// Launch interactive mode where user can input messages and see</span></div>
<div class="line"><span class="lineno"> 303</span> <span class="comment">// their hash</span></div>
<div class="line"><span class="lineno"> 304</span> <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#ac9e1a11f44135b890dd10a00e73b5661">interactive</a>();</div>
<div class="line"><span class="lineno"> 305</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 306</span>}</div>
<div class="ttc" id="asha1_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../d8/d7a/sha1_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Self-test implementations of well-known SHA-1 hashes.</div><div class="ttdef"><b>Definition:</b> sha1.cpp:220</div></div>
<div class="ttc" id="asha1_8cpp_html_ac9e1a11f44135b890dd10a00e73b5661"><div class="ttname"><a href="../../d8/d7a/sha1_8cpp.html#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 SHA-1 hash will be computed and printed.</div><div class="ttdef"><b>Definition:</b> sha1.cpp:274</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d7a/sha1_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="379" height="135"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="aada0803ef851d831b7a290a924e3c228" name="aada0803ef851d831b7a290a924e3c228"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aada0803ef851d831b7a290a924e3c228">&#9670;&#160;</a></span>sig2hex()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> hashing::sha1::sig2hex </td>
<td>(</td>
<td class="paramtype">void *&#160;</td>
<td class="paramname"><em>sig</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Transforms the 160-bit SHA-1 signature into a 40 char hex string. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">sig</td><td>The SHA-1 signature (Expected 20 bytes) </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> The hex signature </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 66</span> {</div>
<div class="line"><span class="lineno"> 67</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"> 68</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"> 69</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/hex.html">hex</a> = <span class="stringliteral">&quot;&quot;</span>;</div>
<div class="line"><span class="lineno"> 70</span> <span class="keywordflow">for</span> (uint8_t i = 0; i &lt; 20; i++) {</div>
<div class="line"><span class="lineno"> 71</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/hex.html">hex</a>.push_back(hexChars[(intsig[i] &gt;&gt; 4) &amp; 0xF]);</div>
<div class="line"><span class="lineno"> 72</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/hex.html">hex</a>.push_back(hexChars[(intsig[i]) &amp; 0xF]);</div>
<div class="line"><span class="lineno"> 73</span> }</div>
<div class="line"><span class="lineno"> 74</span> <span class="keywordflow">return</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/hex.html">hex</a>;</div>
<div class="line"><span class="lineno"> 75</span>}</div>
<div class="ttc" id="ahex_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/hex.html">std::hex</a></div><div class="ttdeci">T hex(T... args)</div></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">static void test </td>
<td>(</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Self-test implementations of well-known SHA-1 hashes. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 220</span> {</div>
<div class="line"><span class="lineno"> 221</span> <span class="comment">// Hashes empty string and stores signature</span></div>
<div class="line"><span class="lineno"> 222</span> <span class="keywordtype">void</span>* sig = <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#a2397f2444a05e4d1487c67e215410d3c">hashing::sha1::hash</a>(<span class="stringliteral">&quot;&quot;</span>);</div>
<div class="line"><span class="lineno"> 223</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Hashing empty string&quot;</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 224</span> <span class="comment">// Prints signature hex representation</span></div>
<div class="line"><span class="lineno"> 225</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#aada0803ef851d831b7a290a924e3c228">hashing::sha1::sig2hex</a>(sig) &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 226</span> <span class="comment">// Test with cassert wether sig is correct from expected value</span></div>
<div class="line"><span class="lineno"> 227</span> assert(hashing::sha1::sig2hex(sig).<a class="code hl_struct" href="../../d1/db3/structcompare.html">compare</a>(</div>
<div class="line"><span class="lineno"> 228</span> <span class="stringliteral">&quot;da39a3ee5e6b4b0d3255bfef95601890afd80709&quot;</span>) == 0);</div>
<div class="line"><span class="lineno"> 229</span> </div>
<div class="line"><span class="lineno"> 230</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"> 231</span> <span class="keywordtype">void</span>* sig2 =</div>
<div class="line"><span class="lineno"> 232</span> <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#a2397f2444a05e4d1487c67e215410d3c">hashing::sha1::hash</a>(<span class="stringliteral">&quot;The quick brown fox jumps over the lazy dog&quot;</span>);</div>
<div class="line"><span class="lineno"> 233</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &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"> 234</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 235</span> <span class="comment">// Prints signature hex representation</span></div>
<div class="line"><span class="lineno"> 236</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#aada0803ef851d831b7a290a924e3c228">hashing::sha1::sig2hex</a>(sig2) &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 237</span> <span class="comment">// Test with cassert wether sig is correct from expected value</span></div>
<div class="line"><span class="lineno"> 238</span> assert(hashing::sha1::sig2hex(sig2).<a class="code hl_struct" href="../../d1/db3/structcompare.html">compare</a>(</div>
<div class="line"><span class="lineno"> 239</span> <span class="stringliteral">&quot;2fd4e1c67a2d28fced849ee1bb76e7391b93eb12&quot;</span>) == 0);</div>
<div class="line"><span class="lineno"> 240</span> </div>
<div class="line"><span class="lineno"> 241</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"> 242</span> <span class="comment">// additional period) and stores signature</span></div>
<div class="line"><span class="lineno"> 243</span> <span class="keywordtype">void</span>* sig3 =</div>
<div class="line"><span class="lineno"> 244</span> <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#a2397f2444a05e4d1487c67e215410d3c">hashing::sha1::hash</a>(<span class="stringliteral">&quot;The quick brown fox jumps over the lazy dog.&quot;</span>);</div>
<div class="line"><span class="lineno"> 245</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Hashing &quot;</span></div>
<div class="line"><span class="lineno"> 246</span> <span class="stringliteral">&quot;The quick brown fox jumps over the lazy dog.&quot;</span></div>
<div class="line"><span class="lineno"> 247</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 248</span> <span class="comment">// Prints signature hex representation</span></div>
<div class="line"><span class="lineno"> 249</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#aada0803ef851d831b7a290a924e3c228">hashing::sha1::sig2hex</a>(sig3) &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 250</span> <span class="comment">// Test with cassert wether sig is correct from expected value</span></div>
<div class="line"><span class="lineno"> 251</span> assert(hashing::sha1::sig2hex(sig3).<a class="code hl_struct" href="../../d1/db3/structcompare.html">compare</a>(</div>
<div class="line"><span class="lineno"> 252</span> <span class="stringliteral">&quot;408d94384216f890ff7a0c3528e8bed1e0b01621&quot;</span>) == 0);</div>
<div class="line"><span class="lineno"> 253</span> </div>
<div class="line"><span class="lineno"> 254</span> <span class="comment">// Hashes &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&quot;</span></div>
<div class="line"><span class="lineno"> 255</span> <span class="comment">// and stores signature</span></div>
<div class="line"><span class="lineno"> 256</span> <span class="keywordtype">void</span>* sig4 = <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#a2397f2444a05e4d1487c67e215410d3c">hashing::sha1::hash</a>(</div>
<div class="line"><span class="lineno"> 257</span> <span class="stringliteral">&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&quot;</span>);</div>
<div class="line"><span class="lineno"> 258</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div>
<div class="line"><span class="lineno"> 259</span> &lt;&lt; <span class="stringliteral">&quot;Hashing &quot;</span></div>
<div class="line"><span class="lineno"> 260</span> <span class="stringliteral">&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&quot;</span></div>
<div class="line"><span class="lineno"> 261</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 262</span> <span class="comment">// Prints signature hex representation</span></div>
<div class="line"><span class="lineno"> 263</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <a class="code hl_function" href="../../d8/d7a/sha1_8cpp.html#aada0803ef851d831b7a290a924e3c228">hashing::sha1::sig2hex</a>(sig4) &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 264</span> <span class="comment">// Test with cassert wether sig is correct from expected value</span></div>
<div class="line"><span class="lineno"> 265</span> assert(hashing::sha1::sig2hex(sig4).<a class="code hl_struct" href="../../d1/db3/structcompare.html">compare</a>(</div>
<div class="line"><span class="lineno"> 266</span> <span class="stringliteral">&quot;761c457bf73b14d27e9e9265c46f4b4dda11f940&quot;</span>) == 0);</div>
<div class="line"><span class="lineno"> 267</span>}</div>
<div class="ttc" id="astructcompare_html"><div class="ttname"><a href="../../d1/db3/structcompare.html">compare</a></div><div class="ttdef"><b>Definition:</b> huffman.cpp:28</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d7a/sha1_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg" width="175" height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_ece9b94c107bbaa1dd68197a8c9983b9.html">hashing</a></li><li class="navelem"><a class="el" href="../../d8/d7a/sha1_8cpp.html">sha1.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.9.5 </li>
</ul>
</div>
</body>
</html>