Sophie

Sophie

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

botan-doc-1.10.17-7.mga7.noarch.rpm


<!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="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Python Binding &#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="Release Notes" href="log.html" />
    <link rel="prev" title="Format Preserving Encryption" href="fpe.html" /> 
  </head><body>
    <div class="header-wrapper">
      <div class="header">
        <h1>Botan</h1>
      </div>
    </div>

    <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="python-binding">
<h1>Python Binding<a class="headerlink" href="#python-binding" title="Permalink to this headline">ΒΆ</a></h1>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The Python binding should be considered alpha software, and the
interfaces may change in the future.</p>
</div>
<p>Botan includes a binding for Python, implemented using Boost.Python.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/python</span>

<span class="kn">import</span> <span class="nn">botan</span>

<span class="k">def</span> <span class="nf">make_into_c_array</span><span class="p">(</span><span class="n">ber</span><span class="p">):</span>
    <span class="n">output</span> <span class="o">=</span> <span class="s1">&#39;static unsigned char key_data[</span><span class="si">%d</span><span class="s1">] = {</span><span class="se">\n\t</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ber</span><span class="p">))</span>

    <span class="k">for</span> <span class="p">(</span><span class="n">idx</span><span class="p">,</span><span class="n">c</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ber</span><span class="p">)),</span> <span class="n">ber</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">idx</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">idx</span> <span class="o">%</span> <span class="mi">8</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">output</span> <span class="o">+=</span> <span class="s2">&quot;</span><span class="se">\n\t</span><span class="s2">&quot;</span>
        <span class="n">output</span> <span class="o">+=</span> <span class="s2">&quot;0x</span><span class="si">%s</span><span class="s2">, &quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;hex&#39;</span><span class="p">))</span>

    <span class="n">output</span> <span class="o">+=</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">};</span><span class="se">\n</span><span class="s2">&quot;</span>

    <span class="k">return</span> <span class="n">output</span>

<span class="n">rng</span> <span class="o">=</span> <span class="n">botan</span><span class="o">.</span><span class="n">RandomNumberGenerator</span><span class="p">()</span>

<span class="n">rsa_priv</span> <span class="o">=</span> <span class="n">botan</span><span class="o">.</span><span class="n">RSA_PrivateKey</span><span class="p">(</span><span class="mi">768</span><span class="p">,</span> <span class="n">rng</span><span class="p">)</span>

<span class="k">print</span> <span class="n">rsa_priv</span><span class="o">.</span><span class="n">to_string</span><span class="p">()</span>
<span class="k">print</span> <span class="nb">int</span><span class="p">(</span><span class="n">rsa_priv</span><span class="o">.</span><span class="n">get_N</span><span class="p">())</span>
<span class="k">print</span> <span class="nb">int</span><span class="p">(</span><span class="n">rsa_priv</span><span class="o">.</span><span class="n">get_E</span><span class="p">())</span>

<span class="n">rsa_pub</span> <span class="o">=</span> <span class="n">botan</span><span class="o">.</span><span class="n">RSA_PublicKey</span><span class="p">(</span><span class="n">rsa_priv</span><span class="p">)</span>

<span class="k">print</span> <span class="n">make_into_c_array</span><span class="p">(</span><span class="n">rsa_pub</span><span class="o">.</span><span class="n">to_ber</span><span class="p">())</span>
<span class="c1">#print make_into_c_array(rsa_priv.to_ber())</span>

<span class="n">key</span> <span class="o">=</span> <span class="n">rng</span><span class="o">.</span><span class="n">gen_random</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>

<span class="n">ciphertext</span> <span class="o">=</span> <span class="n">rsa_pub</span><span class="o">.</span><span class="n">encrypt</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="s1">&#39;EME1(SHA-1)&#39;</span><span class="p">,</span> <span class="n">rng</span><span class="p">)</span>

<span class="k">print</span> <span class="n">ciphertext</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;hex&#39;</span><span class="p">)</span>

<span class="n">plaintext</span> <span class="o">=</span> <span class="n">rsa_priv</span><span class="o">.</span><span class="n">decrypt</span><span class="p">(</span><span class="n">ciphertext</span><span class="p">,</span> <span class="s1">&#39;EME1(SHA-1)&#39;</span><span class="p">)</span>

<span class="k">print</span> <span class="n">plaintext</span> <span class="o">==</span> <span class="n">key</span>

<span class="n">signature</span> <span class="o">=</span> <span class="n">rsa_priv</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="s1">&#39;EMSA4(SHA-256)&#39;</span><span class="p">,</span> <span class="n">rng</span><span class="p">)</span>

<span class="k">print</span> <span class="n">rsa_pub</span><span class="o">.</span><span class="n">verify</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">signature</span><span class="p">,</span>  <span class="s1">&#39;EMSA4(SHA-256)&#39;</span><span class="p">)</span>

<span class="c1"># Corrupt the signature, make sure it doesn&#39;t verify</span>
<span class="n">signature</span> <span class="o">=</span> <span class="n">signature</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">signature</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s1">&#39;0&#39;</span><span class="p">)</span>

<span class="k">print</span> <span class="n">rsa_pub</span><span class="o">.</span><span class="n">verify</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">signature</span><span class="p">,</span>  <span class="s1">&#39;EMSA4(SHA-256)&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
        </div>
        <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"><a class="reference internal" href="firststep.html">Getting Started</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 current"><a class="current reference internal" href="#">Python Binding</a></li>
</ul>

          <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" />
            </form>
          </div>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

    <div class="footer-wrapper">
      <div class="footer">
        <div class="left">
          <div role="navigation" aria-label="related navigaton">
            <a href="fpe.html" title="Format Preserving Encryption"
              accesskey="P">previous</a> |
            <a href="log.html" title="Release Notes"
              accesskey="N">next</a> |
            <a href="genindex.html" title="General Index"
              accesskey="I">index</a>
          </div>
          <div role="note" aria-label="source link">
              <br/>
              <a href="_sources/python.txt"
                rel="nofollow">Show Source</a>
          </div>
        </div>

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

  </body>
</html>