📄 randpool_8cpp-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Crypto++: randpool.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.2 --><div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Compound List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Compound Members</a> | <a class="qindex" href="globals.html">File Members</a></div><h1>randpool.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// randpool.cpp - written and placed in the public domain by Wei Dai</span>00002 <span class="comment">// The algorithm in this module comes from PGP's randpool.c</span>00003 00004 <span class="preprocessor">#include "pch.h"</span>00005 00006 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span>00007 <span class="preprocessor"></span>00008 <span class="preprocessor">#include "randpool.h"</span>00009 <span class="preprocessor">#include "<a class="code" href="mdc_8h.html">mdc.h</a>"</span>00010 <span class="preprocessor">#include "sha.h"</span>00011 <span class="preprocessor">#include "<a class="code" href="modes_8h.html">modes.h</a>"</span>00012 00013 NAMESPACE_BEGIN(CryptoPP)00014 00015 <span class="keyword">typedef</span> <a class="code" href="class_m_d_c.html">MDC<SHA></a> <a class="code" href="class_m_d_c.html">RandomPoolCipher</a>;00016 <a name="l00017"></a><a class="code" href="class_random_pool.html#_random_poola0">00017</a> <a class="code" href="class_random_pool.html#_random_poola0">RandomPool::RandomPool</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> poolSize)00018 : pool(poolSize), key(RandomPoolCipher::DEFAULT_KEYLENGTH)00019 {00020 assert(poolSize > key.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>());00021 00022 addPos=0;00023 getPos=poolSize;00024 memset(pool, 0, poolSize);00025 memset(key, 0, key.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>());00026 }00027 00028 <span class="keywordtype">void</span> RandomPool::Stir()00029 {00030 <a class="code" href="struct_c_f_b___mode.html">CFB_Mode<RandomPoolCipher></a>::Encryption cipher;00031 00032 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i<2; i++)00033 {00034 cipher.SetKeyWithIV(key, key.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>(), pool.<a class="code" href="class_sec_block.html#_sec_block_with_hinta9">end</a>()-cipher.IVSize());00035 cipher.ProcessString(pool, pool.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>());00036 memcpy(key, pool, key.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>());00037 }00038 00039 addPos = 0;00040 getPos = key.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>();00041 }00042 <a name="l00043"></a><a class="code" href="class_random_pool.html#_random_poola1">00043</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_random_pool.html#_random_poola1">RandomPool::Put2</a>(<span class="keyword">const</span> byte *inString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keywordtype">int</span> messageEnd, <span class="keywordtype">bool</span> blocking)00044 {00045 <span class="keywordtype">unsigned</span> t;00046 00047 <span class="keywordflow">while</span> (length > (t = pool.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>() - addPos))00048 {00049 xorbuf(pool+addPos, inString, t);00050 inString += t;00051 length -= t;00052 Stir();00053 }00054 00055 <span class="keywordflow">if</span> (length)00056 {00057 xorbuf(pool+addPos, inString, length);00058 addPos += length;00059 getPos = pool.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>(); <span class="comment">// Force stir on get</span>00060 }00061 00062 <span class="keywordflow">return</span> 0;00063 }00064 <a name="l00065"></a><a class="code" href="class_random_pool.html#_random_poola4">00065</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_random_pool.html#_random_poola4">RandomPool::TransferTo2</a>(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &target, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> &transferBytes, <span class="keyword">const</span> std::string &channel, <span class="keywordtype">bool</span> blocking)00066 {00067 <span class="keywordflow">if</span> (!blocking)00068 <span class="keywordflow">throw</span> <a class="code" href="class_not_implemented.html">NotImplemented</a>(<span class="stringliteral">"RandomPool: nonblocking transfer is not implemented by this object"</span>);00069 00070 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> t;00071 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> size = transferBytes;00072 00073 <span class="keywordflow">while</span> (size > (t = pool.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>() - getPos))00074 {00075 target.<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz13_0">ChannelPut</a>(channel, pool+getPos, t);00076 size -= t;00077 Stir();00078 }00079 00080 <span class="keywordflow">if</span> (size)00081 {00082 target.<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz13_0">ChannelPut</a>(channel, pool+getPos, size);00083 getPos += size;00084 }00085 00086 <span class="keywordflow">return</span> 0;00087 }00088 <a name="l00089"></a><a class="code" href="class_random_pool.html#_random_poola6">00089</a> byte <a class="code" href="class_random_pool.html#_random_poola6">RandomPool::GenerateByte</a>()00090 {00091 <span class="keywordflow">if</span> (getPos == pool.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>())00092 Stir();00093 00094 <span class="keywordflow">return</span> pool[getPos++];00095 }00096 <a name="l00097"></a><a class="code" href="class_random_pool.html#_random_poola7">00097</a> <span class="keywordtype">void</span> <a class="code" href="class_random_pool.html#_random_poola7">RandomPool::GenerateBlock</a>(byte *outString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size)00098 {00099 <a class="code" href="class_array_sink.html">ArraySink</a> sink(outString, size);00100 <a class="code" href="class_buffered_transformation.html#_zlib_decompressorz7_10">TransferTo</a>(sink, size);00101 }00102 00103 NAMESPACE_END00104 00105 <span class="preprocessor">#endif</span></pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:23 2003 for Crypto++ by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.2 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -