📄 randpool_8cpp-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>Crypto++: randpool.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.2 --><div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="classes.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul></div><div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul></div><h1>randpool.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// randpool.cpp - written and placed in the public domain by Wei Dai</span><a name="l00002"></a>00002 <span class="comment">// RandomPool used to follow the design of randpool in PGP 2.6.x,</span><a name="l00003"></a>00003 <span class="comment">// but as of version 5.5 it has been redesigned to reduce the risk</span><a name="l00004"></a>00004 <span class="comment">// of reusing random numbers after state rollback (which may occur</span><a name="l00005"></a>00005 <span class="comment">// when running in a virtual machine like VMware).</span><a name="l00006"></a>00006 <a name="l00007"></a>00007 <span class="preprocessor">#include "pch.h"</span><a name="l00008"></a>00008 <a name="l00009"></a>00009 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span><a name="l00010"></a>00010 <span class="preprocessor"></span><a name="l00011"></a>00011 <span class="preprocessor">#include "randpool.h"</span><a name="l00012"></a>00012 <span class="preprocessor">#include "aes.h"</span><a name="l00013"></a>00013 <span class="preprocessor">#include "sha.h"</span><a name="l00014"></a>00014 <span class="preprocessor">#include "hrtimer.h"</span><a name="l00015"></a>00015 <span class="preprocessor">#include <time.h></span><a name="l00016"></a>00016 <a name="l00017"></a>00017 NAMESPACE_BEGIN(CryptoPP)<a name="l00018"></a>00018 <a name="l00019"></a><a class="code" href="class_random_pool.html#1197329c8cf4ffd9097a2675431c0da1">00019</a> <a class="code" href="class_random_pool.html" title="Randomness Pool.">RandomPool</a>::<a class="code" href="class_random_pool.html" title="Randomness Pool.">RandomPool</a>()<a name="l00020"></a>00020 : m_pCipher(new <a class="code" href="class_a_e_s.html" title="AES winner, announced on 10/2/2000.">AES</a>::Encryption), m_keySet(false)<a name="l00021"></a>00021 {<a name="l00022"></a>00022 }<a name="l00023"></a>00023 <a name="l00024"></a><a class="code" href="class_random_pool.html#4339d77cb309a5ee47d7a5f1751481f3">00024</a> <span class="keywordtype">void</span> <a class="code" href="class_random_pool.html#4339d77cb309a5ee47d7a5f1751481f3" title="update RNG state with additional unpredictable values">RandomPool::IncorporateEntropy</a>(<span class="keyword">const</span> byte *input, <span class="keywordtype">size_t</span> length)<a name="l00025"></a>00025 {<a name="l00026"></a>00026 <a class="code" href="class_s_h_a256.html" title="implements the SHA-256 standard">SHA256</a> hash;<a name="l00027"></a>00027 hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(m_key, 32);<a name="l00028"></a>00028 hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(input, length);<a name="l00029"></a>00029 hash.<a class="code" href="class_hash_transformation.html#a0b8c7a110d8968268fd02ec32b9a8e8" title="compute hash for current message, then restart for a new message">Final</a>(m_key);<a name="l00030"></a>00030 m_keySet = <span class="keyword">false</span>;<a name="l00031"></a>00031 }<a name="l00032"></a>00032 <a name="l00033"></a><a class="code" href="class_random_pool.html#a8f69e71e3f43bc78b73084b81567a7e">00033</a> <span class="keywordtype">void</span> <a class="code" href="class_random_pool.html#a8f69e71e3f43bc78b73084b81567a7e" title="generate random bytes as input to a BufferedTransformation">RandomPool::GenerateIntoBufferedTransformation</a>(<a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> &target, <span class="keyword">const</span> std::string &channel, lword size)<a name="l00034"></a>00034 {<a name="l00035"></a>00035 <span class="keywordflow">if</span> (size > 0)<a name="l00036"></a>00036 {<a name="l00037"></a>00037 <span class="keywordflow">if</span> (!m_keySet)<a name="l00038"></a>00038 m_pCipher->SetKey(m_key, 32);<a name="l00039"></a>00039 <a name="l00040"></a>00040 <a class="code" href="class_timer.html" title="high resolution timer">Timer</a> timer;<a name="l00041"></a>00041 TimerWord tw = timer.<a class="code" href="class_timer.html#2614960e78eb57d97ad284dcb6fd8f02">GetCurrentTimerValue</a>();<a name="l00042"></a>00042 CRYPTOPP_COMPILE_ASSERT(<span class="keyword">sizeof</span>(tw) <= 16);<a name="l00043"></a>00043 *(TimerWord *)m_seed.<a class="code" href="class_sec_block.html#42ad7c28810297ca50b2411284876845">data</a>() += tw;<a name="l00044"></a>00044 <a name="l00045"></a>00045 time_t t = time(NULL);<a name="l00046"></a>00046 CRYPTOPP_COMPILE_ASSERT(<span class="keyword">sizeof</span>(t) <= 8);<a name="l00047"></a>00047 *(time_t *)(m_seed.<a class="code" href="class_sec_block.html#42ad7c28810297ca50b2411284876845">data</a>()+8) += t;<a name="l00048"></a>00048 <a name="l00049"></a>00049 <span class="keywordflow">do</span><a name="l00050"></a>00050 {<a name="l00051"></a>00051 m_pCipher->ProcessBlock(m_seed);<a name="l00052"></a>00052 <span class="keywordtype">size_t</span> len = UnsignedMin(16, size);<a name="l00053"></a>00053 target.<a class="code" href="class_buffered_transformation.html#97a60b54fafdb3df59e1457ef629fc5f">ChannelPut</a>(channel, m_seed, len);<a name="l00054"></a>00054 size -= len;<a name="l00055"></a>00055 } <span class="keywordflow">while</span> (size > 0);<a name="l00056"></a>00056 }<a name="l00057"></a>00057 }<a name="l00058"></a>00058 <a name="l00059"></a>00059 NAMESPACE_END<a name="l00060"></a>00060 <a name="l00061"></a>00061 <span class="preprocessor">#endif</span></pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Fri Jun 1 11:11:23 2007 for Crypto++ by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.2 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -