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

📄 default_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++: default.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>default.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// default.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 "default.h"</span>00005 <span class="preprocessor">#include "queue.h"</span>00006 <span class="preprocessor">#include &lt;time.h&gt;</span>00007 <span class="preprocessor">#include &lt;memory&gt;</span>00008 00009 NAMESPACE_BEGIN(CryptoPP)00010 00011 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> MASH_ITERATIONS = 200;00012 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> SALTLENGTH = 8;00013 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> BLOCKSIZE = Default_BlockCipher::Encryption::BLOCKSIZE;00014 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> KEYLENGTH = Default_BlockCipher::Encryption::DEFAULT_KEYLENGTH;00015 00016 <span class="comment">// The purpose of this function Mash() is to take an arbitrary length input</span>00017 <span class="comment">// string and *deterministicly* produce an arbitrary length output string such</span>00018 <span class="comment">// that (1) it looks random, (2) no information about the input is</span>00019 <span class="comment">// deducible from it, and (3) it contains as much entropy as it can hold, or</span>00020 <span class="comment">// the amount of entropy in the input string, whichever is smaller.</span>00021 00022 <span class="keyword">static</span> <span class="keywordtype">void</span> Mash(<span class="keyword">const</span> byte *in, word16 inLen, byte *out, word16 outLen, <span class="keywordtype">int</span> iterations)00023 {00024         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufSize = (outLen-1+DefaultHashModule::DIGESTSIZE-((outLen-1)%DefaultHashModule::DIGESTSIZE));00025 00026         <span class="comment">// ASSERT: bufSize == (the smallest multiple of DIGESTSIZE that is &gt;= outLen)</span>00027 00028         byte b[2];00029         <a class="code" href="class_sec_block.html">SecByteBlock</a> buf(bufSize);00030         <a class="code" href="class_sec_block.html">SecByteBlock</a> outBuf(bufSize);00031         <a class="code" href="class_s_h_a.html">DefaultHashModule</a> hash;00032 00033         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;00034         <span class="keywordflow">for</span>(i=0; i&lt;outLen; i+=DefaultHashModule::DIGESTSIZE)00035         {00036                 b[0] = (byte) i &gt;&gt; 8;00037                 b[1] = (byte) i;00038                 hash.Update(b, 2);00039                 hash.Update(in, inLen);00040                 hash.<a class="code" href="class_hash_transformation.html#_x_m_a_c_c___basea13">Final</a>(outBuf+i);00041         }00042 00043         <span class="keywordflow">while</span> (iterations-- &gt; 1)00044         {00045                 memcpy(buf, outBuf, bufSize);00046                 <span class="keywordflow">for</span> (i=0; i&lt;bufSize; i+=DefaultHashModule::DIGESTSIZE)00047                 {00048                         b[0] = (byte) i &gt;&gt; 8;00049                         b[1] = (byte) i;00050                         hash.Update(b, 2);00051                         hash.Update(buf, bufSize);00052                         hash.<a class="code" href="class_hash_transformation.html#_x_m_a_c_c___basea13">Final</a>(outBuf+i);00053                 }00054         }00055 00056         memcpy(out, outBuf, outLen);00057 }00058 00059 <span class="keyword">static</span> <span class="keywordtype">void</span> GenerateKeyIV(<span class="keyword">const</span> byte *passphrase, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> passphraseLength, <span class="keyword">const</span> byte *salt, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> saltLength, byte *key, byte *IV)00060 {00061         <a class="code" href="class_sec_block.html">SecByteBlock</a> temp(passphraseLength+saltLength);00062         memcpy(temp, passphrase, passphraseLength);00063         memcpy(temp+passphraseLength, salt, saltLength);00064         <a class="code" href="class_sec_block.html">SecByteBlock</a> keyIV(KEYLENGTH+BLOCKSIZE);00065         Mash(temp, passphraseLength + saltLength, keyIV, KEYLENGTH+BLOCKSIZE, MASH_ITERATIONS);00066         memcpy(key, keyIV, KEYLENGTH);00067         memcpy(IV, keyIV+KEYLENGTH, BLOCKSIZE);00068 }00069 00070 <span class="comment">// ********************************************************</span>00071 00072 DefaultEncryptor::DefaultEncryptor(<span class="keyword">const</span> <span class="keywordtype">char</span> *passphrase, <a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> *attachment)00073         : <a class="code" href="class_proxy_filter.html">ProxyFilter</a>(NULL, 0, 0, attachment), m_passphrase((const byte *)passphrase, strlen(passphrase))00074 {00075 }00076 00077 DefaultEncryptor::DefaultEncryptor(<span class="keyword">const</span> byte *passphrase, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> passphraseLength, <a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> *attachment)00078         : <a class="code" href="class_proxy_filter.html">ProxyFilter</a>(NULL, 0, 0, attachment), m_passphrase(passphrase, passphraseLength)00079 {00080 }00081 00082 00083 <span class="keywordtype">void</span> DefaultEncryptor::FirstPut(<span class="keyword">const</span> byte *)00084 {00085         <span class="comment">// VC60 workaround: __LINE__ expansion bug</span>00086         CRYPTOPP_COMPILE_ASSERT_INSTANCE(SALTLENGTH &lt;= DefaultHashModule::DIGESTSIZE, 1);00087         CRYPTOPP_COMPILE_ASSERT_INSTANCE(BLOCKSIZE &lt;= DefaultHashModule::DIGESTSIZE, 2);00088 00089         <a class="code" href="class_sec_block.html">SecByteBlock</a> salt(DefaultHashModule::DIGESTSIZE), keyCheck(DefaultHashModule::DIGESTSIZE);00090         <a class="code" href="class_s_h_a.html">DefaultHashModule</a> hash;00091 00092         <span class="comment">// use hash(passphrase | time | clock) as salt</span>00093         hash.Update(m_passphrase, m_passphrase.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>());00094         time_t t=time(0);00095         hash.Update((byte *)&amp;t, <span class="keyword">sizeof</span>(t));00096         clock_t c=clock();00097         hash.Update((byte *)&amp;c, <span class="keyword">sizeof</span>(c));00098         hash.<a class="code" href="class_hash_transformation.html#_x_m_a_c_c___basea13">Final</a>(salt);00099 00100         <span class="comment">// use hash(passphrase | salt) as key check</span>00101         hash.Update(m_passphrase, m_passphrase.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>());00102         hash.Update(salt, SALTLENGTH);00103         hash.<a class="code" href="class_hash_transformation.html#_x_m_a_c_c___basea13">Final</a>(keyCheck);00104 00105         <a class="code" href="class_filter.html#_zlib_decompressora8">AttachedTransformation</a>()-&gt;<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz1_0">Put</a>(salt, SALTLENGTH);00106 00107         <span class="comment">// mash passphrase and salt together into key and IV</span>00108         <a class="code" href="class_sec_block.html">SecByteBlock</a> key(KEYLENGTH);00109         <a class="code" href="class_sec_block.html">SecByteBlock</a> <a class="code" href="namespace_name.html#a4">IV</a>(BLOCKSIZE);00110         GenerateKeyIV(m_passphrase, m_passphrase.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>(), salt, SALTLENGTH, key, <a class="code" href="namespace_name.html#a4">IV</a>);00111 00112         m_cipher.SetKeyWithIV(key, key.size(), <a class="code" href="namespace_name.html#a4">IV</a>);00113         SetFilter(<span class="keyword">new</span> <a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(m_cipher));00114 00115         m_filter-&gt;Put(keyCheck, BLOCKSIZE);00116 }00117 00118 <span class="keywordtype">void</span> DefaultEncryptor::LastPut(<span class="keyword">const</span> byte *inString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00119 {00120         m_filter-&gt;MessageEnd();00121 }00122 00123 <span class="comment">// ********************************************************</span>00124 00125 DefaultDecryptor::DefaultDecryptor(<span class="keyword">const</span> <span class="keywordtype">char</span> *p, <a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> *attachment, <span class="keywordtype">bool</span> throwException)00126         : <a class="code" href="class_proxy_filter.html">ProxyFilter</a>(NULL, SALTLENGTH+BLOCKSIZE, 0, attachment)00127         , m_state(WAITING_FOR_KEYCHECK)00128         , m_passphrase((const byte *)p, strlen(p))00129         , m_throwException(throwException)

⌨️ 快捷键说明

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