summaryrefslogtreecommitdiff
path: root/doc/html/_modules/M2Crypto/BN.html
blob: b8287fb9f51fbdf621eaa50d2d15b58f34a80730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

<!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>M2Crypto.BN &#8212; M2Crypto  documentation</title>
    <link rel="stylesheet" href="../../_static/alabaster.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="stylesheet" href="../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for M2Crypto.BN</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">absolute_import</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">M2Crypto wrapper for OpenSSL BN (BIGNUM) API.</span>

<span class="sd">Copyright (c) 2005 Open Source Applications Foundation. All rights reserved.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">M2Crypto</span> <span class="k">import</span> <span class="n">m2</span><span class="p">,</span> <span class="n">util</span>
<span class="k">if</span> <span class="n">util</span><span class="o">.</span><span class="n">py27plus</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">Optional</span>  <span class="c1"># noqa</span>


<div class="viewcode-block" id="rand"><a class="viewcode-back" href="../../M2Crypto.html#M2Crypto.BN.rand">[docs]</a><span class="k">def</span> <span class="nf">rand</span><span class="p">(</span><span class="n">bits</span><span class="p">,</span> <span class="n">top</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="n">bottom</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="c1"># type: (int, int, int) -&gt; Optional[int]</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generate cryptographically strong random number.</span>

<span class="sd">    :param bits:   Length of random number in bits.</span>
<span class="sd">    :param top:    If -1, the most significant bit can be 0. If 0, the most</span>
<span class="sd">                   significant bit is 1, and if 1, the two most significant</span>
<span class="sd">                   bits will be 1.</span>
<span class="sd">    :param bottom: If bottom is true, the number will be odd.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">m2</span><span class="o">.</span><span class="n">bn_rand</span><span class="p">(</span><span class="n">bits</span><span class="p">,</span> <span class="n">top</span><span class="p">,</span> <span class="n">bottom</span><span class="p">)</span></div>


<div class="viewcode-block" id="rand_range"><a class="viewcode-back" href="../../M2Crypto.html#M2Crypto.BN.rand_range">[docs]</a><span class="k">def</span> <span class="nf">rand_range</span><span class="p">(</span><span class="nb">range</span><span class="p">):</span>
    <span class="c1"># type: (int) -&gt; int</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generate a random number in a range.</span>

<span class="sd">    :param range: Upper limit for range.</span>
<span class="sd">    :return:      A random number in the range [0, range)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">m2</span><span class="o">.</span><span class="n">bn_rand_range</span><span class="p">(</span><span class="nb">range</span><span class="p">)</span></div>


<div class="viewcode-block" id="randfname"><a class="viewcode-back" href="../../M2Crypto.html#M2Crypto.BN.randfname">[docs]</a><span class="k">def</span> <span class="nf">randfname</span><span class="p">(</span><span class="n">length</span><span class="p">):</span>
    <span class="c1"># type: (int) -&gt; str</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a random filename, which is simply a string where all</span>
<span class="sd">    the characters are from the set [a-zA-Z0-9].</span>

<span class="sd">    :param length: Length of filename to return.</span>
<span class="sd">    :return:       random filename string</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">import</span> <span class="nn">warnings</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
        <span class="s2">&quot;Don&#39;t use BN.randfname(), use tempfile methods instead.&quot;</span><span class="p">,</span>
        <span class="ne">DeprecationWarning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
    <span class="n">letters</span> <span class="o">=</span> <span class="s1">&#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890&#39;</span>
    <span class="n">lettersLen</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">letters</span><span class="p">)</span>
    <span class="n">fname</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># type: list</span>
    <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">length</span><span class="p">):</span>
        <span class="n">fname</span> <span class="o">+=</span> <span class="p">[</span><span class="n">letters</span><span class="p">[</span><span class="n">m2</span><span class="o">.</span><span class="n">bn_rand_range</span><span class="p">(</span><span class="n">lettersLen</span><span class="p">)]]</span>

    <span class="k">return</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">fname</span><span class="p">)</span></div>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../index.html">M2Crypto</a></h1>








<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../M2Crypto.html">M2Crypto Package</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../index.html">Documentation overview</a><ul>
  <li><a href="../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <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>
<script type="text/javascript">$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2017, Matej Cepl <mcepl@cepl.eu>.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.5</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
    </div>

    

    
  </body>
</html>