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

📄 default_8cpp-source.html

📁 著名的密码库Crypto++的文档 C++语言的杰作。程序员必备。
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>Crypto++: default.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.2 --><div class="tabs">  <ul>    <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>    <li><a href="namespaces.html"><span>Namespaces</span></a></li>    <li><a href="classes.html"><span>Classes</span></a></li>    <li class="current"><a href="files.html"><span>Files</span></a></li>  </ul></div><div class="tabs">  <ul>    <li><a href="files.html"><span>File&nbsp;List</span></a></li>    <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>  </ul></div><h1>default.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// default.cpp - written and placed in the public domain by Wei Dai</span><a name="l00002"></a>00002 <a name="l00003"></a>00003 <span class="preprocessor">#include "pch.h"</span><a name="l00004"></a>00004 <span class="preprocessor">#include "default.h"</span><a name="l00005"></a>00005 <span class="preprocessor">#include "queue.h"</span><a name="l00006"></a>00006 <span class="preprocessor">#include &lt;time.h&gt;</span><a name="l00007"></a>00007 <span class="preprocessor">#include &lt;memory&gt;</span><a name="l00008"></a>00008 <a name="l00009"></a>00009 NAMESPACE_BEGIN(CryptoPP)<a name="l00010"></a>00010 <a name="l00011"></a>00011 static const <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> MASH_ITERATIONS = 200;<a name="l00012"></a>00012 static const <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> SALTLENGTH = 8;<a name="l00013"></a>00013 static const <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> BLOCKSIZE = <a class="code" href="class_d_e_s___e_d_e2.html" title="DES-EDE2">Default_BlockCipher</a>::Encryption::BLOCKSIZE;<a name="l00014"></a>00014 static const <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> KEYLENGTH = <a class="code" href="class_d_e_s___e_d_e2.html" title="DES-EDE2">Default_BlockCipher</a>::Encryption::DEFAULT_KEYLENGTH;<a name="l00015"></a>00015 <a name="l00016"></a>00016 <span class="comment">// The purpose of this function Mash() is to take an arbitrary length input</span><a name="l00017"></a>00017 <span class="comment">// string and *deterministicly* produce an arbitrary length output string such</span><a name="l00018"></a>00018 <span class="comment">// that (1) it looks random, (2) no information about the input is</span><a name="l00019"></a>00019 <span class="comment">// deducible from it, and (3) it contains as much entropy as it can hold, or</span><a name="l00020"></a>00020 <span class="comment">// the amount of entropy in the input string, whichever is smaller.</span><a name="l00021"></a>00021 <a name="l00022"></a>00022 static <span class="keywordtype">void</span> Mash(const byte *in, <span class="keywordtype">size_t</span> inLen, byte *out, <span class="keywordtype">size_t</span> outLen, <span class="keywordtype">int</span> iterations)<a name="l00023"></a>00023 {<a name="l00024"></a>00024         <span class="keywordflow">if</span> (BytePrecision(outLen) &gt; 2)<a name="l00025"></a>00025                 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html" title="exception thrown when an invalid argument is detected">InvalidArgument</a>(<span class="stringliteral">"Mash: output legnth too large"</span>);<a name="l00026"></a>00026 <a name="l00027"></a>00027         <span class="keywordtype">size_t</span> bufSize = RoundUpToMultipleOf(outLen, (<span class="keywordtype">size_t</span>)<a class="code" href="class_iterated_hash_with_static_transform.html#86286d03ba76ccdd7bf3007088250859">DefaultHashModule::DIGESTSIZE</a>);<a name="l00028"></a>00028         byte b[2];<a name="l00029"></a>00029         <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> buf(bufSize);<a name="l00030"></a>00030         <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> outBuf(bufSize);<a name="l00031"></a>00031         <a class="code" href="class_s_h_a1.html" title="SHA-1">DefaultHashModule</a> hash;<a name="l00032"></a>00032 <a name="l00033"></a>00033         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;<a name="l00034"></a>00034         <span class="keywordflow">for</span>(i=0; i&lt;outLen; i+=<a class="code" href="class_iterated_hash_with_static_transform.html#86286d03ba76ccdd7bf3007088250859">DefaultHashModule::DIGESTSIZE</a>)<a name="l00035"></a>00035         {<a name="l00036"></a>00036                 b[0] = (byte) (i &gt;&gt; 8);<a name="l00037"></a>00037                 b[1] = (byte) i;<a name="l00038"></a>00038                 hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(b, 2);<a name="l00039"></a>00039                 hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(in, inLen);<a name="l00040"></a>00040                 hash.<a class="code" href="class_hash_transformation.html#a0b8c7a110d8968268fd02ec32b9a8e8" title="compute hash for current message, then restart for a new message">Final</a>(outBuf+i);<a name="l00041"></a>00041         }<a name="l00042"></a>00042 <a name="l00043"></a>00043         <span class="keywordflow">while</span> (iterations-- &gt; 1)<a name="l00044"></a>00044         {<a name="l00045"></a>00045                 memcpy(buf, outBuf, bufSize);<a name="l00046"></a>00046                 <span class="keywordflow">for</span> (i=0; i&lt;bufSize; i+=<a class="code" href="class_iterated_hash_with_static_transform.html#86286d03ba76ccdd7bf3007088250859">DefaultHashModule::DIGESTSIZE</a>)<a name="l00047"></a>00047                 {<a name="l00048"></a>00048                         b[0] = (byte) (i &gt;&gt; 8);<a name="l00049"></a>00049                         b[1] = (byte) i;<a name="l00050"></a>00050                         hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(b, 2);<a name="l00051"></a>00051                         hash.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(buf, bufSize);<a name="l00052"></a>00052                         hash.<a class="code" href="class_hash_transformation.html#a0b8c7a110d8968268fd02ec32b9a8e8" title="compute hash for current message, then restart for a new message">Final</a>(outBuf+i);<a name="l00053"></a>00053                 }<a name="l00054"></a>00054         }<a name="l00055"></a>00055 <a name="l00056"></a>00056         memcpy(out, outBuf, outLen);<a name="l00057"></a>00057 }<a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="keyword">static</span> <span class="keywordtype">void</span> GenerateKeyIV(<span class="keyword">const</span> byte *passphrase, <span class="keywordtype">size_t</span> passphraseLength, <span class="keyword">const</span> byte *salt, <span class="keywordtype">size_t</span> saltLength, byte *key, byte *IV)<a name="l00060"></a>00060 {<a name="l00061"></a>00061         <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> temp(passphraseLength+saltLength);<a name="l00062"></a>00062         memcpy(temp, passphrase, passphraseLength);<a name="l00063"></a>00063         memcpy(temp+passphraseLength, salt, saltLength);<a name="l00064"></a>00064         <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> keyIV(KEYLENGTH+BLOCKSIZE);<a name="l00065"></a>00065         Mash(temp, passphraseLength + saltLength, keyIV, KEYLENGTH+BLOCKSIZE, MASH_ITERATIONS);<a name="l00066"></a>00066         memcpy(key, keyIV, KEYLENGTH);<a name="l00067"></a>00067         memcpy(IV, keyIV+KEYLENGTH, BLOCKSIZE);<a name="l00068"></a>00068 }<a name="l00069"></a>00069 <a name="l00070"></a>00070 <span class="comment">// ********************************************************</span><a name="l00071"></a>00071 <a name="l00072"></a><a class="code" href="class_default_encryptor.html#351c407758faa27a7fc50c2d49d9b439">00072</a> <a class="code" href="class_default_encryptor.html#351c407758faa27a7fc50c2d49d9b439">DefaultEncryptor::DefaultEncryptor</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *passphrase, <a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> *attachment)<a name="l00073"></a>00073         : <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((const byte *)passphrase, strlen(passphrase))<a name="l00074"></a>00074 {

⌨️ 快捷键说明

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