distrib > Mageia > cauldron > i586 > by-pkgid > 92a9f9a2ff17105809ab69f8e09db031 > files > 68


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Getting Started &#8212; Botan</title>
    <link rel="stylesheet" href="_static/agogo.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="_static/language_data.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Information Flow: Pipes and Filters" href="filters.html" />
    <link rel="prev" title="Building The Library" href="building.html" /> 
    <div class="header-wrapper">
      <div class="header">

    <div class="content-wrapper">
      <div class="content">
        <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
  <div class="section" id="getting-started">
<h1>Getting Started<a class="headerlink" href="#getting-started" title="Permalink to this headline">¶</a></h1>
<p>All declarations in the library are contained within the namespace
<code class="docutils literal notranslate"><span class="pre">Botan</span></code>, so you need to either prefix types with <code class="docutils literal notranslate"><span class="pre">Botan::</span></code> or add
a <code class="docutils literal notranslate"><span class="pre">using</span></code> declaration in your code. All examples will assume a
<code class="docutils literal notranslate"><span class="pre">using</span></code> declaration.</p>
<p>All library headers are included like so:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;botan/botan.h&gt;</span><span class="cp"></span>
<div class="section" id="initializing-the-library">
<h2>Initializing the Library<a class="headerlink" href="#initializing-the-library" title="Permalink to this headline">¶</a></h2>
<p>There is a set of core services that the library needs access to while
it is performing requests. To ensure these are set up, you must create
an object of type</p>
<dl class="class">
<dt id="_CPPv318LibraryInitializer">
<span id="_CPPv218LibraryInitializer"></span><span id="LibraryInitializer"></span><em class="property">class </em><code class="descname">LibraryInitializer</code><a class="headerlink" href="#_CPPv318LibraryInitializer" title="Permalink to this definition">¶</a><br /></dt>

<p>prior to making any other library calls. Typically this will be named
something like <code class="docutils literal notranslate"><span class="pre">init</span></code> or <code class="docutils literal notranslate"><span class="pre">botan_init</span></code>. The object lifetime must
exceed that of all other Botan objects your application creates; for
this reason the best place to create the <code class="docutils literal notranslate"><span class="pre">LibraryInitializer</span></code> is at
the start of your <code class="docutils literal notranslate"><span class="pre">main</span></code> function, since this guarantees that it
will be created first and destroyed last (via standard C++ RAII
rules). The initializer does things like setting up the memory
allocation system and algorithm lookup tables, finding out if there is
a high resolution timer available to use, and similar such
matters. With no arguments, the library is initialized with various
default settings. So (unless you are writing threaded code; see
below), all you need is:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">Botan</span><span class="o">::</span><span class="n">LibraryInitializer</span> <span class="n">init</span><span class="p">;</span>
<p>at the start of your <code class="docutils literal notranslate"><span class="pre">main</span></code>.</p>
<p>The constructor takes an optional string that specifies arguments.
Currently the only possible argument is “thread_safe”, which must have
an boolean argument (for instance “thread_safe=false” or
“thread_safe=true”). If “thread_safe” is specified as true the library
will attempt to register a mutex type to properly guard access to
shared resources. However these locks do not protect individual Botan
objects: explicit locking must be used if you wish to share a single
object between threads.</p>
<p>If you do not create a <code class="docutils literal notranslate"><span class="pre">LibraryInitializer</span></code> object, all library
operations will fail, because it will be unable to do basic things
like allocate memory or get random bits. You should never create more
than one <code class="docutils literal notranslate"><span class="pre">LibraryInitializer</span></code>.</p>
<div class="section" id="pitfalls">
<h2>Pitfalls<a class="headerlink" href="#pitfalls" title="Permalink to this headline">¶</a></h2>
<p>There are a few things to watch out for to prevent problems when using
the library.</p>
<p>Never allocate any kind of Botan object globally. The problem with
doing this is that the constructor for such an object will be called
before the library is initialized. Many Botan objects will, in their
constructor, make one or more calls into the library global state
object. Access to this object is checked, so an exception should be
thrown (rather than a memory access violation or undetected
uninitialized object access). A rough equivalent that will work is to
keep a global pointer to the object, initializing it after creating
your <code class="docutils literal notranslate"><span class="pre">LibraryInitializer</span></code>. Merely making the
<code class="docutils literal notranslate"><span class="pre">LibraryInitializer</span></code> also global will probably not help, because
C++ does not make very strong guarantees about the order that such
objects will be created.</p>
<p>The same rule applies for making sure the destructors of all your
Botan objects are called before the <code class="docutils literal notranslate"><span class="pre">LibraryInitializer</span></code> is
destroyed. This implies you can’t have static variables that are Botan
objects inside functions or classes; in many C++ runtimes, these
objects will be destroyed after main has returned.</p>
<p>The memory object classes (<code class="docutils literal notranslate"><span class="pre">MemoryRegion</span></code>, <code class="docutils literal notranslate"><span class="pre">MemoryVector</span></code>,
<code class="docutils literal notranslate"><span class="pre">SecureVector</span></code>) are extremely primitive, and meant only for
secure storage of potentially sensitive data like keys. They do not
meet the requirements for an STL container object and you should not
try to use them with STL algorithms. For a general-purpose container,
use <code class="docutils literal notranslate"><span class="pre">std::vector</span></code>.</p>
<p>Use a <code class="docutils literal notranslate"><span class="pre">try</span></code>/<code class="docutils literal notranslate"><span class="pre">catch</span></code> block inside your <code class="docutils literal notranslate"><span class="pre">main</span></code> function, and catch
any <code class="docutils literal notranslate"><span class="pre">std::exception</span></code> throws (remember to catch by reference, as
<code class="docutils literal notranslate"><span class="pre">std::exception::what</span></code> is polymorphic):</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
   <span class="p">{</span>
   <span class="k">try</span>
      <span class="p">{</span>
      <span class="n">LibraryInitializer</span> <span class="n">init</span><span class="p">;</span>

      <span class="c1">// ...</span>
      <span class="p">}</span>
   <span class="k">catch</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">exception</span><span class="o">&amp;</span> <span class="n">e</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
      <span class="p">}</span>
   <span class="p">}</span>
<p>This is not strictly required, but if you don’t, and Botan throws an
exception, the runtime will call <code class="docutils literal notranslate"><span class="pre">std::terminate</span></code>, which usually
calls <code class="docutils literal notranslate"><span class="pre">abort</span></code> or something like it, leaving you (or worse, a user of
your application) wondering what went wrong.</p>

        <div class="sidebar">
          <h3>Table of Contents</h3>
          <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="index.html">Welcome</a></li>
<li class="toctree-l1"><a class="reference internal" href="reading.html">Recommended Reading</a></li>
<li class="toctree-l1"><a class="reference internal" href="building.html">Building The Library</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Getting Started</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#initializing-the-library">Initializing the Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="#pitfalls">Pitfalls</a></li>
<li class="toctree-l1"><a class="reference internal" href="filters.html">Information Flow: Pipes and Filters</a></li>
<li class="toctree-l1"><a class="reference internal" href="pubkey.html">Public Key Cryptography</a></li>
<li class="toctree-l1"><a class="reference internal" href="x509.html">Certificate Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="ssl.html">SSL and TLS</a></li>
<li class="toctree-l1"><a class="reference internal" href="bigint.html">BigInt</a></li>
<li class="toctree-l1"><a class="reference internal" href="lowlevel.html">The Low-Level Interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="secmem.html">Secure Memory Containers</a></li>
<li class="toctree-l1"><a class="reference internal" href="kdf.html">Key Derivation Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="pbkdf.html">PBKDF Algorithms</a></li>
<li class="toctree-l1"><a class="reference internal" href="passhash.html">Password Hashing</a></li>
<li class="toctree-l1"><a class="reference internal" href="rng.html">Random Number Generators</a></li>
<li class="toctree-l1"><a class="reference internal" href="fpe.html">Format Preserving Encryption</a></li>
<li class="toctree-l1"><a class="reference internal" href="python.html">Python Binding</a></li>

          <div role="search">
            <h3 style="margin-top: 1.5em;">Search</h3>
            <form class="search" action="search.html" method="get">
                <input type="text" name="q" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
        <div class="clearer"></div>

    <div class="footer-wrapper">
      <div class="footer">
        <div class="left">
          <div role="navigation" aria-label="related navigaton">
            <a href="building.html" title="Building The Library"
              accesskey="P">previous</a> |
            <a href="filters.html" title="Information Flow: Pipes and Filters"
              accesskey="N">next</a> |
            <a href="genindex.html" title="General Index"
          <div role="note" aria-label="source link">
              <a href="_sources/firststep.txt"
                rel="nofollow">Show Source</a>

        <div class="right">
    <div class="footer" role="contentinfo">
        &#169; Copyright 2000-2011, Jack Lloyd.
      Created using <a href="">Sphinx</a> 1.8.3.
        <div class="clearer"></div>