<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>ucommon: cipher.cpp</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="main.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="examples.html"><span>Examples</span></a></li> </ul> </div> </div> <div class="contents"> <h1>cipher.cpp</h1><p>Example of cipher code.</p> <div class="fragment"><pre class="fragment"><span class="comment">// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.</span> <span class="comment">//</span> <span class="comment">// This file is part of GNU uCommon C++.</span> <span class="comment">//</span> <span class="comment">// GNU uCommon C++ is free software: you can redistribute it and/or modify</span> <span class="comment">// it under the terms of the GNU Lesser General Public License as published</span> <span class="comment">// by the Free Software Foundation, either version 3 of the License, or</span> <span class="comment">// (at your option) any later version.</span> <span class="comment">//</span> <span class="comment">// GNU uCommon C++ is distributed in the hope that it will be useful,</span> <span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> <span class="comment">// GNU Lesser General Public License for more details.</span> <span class="comment">//</span> <span class="comment">// You should have received a copy of the GNU Lesser General Public License</span> <span class="comment">// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.</span> <span class="preprocessor">#ifndef DEBUG</span> <span class="preprocessor"></span><span class="preprocessor">#define DEBUG</span> <span class="preprocessor"></span><span class="preprocessor">#endif</span> <span class="preprocessor"></span> <span class="preprocessor">#include <config.h></span> <span class="preprocessor">#include <<a class="code" href="a00196.html" title="This library holds basic cryptographic functions and secure socket support for use...">ucommon/secure.h</a>></span> <span class="preprocessor">#include <stdio.h></span> <span class="keyword">using namespace </span>UCOMMON_NAMESPACE; <span class="preprocessor">#define STR "this is a test of some text we wish to post"</span> <span class="preprocessor"></span> <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv) { <span class="keywordflow">if</span>(!<a name="a0"></a><a class="code" href="a00194.html#a5fc126f9a4406cb2d0d054488cf00809" title="Template function to initialize memory by invoking default constructor.">secure::init</a>()) <span class="keywordflow">return</span> 0; <a name="a1"></a><a class="code" href="a00208.html#a6fc0e947d5804f6c146674fa3b7042c2" title="Convenience type for generic cipher key.">skey_t</a> mykey(<span class="stringliteral">"aes256"</span>, <span class="stringliteral">"sha"</span>, <span class="stringliteral">"testing"</span>); <a name="a2"></a><a class="code" href="a00208.html#a249b7f159cb1ba294206586ca8aeb4af" title="Convenience type for generic ciphers.">cipher_t</a> enc, dec; <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> ebuf[256], dbuf[256]; memset(dbuf, 0, <span class="keyword">sizeof</span>(dbuf)); enc.set(&mykey, Cipher::ENCRYPT, ebuf); dec.set(&mykey, Cipher::DECRYPT, dbuf); <span class="keywordtype">size_t</span> total = enc.puts(STR); assert(!<a name="a3"></a><a class="code" href="a00208.html#affb4ebc34459cfbe1d2965c178e52ee9" title="Compare two socket addresses to see if equal.">eq</a>(STR, (<span class="keywordtype">char</span> *)ebuf, strlen(STR))); assert(total == 48); dec.put(ebuf, total); dec.flush(); assert(<a class="code" href="a00208.html#affb4ebc34459cfbe1d2965c178e52ee9" title="Compare two socket addresses to see if equal.">eq</a>((<span class="keywordtype">char</span> *)dbuf, STR)); <span class="keywordflow">return</span> 0; } </pre></div> </div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Thu Jul 14 16:38:56 2011 for ucommon by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>