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

📄 esign_8cpp-source.html

📁 Crypto++是一个非常强大的密码学库,主要是功能全
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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++: esign.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>esign.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// esign.cpp - written and placed in the public domain by Wei Dai</span>00002 00003 <span class="preprocessor">#include "pch.h"</span>00004 <span class="preprocessor">#include "<a class="code" href="esign_8h.html">esign.h</a>"</span>00005 <span class="preprocessor">#include "asn.h"</span>00006 <span class="preprocessor">#include "modarith.h"</span>00007 <span class="preprocessor">#include "nbtheory.h"</span>00008 <span class="preprocessor">#include "sha.h"</span>00009 <span class="preprocessor">#include "algparam.h"</span>00010 00011 NAMESPACE_BEGIN(CryptoPP)00012 00013 <span class="keywordtype">void</span> ESIGN_TestInstantiations()00014 {00015         <a class="code" href="struct_e_s_i_g_n.html">ESIGN&lt;SHA&gt;</a>::Verifier x1(1, 1);00016         <a class="code" href="struct_e_s_i_g_n.html">ESIGN&lt;SHA&gt;</a>::Signer x2(NullRNG(), 1);00017         <a class="code" href="struct_e_s_i_g_n.html">ESIGN&lt;SHA&gt;</a>::Verifier x3(x2);00018         <a class="code" href="struct_e_s_i_g_n.html">ESIGN&lt;SHA&gt;</a>::Verifier x4(x2.GetKey());00019         <a class="code" href="struct_e_s_i_g_n.html">ESIGN&lt;SHA&gt;</a>::Verifier x5(x3);00020         <a class="code" href="struct_e_s_i_g_n.html">ESIGN&lt;SHA&gt;</a>::Signer x6 = x2;00021 00022         x6 = x2;00023         x3 = <a class="code" href="class_t_f___s_s_a.html#_t_f___s_s_aw4">ESIGN&lt;SHA&gt;::Verifier</a>(x2);00024         x4 = x2.GetKey();00025 }00026 <a name="l00027"></a><a class="code" href="class_e_s_i_g_n_function.html#_e_s_i_g_n_functiona1">00027</a> <span class="keywordtype">void</span> <a class="code" href="class_e_s_i_g_n_function.html#_e_s_i_g_n_functiona1">ESIGNFunction::BERDecode</a>(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &amp;bt)00028 {00029         <a class="code" href="class_b_e_r_sequence_decoder.html">BERSequenceDecoder</a> seq(bt);00030                 m_n.<a class="code" href="class_integer.html#_integerz39_9">BERDecode</a>(seq);00031                 m_e.<a class="code" href="class_integer.html#_integerz39_9">BERDecode</a>(seq);00032         seq.<a class="code" href="class_b_e_r_general_decoder.html#_b_e_r_set_decodera9">MessageEnd</a>();00033 }00034 <a name="l00035"></a><a class="code" href="class_e_s_i_g_n_function.html#_e_s_i_g_n_functiona2">00035</a> <span class="keywordtype">void</span> <a class="code" href="class_e_s_i_g_n_function.html#_e_s_i_g_n_functiona2">ESIGNFunction::DEREncode</a>(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &amp;bt)<span class="keyword"> const</span>00036 <span class="keyword"></span>{00037         <a class="code" href="class_d_e_r_sequence_encoder.html">DERSequenceEncoder</a> seq(bt);00038                 m_n.<a class="code" href="class_integer.html#_integerz39_3">DEREncode</a>(seq);00039                 m_e.<a class="code" href="class_integer.html#_integerz39_3">DEREncode</a>(seq);00040         seq.<a class="code" href="class_d_e_r_general_encoder.html#_d_e_r_set_encodera2">MessageEnd</a>();00041 }00042 00043 <a class="code" href="class_integer.html">Integer</a> ESIGNFunction::ApplyFunction(<span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;x)<span class="keyword"> const</span>00044 <span class="keyword"></span>{00045         DoQuickSanityCheck();00046         <span class="keywordflow">return</span> STDMIN(a_exp_b_mod_c(x, m_e, m_n) &gt;&gt; (2*GetK()+2), MaxImage());00047 }00048 <a name="l00049"></a><a class="code" href="class_e_s_i_g_n_function.html#_e_s_i_g_n_functiona3">00049</a> <span class="keywordtype">bool</span> <a class="code" href="class_e_s_i_g_n_function.html#_e_s_i_g_n_functiona3">ESIGNFunction::Validate</a>(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &amp;rng, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> level)<span class="keyword"> const</span>00050 <span class="keyword"></span>{00051         <span class="keywordtype">bool</span> pass = <span class="keyword">true</span>;00052         pass = pass &amp;&amp; m_n &gt; <a class="code" href="class_integer.html#_integerz37_11">Integer::One</a>() &amp;&amp; m_n.<a class="code" href="class_integer.html#_integerz41_15">IsOdd</a>();00053         pass = pass &amp;&amp; m_e &gt;= 8 &amp;&amp; m_e &lt; m_n;00054         <span class="keywordflow">return</span> pass;00055 }00056 <a name="l00057"></a><a class="code" href="class_e_s_i_g_n_function.html#_e_s_i_g_n_functiona4">00057</a> <span class="keywordtype">bool</span> <a class="code" href="class_e_s_i_g_n_function.html#_e_s_i_g_n_functiona4">ESIGNFunction::GetVoidValue</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <span class="keyword">const</span> std::type_info &amp;valueType, <span class="keywordtype">void</span> *pValue)<span class="keyword"> const</span>00058 <span class="keyword"></span>{00059         <span class="keywordflow">return</span> GetValueHelper(<span class="keyword">this</span>, name, valueType, pValue).Assignable()00060                 CRYPTOPP_GET_FUNCTION_ENTRY(<a class="code" href="namespace_name.html#a15">Modulus</a>)00061                 CRYPTOPP_GET_FUNCTION_ENTRY(<a class="code" href="namespace_name.html#a16">PublicExponent</a>)00062                 ;00063 }00064 <a name="l00065"></a><a class="code" href="class_e_s_i_g_n_function.html#_e_s_i_g_n_functiona5">00065</a> <span class="keywordtype">void</span> <a class="code" href="class_e_s_i_g_n_function.html#_e_s_i_g_n_functiona5">ESIGNFunction::AssignFrom</a>(<span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &amp;source)00066 {00067         AssignFromHelper(<span class="keyword">this</span>, source)00068                 CRYPTOPP_SET_FUNCTION_ENTRY(<a class="code" href="namespace_name.html#a15">Modulus</a>)00069                 CRYPTOPP_SET_FUNCTION_ENTRY(<a class="code" href="namespace_name.html#a16">PublicExponent</a>)00070                 ;00071 }00072 00073 <span class="comment">// *****************************************************************************</span>00074 <a name="l00075"></a><a class="code" href="class_invertible_e_s_i_g_n_function.html#_invertible_e_s_i_g_n_functiona8">00075</a> <span class="keywordtype">void</span> <a class="code" href="class_invertible_e_s_i_g_n_function.html#_invertible_e_s_i_g_n_functiona8">InvertibleESIGNFunction::GenerateRandom</a>(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &amp;rng, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &amp;param)00076 {00077         <span class="keywordtype">int</span> modulusSize = 1023*2;00078         param.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha42">GetIntValue</a>(<span class="stringliteral">"ModulusSize"</span>, modulusSize) || param.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha42">GetIntValue</a>(<span class="stringliteral">"KeySize"</span>, modulusSize);00079 00080         <span class="keywordflow">if</span> (modulusSize &lt; 24)00081                 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html">InvalidArgument</a>(<span class="stringliteral">"InvertibleESIGNFunction: specified modulus size is too small"</span>);00082 00083         <span class="keywordflow">if</span> (modulusSize % 3 != 0)00084                 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html">InvalidArgument</a>(<span class="stringliteral">"InvertibleESIGNFunction: modulus size must be divisible by 3"</span>);00085 00086         m_e = param.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha40">GetValueWithDefault</a>(<span class="stringliteral">"PublicExponent"</span>, <a class="code" href="class_integer.html">Integer</a>(32));00087 00088         <span class="keywordflow">if</span> (m_e &lt; 8)00089                 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html">InvalidArgument</a>(<span class="stringliteral">"InvertibleESIGNFunction: public exponents less than 8 may not be secure"</span>);00090 00091         <span class="comment">// VC70 workaround: putting these after primeParam causes overlapped stack allocation</span>00092         <a class="code" href="class_const_byte_array_parameter.html">ConstByteArrayParameter</a> seedParam;00093         <a class="code" href="class_sec_block.html">SecByteBlock</a> seed;00094 00095         <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> minP = <a class="code" href="class_integer.html">Integer</a>(204) &lt;&lt; (modulusSize/3-8);00096         <span class="keyword">const</span> Integer maxP = <a class="code" href="class_integer.html#_integerz37_13">Integer::Power2</a>(modulusSize/3)-1;00097         <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &amp;primeParam = MakeParameters(<span class="stringliteral">"Min"</span>, minP)(<span class="stringliteral">"Max"</span>, maxP)(<span class="stringliteral">"RandomNumberType"</span>, Integer::PRIME);00098 00099         <span class="keywordflow">if</span> (param.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha39">GetValue</a>(<span class="stringliteral">"Seed"</span>, seedParam))00100         {00101                 seed.<a class="code" href="class_sec_block.html#_sec_block_with_hinta23">resize</a>(seedParam.<a class="code" href="class_const_byte_array_parameter.html#_const_byte_array_parametera6">size</a>() + 4);00102                 memcpy(seed + 4, seedParam.<a class="code" href="class_const_byte_array_parameter.html#_const_byte_array_parametera4">begin</a>(), seedParam.<a class="code" href="class_const_byte_array_parameter.html#_const_byte_array_parametera6">size</a>());00103 00104                 UnalignedPutWord(BIG_ENDIAN_ORDER, seed, (word32)0);00105                 m_p.<a class="code" href="class_integer.html#_integerz43_14">GenerateRandom</a>(rng, CombinedNameValuePairs(primeParam, MakeParameters(<span class="stringliteral">"Seed"</span>, <a class="code" href="class_const_byte_array_parameter.html">ConstByteArrayParameter</a>(seed))));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -