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

📄 default_8cpp-source.html

📁 著名的密码库Crypto++的文档 C++语言的杰作。程序员必备。
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<a name="l00075"></a>00075 }<a name="l00076"></a>00076 <a name="l00077"></a><a class="code" href="class_default_encryptor.html#a2bfc09437b2ec346ab013ed6afda830">00077</a> <a class="code" href="class_default_encryptor.html#351c407758faa27a7fc50c2d49d9b439">DefaultEncryptor::DefaultEncryptor</a>(<span class="keyword">const</span> byte *passphrase, <span class="keywordtype">size_t</span> passphraseLength, <a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> *attachment)<a name="l00078"></a>00078         : <a class="code" href="class_proxy_filter.html" title="Base class for Filter classes that are proxies for a chain of other filters.">ProxyFilter</a>(NULL, 0, 0, attachment), m_passphrase(passphrase, passphraseLength)<a name="l00079"></a>00079 {<a name="l00080"></a>00080 }<a name="l00081"></a>00081 <a name="l00082"></a>00082 <a name="l00083"></a><a class="code" href="class_default_encryptor.html#0d4a29cb66bdce3360a2233fa916cb99">00083</a> <span class="keywordtype">void</span> <a class="code" href="class_default_encryptor.html#0d4a29cb66bdce3360a2233fa916cb99">DefaultEncryptor::FirstPut</a>(<span class="keyword">const</span> byte *)<a name="l00084"></a>00084 {<a name="l00085"></a>00085         <span class="comment">// VC60 workaround: __LINE__ expansion bug</span><a name="l00086"></a>00086         CRYPTOPP_COMPILE_ASSERT_INSTANCE(SALTLENGTH &lt;= <a class="code" href="class_iterated_hash_with_static_transform.html#86286d03ba76ccdd7bf3007088250859">DefaultHashModule::DIGESTSIZE</a>, 1);<a name="l00087"></a>00087         CRYPTOPP_COMPILE_ASSERT_INSTANCE(BLOCKSIZE &lt;= <a class="code" href="class_iterated_hash_with_static_transform.html#86286d03ba76ccdd7bf3007088250859">DefaultHashModule::DIGESTSIZE</a>, 2);<a name="l00088"></a>00088 <a name="l00089"></a>00089         <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> salt(<a class="code" href="class_iterated_hash_with_static_transform.html#86286d03ba76ccdd7bf3007088250859">DefaultHashModule::DIGESTSIZE</a>), keyCheck(<a class="code" href="class_iterated_hash_with_static_transform.html#86286d03ba76ccdd7bf3007088250859">DefaultHashModule::DIGESTSIZE</a>);<a name="l00090"></a>00090         <a class="code" href="class_s_h_a1.html" title="SHA-1">DefaultHashModule</a> hash;<a name="l00091"></a>00091 <a name="l00092"></a>00092         <span class="comment">// use hash(passphrase | time | clock) as salt</span><a name="l00093"></a>00093         hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(m_passphrase, m_passphrase.<a class="code" href="class_sec_block.html#f5999bffe3193e62719cc0792b0282a7">size</a>());<a name="l00094"></a>00094         time_t t=time(0);<a name="l00095"></a>00095         hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>((byte *)&amp;t, <span class="keyword">sizeof</span>(t));<a name="l00096"></a>00096         clock_t c=clock();<a name="l00097"></a>00097         hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>((byte *)&amp;c, <span class="keyword">sizeof</span>(c));<a name="l00098"></a>00098         hash.<a class="code" href="class_hash_transformation.html#a0b8c7a110d8968268fd02ec32b9a8e8" title="compute hash for current message, then restart for a new message">Final</a>(salt);<a name="l00099"></a>00099 <a name="l00100"></a>00100         <span class="comment">// use hash(passphrase | salt) as key check</span><a name="l00101"></a>00101         hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(m_passphrase, m_passphrase.<a class="code" href="class_sec_block.html#f5999bffe3193e62719cc0792b0282a7">size</a>());<a name="l00102"></a>00102         hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(salt, SALTLENGTH);<a name="l00103"></a>00103         hash.<a class="code" href="class_hash_transformation.html#a0b8c7a110d8968268fd02ec32b9a8e8" title="compute hash for current message, then restart for a new message">Final</a>(keyCheck);<a name="l00104"></a>00104 <a name="l00105"></a>00105         <a class="code" href="class_filter.html#c8fc888888b2dad88bf64fc703044a93" title="returns the object immediately attached to this object or NULL for no attachment">AttachedTransformation</a>()-&gt;<a class="code" href="class_buffered_transformation.html#e70658b0d271f8e114ac6c3cc9774ede" title="input a byte for processing">Put</a>(salt, SALTLENGTH);<a name="l00106"></a>00106 <a name="l00107"></a>00107         <span class="comment">// mash passphrase and salt together into key and IV</span><a name="l00108"></a>00108         <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> key(KEYLENGTH);<a name="l00109"></a>00109         <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> IV(BLOCKSIZE);<a name="l00110"></a>00110         GenerateKeyIV(m_passphrase, m_passphrase.<a class="code" href="class_sec_block.html#f5999bffe3193e62719cc0792b0282a7">size</a>(), salt, SALTLENGTH, key, IV);<a name="l00111"></a>00111 <a name="l00112"></a>00112         m_cipher.SetKeyWithIV(key, key.size(), IV);<a name="l00113"></a>00113         <a class="code" href="class_proxy_filter.html#98a1d161913c42feb64951c396f0f3e0">SetFilter</a>(<span class="keyword">new</span> <a class="code" href="class_stream_transformation_filter.html" title="Filter Wrapper for StreamTransformation.">StreamTransformationFilter</a>(m_cipher));<a name="l00114"></a>00114 <a name="l00115"></a>00115         <a class="code" href="class_proxy_filter.html#1f89e47a61f678d1d2dbb52ca2ba2996">m_filter</a>-&gt;Put(keyCheck, BLOCKSIZE);<a name="l00116"></a>00116 }<a name="l00117"></a>00117 <a name="l00118"></a><a class="code" href="class_default_encryptor.html#d40665647f5693f8350fc9b9375ca078">00118</a> <span class="keywordtype">void</span> <a class="code" href="class_default_encryptor.html#d40665647f5693f8350fc9b9375ca078">DefaultEncryptor::LastPut</a>(<span class="keyword">const</span> byte *inString, <span class="keywordtype">size_t</span> length)<a name="l00119"></a>00119 {<a name="l00120"></a>00120         <a class="code" href="class_proxy_filter.html#1f89e47a61f678d1d2dbb52ca2ba2996">m_filter</a>-&gt;MessageEnd();<a name="l00121"></a>00121 }<a name="l00122"></a>00122 <a name="l00123"></a>00123 <span class="comment">// ********************************************************</span><a name="l00124"></a>00124 <a name="l00125"></a><a class="code" href="class_default_decryptor.html#bccfb4027c9af76b33c242d4cbda6879">00125</a> <a class="code" href="class_default_decryptor.html#bccfb4027c9af76b33c242d4cbda6879">DefaultDecryptor::DefaultDecryptor</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *p, <a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> *attachment, <span class="keywordtype">bool</span> throwException)<a name="l00126"></a>00126         : <a class="code" href="class_proxy_filter.html" title="Base class for Filter classes that are proxies for a chain of other filters.">ProxyFilter</a>(NULL, SALTLENGTH+BLOCKSIZE, 0, attachment)<a name="l00127"></a>00127         , m_state(WAITING_FOR_KEYCHECK)<a name="l00128"></a>00128         , m_passphrase((const byte *)p, strlen(p))<a name="l00129"></a>00129         , m_throwException(throwException)<a name="l00130"></a>00130 {<a name="l00131"></a>00131 }<a name="l00132"></a>00132 <a name="l00133"></a><a class="code" href="class_default_decryptor.html#dceb654320c139fa8d0d13f88a98afc6">00133</a> <a class="code" href="class_default_decryptor.html#bccfb4027c9af76b33c242d4cbda6879">DefaultDecryptor::DefaultDecryptor</a>(<span class="keyword">const</span> byte *passphrase, <span class="keywordtype">size_t</span> passphraseLength, <a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> *attachment, <span class="keywordtype">bool</span> throwException)<a name="l00134"></a>00134         : <a class="code" href="class_proxy_filter.html" title="Base class for Filter classes that are proxies for a chain of other filters.">ProxyFilter</a>(NULL, SALTLENGTH+BLOCKSIZE, 0, attachment)<a name="l00135"></a>00135         , <a class="code" href="class_default_decryptor.html#9b887466b08abc8eea2c2825b9ab3d2d">m_state</a>(<a class="code" href="class_default_decryptor.html#7961421ac0c0b58da061c793afbd8fa72220b3348856ea0c6e9c6619fc39ca38">WAITING_FOR_KEYCHECK</a>)<a name="l00136"></a>00136         , m_passphrase(passphrase, passphraseLength)<a name="l00137"></a>00137         , m_throwException(throwException)<a name="l00138"></a>00138 {<a name="l00139"></a>00139 }<a name="l00140"></a>00140 <a name="l00141"></a><a class="code" href="class_default_decryptor.html#a1ee016673cf4154c9b20f287ba206d3">00141</a> <span class="keywordtype">void</span> <a class="code" href="class_default_decryptor.html#a1ee016673cf4154c9b20f287ba206d3">DefaultDecryptor::FirstPut</a>(<span class="keyword">const</span> byte *inString)<a name="l00142"></a>00142 {<a name="l00143"></a>00143         CheckKey(inString, inString+SALTLENGTH);<a name="l00144"></a>00144 }<a name="l00145"></a>00145 <a name="l00146"></a><a class="code" href="class_default_decryptor.html#3b8b9a23c00e6ccaae81b1d3e6ebb3e7">00146</a> <span class="keywordtype">void</span> <a class="code" href="class_default_decryptor.html#3b8b9a23c00e6ccaae81b1d3e6ebb3e7">DefaultDecryptor::LastPut</a>(<span class="keyword">const</span> byte *inString, <span class="keywordtype">size_t</span> length)<a name="l00147"></a>00147 {<a name="l00148"></a>00148         <span class="keywordflow">if</span> (<a class="code" href="class_proxy_filter.html#1f89e47a61f678d1d2dbb52ca2ba2996">m_filter</a>.<a class="code" href="classmember__ptr.html#2d10dec2a51691b589d56b71ec78e59a">get</a>() == NULL)<a name="l00149"></a>00149         {<a name="l00150"></a>00150                 <a class="code" href="class_default_decryptor.html#9b887466b08abc8eea2c2825b9ab3d2d">m_state</a> = <a class="code" href="class_default_decryptor.html#7961421ac0c0b58da061c793afbd8fa7bd88dfa019e7b11db45c6a10fc9008a6">KEY_BAD</a>;<a name="l00151"></a>00151                 <span class="keywordflow">if</span> (m_throwException)<a name="l00152"></a>00152                         <span class="keywordflow">throw</span> <a class="code" href="class_default_decryptor_1_1_key_bad_err.html">KeyBadErr</a>();<a name="l00153"></a>00153         }<a name="l00154"></a>00154         <span class="keywordflow">else</span><a name="l00155"></a>00155         {<a name="l00156"></a>00156                 <a class="code" href="class_proxy_filter.html#1f89e47a61f678d1d2dbb52ca2ba2996">m_filter</a>-&gt;MessageEnd();<a name="l00157"></a>00157                 <a class="code" href="class_default_decryptor.html#9b887466b08abc8eea2c2825b9ab3d2d">m_state</a> = <a class="code" href="class_default_decryptor.html#7961421ac0c0b58da061c793afbd8fa72220b3348856ea0c6e9c6619fc39ca38">WAITING_FOR_KEYCHECK</a>;<a name="l00158"></a>00158         }<a name="l00159"></a>00159 }<a name="l00160"></a>00160 <a name="l00161"></a>00161 <span class="keywordtype">void</span> DefaultDecryptor::CheckKey(<span class="keyword">const</span> byte *salt, <span class="keyword">const</span> byte *keyCheck)<a name="l00162"></a>00162 {<a name="l00163"></a>00163         <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> check(STDMAX((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)2*BLOCKSIZE, (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)<a class="code" href="class_iterated_hash_with_static_transform.html#86286d03ba76ccdd7bf3007088250859">DefaultHashModule::DIGESTSIZE</a>));<a name="l00164"></a>00164 <a name="l00165"></a>00165         <a class="code" href="class_s_h_a1.html" title="SHA-1">DefaultHashModule</a> hash;<a name="l00166"></a>00166         hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(m_passphrase, m_passphrase.<a class="code" href="class_sec_block.html#f5999bffe3193e62719cc0792b0282a7">size</a>());<a name="l00167"></a>00167         hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(salt, SALTLENGTH);<a name="l00168"></a>00168         hash.<a class="code" href="class_hash_transformation.html#a0b8c7a110d8968268fd02ec32b9a8e8" title="compute hash for current message, then restart for a new message">Final</a>(check);<a name="l00169"></a>00169 

⌨️ 快捷键说明

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