⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rng_8cpp-source.html

📁 Crypto++是一个非常强大的密码学库,主要是功能全
💻 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++: rng.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&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Compound&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Compound&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div><h1>rng.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// rng.cpp - written and placed in the public domain by Wei Dai</span>00002 00003 <span class="preprocessor">#include "pch.h"</span>00004 00005 <span class="preprocessor">#include "rng.h"</span>00006 00007 <span class="preprocessor">#include &lt;time.h&gt;</span>00008 <span class="preprocessor">#include &lt;math.h&gt;</span>00009 00010 NAMESPACE_BEGIN(CryptoPP)00011 00012 <span class="comment">// linear congruential generator</span>00013 <span class="comment">// originally by William S. England</span>00014 00015 <span class="comment">// do not use for cryptographic purposes</span>00016 00017 <span class="comment">/*</span>00018 <span class="comment">** Original_numbers are the original published m and q in the</span>00019 <span class="comment">** ACM article above.  John Burton has furnished numbers for</span>00020 <span class="comment">** a reportedly better generator.  The new numbers are now</span>00021 <span class="comment">** used in this program by default.</span>00022 <span class="comment">*/</span>00023 00024 #ifndef LCRNG_ORIGINAL_NUMBERS00025 <span class="keyword">const</span> word32 LC_RNG::m=2147483647L;00026 <span class="keyword">const</span> word32 LC_RNG::q=44488L;00027 00028 <span class="keyword">const</span> word16 LC_RNG::a=(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)48271L;00029 <span class="keyword">const</span> word16 LC_RNG::r=3399;00030 <span class="preprocessor">#else</span>00031 <span class="preprocessor"></span><span class="keyword">const</span> word32 LC_RNG::m=2147483647L;00032 <span class="keyword">const</span> word32 LC_RNG::q=127773L;00033 00034 <span class="keyword">const</span> word16 LC_RNG::a=16807;00035 <span class="keyword">const</span> word16 LC_RNG::r=2836;00036 <span class="preprocessor">#endif</span>00037 <span class="preprocessor"></span><a name="l00038"></a><a class="code" href="class_l_c___r_n_g.html#_l_c___r_n_ga1">00038</a> byte <a class="code" href="class_l_c___r_n_g.html#_l_c___r_n_ga1">LC_RNG::GenerateByte</a>()00039 {00040         word32 hi = seed/q;00041         word32 lo = seed%q;00042 00043         <span class="keywordtype">long</span> test = a*lo - r*hi;00044 00045         <span class="keywordflow">if</span> (test &gt; 0)00046                 seed = test;00047         <span class="keywordflow">else</span>00048                 seed = test+ m;00049 00050         <span class="keywordflow">return</span> (GETBYTE(seed, 0) ^ GETBYTE(seed, 1) ^ GETBYTE(seed, 2) ^ GETBYTE(seed, 3));00051 }00052 00053 <span class="comment">// ********************************************************</span>00054 00055 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span>00056 <span class="preprocessor"></span>00057 X917RNG::X917RNG(<a class="code" href="class_block_transformation.html">BlockTransformation</a> *c, <span class="keyword">const</span> byte *seed, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> deterministicTimeVector)00058         : cipher(c),00059           S(cipher-&gt;<a class="code" href="namespace_name.html#a9">BlockSize</a>()),00060           dtbuf(S),00061           randseed(seed, S),00062           randbuf(S),00063           randbuf_counter(0),00064           m_deterministicTimeVector(deterministicTimeVector)00065 {00066         <span class="keywordflow">if</span> (m_deterministicTimeVector)00067         {00068                 memset(dtbuf, 0, S);00069                 memcpy(dtbuf, (byte *)&amp;m_deterministicTimeVector, STDMIN((<span class="keywordtype">int</span>)<span class="keyword">sizeof</span>(m_deterministicTimeVector), S));00070         }00071         <span class="keywordflow">else</span>00072         {00073                 time_t tstamp1 = time(0);00074                 xorbuf(dtbuf, (byte *)&amp;tstamp1, STDMIN((<span class="keywordtype">int</span>)<span class="keyword">sizeof</span>(tstamp1), S));00075                 cipher-&gt;ProcessBlock(dtbuf);00076                 clock_t tstamp2 = clock();00077                 xorbuf(dtbuf, (byte *)&amp;tstamp2, STDMIN((<span class="keywordtype">int</span>)<span class="keyword">sizeof</span>(tstamp2), S));00078                 cipher-&gt;ProcessBlock(dtbuf);00079         }00080 }00081 <a name="l00082"></a><a class="code" href="class_x917_r_n_g.html#_x917_r_n_ga1">00082</a> byte <a class="code" href="class_x917_r_n_g.html#_x917_r_n_ga1">X917RNG::GenerateByte</a>()00083 {00084         <span class="keywordflow">if</span> (randbuf_counter==0)00085         {00086                 <span class="comment">// calculate new enciphered timestamp</span>00087                 <span class="keywordflow">if</span> (m_deterministicTimeVector)00088                 {00089                         xorbuf(dtbuf, (byte *)&amp;m_deterministicTimeVector, STDMIN((<span class="keywordtype">int</span>)<span class="keyword">sizeof</span>(m_deterministicTimeVector), S));00090                         <span class="keywordflow">while</span> (++m_deterministicTimeVector == 0) {}     <span class="comment">// skip 0</span>00091                 }00092                 <span class="keywordflow">else</span>00093                 {00094                         clock_t tstamp = clock();00095                         xorbuf(dtbuf, (byte *)&amp;tstamp, STDMIN((<span class="keywordtype">int</span>)<span class="keyword">sizeof</span>(tstamp), S));00096                 }00097                 cipher-&gt;ProcessBlock(dtbuf);00098 00099                 <span class="comment">// combine enciphered timestamp with seed</span>00100                 xorbuf(randseed, dtbuf, S);00101 00102                 <span class="comment">// generate a new block of random bytes</span>00103                 cipher-&gt;ProcessBlock(randseed, randbuf);00104 00105                 <span class="comment">// compute new seed vector</span>00106                 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;S; i++)00107                         randseed[i] = randbuf[i] ^ dtbuf[i];00108                 cipher-&gt;ProcessBlock(randseed);00109 00110                 randbuf_counter=S;00111         }00112         <span class="keywordflow">return</span>(randbuf[--randbuf_counter]);00113 }00114 00115 <span class="preprocessor">#endif</span>00116 <span class="preprocessor"></span>00117 MaurerRandomnessTest::MaurerRandomnessTest()00118         : sum(0.0), n(0)00119 {00120         <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i&lt;V; i++)00121                 tab[i] = 0;00122 }00123 00124 <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="class_buffered_transformation.html#_zlib_decompressorz1_0">MaurerRandomnessTest::Put</a>(byte inByte)00125 {00126         <span class="keywordflow">if</span> (n &gt;= Q)00127                 sum += log(<span class="keywordtype">double</span>(n - tab[inByte]));00128         tab[inByte] = n;00129         n++;00130 }00131 00132 <span class="keywordtype">void</span> <a class="code" href="class_buffered_transformation.html#_zlib_decompressorz1_0">MaurerRandomnessTest::Put</a>(<span class="keyword">const</span> byte *inString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00133 {00134         <span class="keywordflow">while</span> (length--)00135                 <a class="code" href="class_buffered_transformation.html#_zlib_decompressorz1_0">Put</a>(*inString++);00136 }00137 00138 <span class="keywordtype">double</span> MaurerRandomnessTest::GetTestValue()<span class="keyword"> const</span>00139 <span class="keyword"></span>{00140         <span class="keywordtype">double</span> fTu = (sum/(n-Q))/log(2.0);      <span class="comment">// this is the test value defined by Maurer</span>00141 00142         <span class="keywordtype">double</span> value = fTu * 0.1392;            <span class="comment">// arbitrarily normalize it to</span>00143         <span class="keywordflow">return</span> value &gt; 1.0 ? 1.0 : value;       <span class="comment">// a number between 0 and 1</span>00144 }00145 00146 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:24 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 + -