Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(432)

Side by Side Diff: third_party/google-endpoints/Crypto/PublicKey/_RSA.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #
2 # RSA.py : RSA encryption/decryption
3 #
4 # Part of the Python Cryptography Toolkit
5 #
6 # Written by Andrew Kuchling, Paul Swartz, and others
7 #
8 # ===================================================================
9 # The contents of this file are dedicated to the public domain. To
10 # the extent that dedication to the public domain is not available,
11 # everyone is granted a worldwide, perpetual, royalty-free,
12 # non-exclusive license to exercise all rights associated with the
13 # contents of this file for any purpose whatsoever.
14 # No rights are reserved.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 # SOFTWARE.
24 # ===================================================================
25 #
26
27 __revision__ = "$Id$"
28
29 from Crypto.PublicKey import pubkey
30 from Crypto.Util import number
31
32 def generate_py(bits, randfunc, progress_func=None, e=65537):
33 """generate(bits:int, randfunc:callable, progress_func:callable, e:int)
34
35 Generate an RSA key of length 'bits', public exponent 'e'(which must be
36 odd), using 'randfunc' to get random data and 'progress_func',
37 if present, to display the progress of the key generation.
38 """
39 obj=RSAobj()
40 obj.e = long(e)
41
42 # Generate the prime factors of n
43 if progress_func:
44 progress_func('p,q\n')
45 p = q = 1L
46 while number.size(p*q) < bits:
47 # Note that q might be one bit longer than p if somebody specifies an od d
48 # number of bits for the key. (Why would anyone do that? You don't get
49 # more security.)
50 p = pubkey.getStrongPrime(bits>>1, obj.e, 1e-12, randfunc)
51 q = pubkey.getStrongPrime(bits - (bits>>1), obj.e, 1e-12, randfunc)
52
53 # It's OK for p to be larger than q, but let's be
54 # kind to the function that will invert it for
55 # th calculation of u.
56 if p > q:
57 (p, q)=(q, p)
58 obj.p = p
59 obj.q = q
60
61 if progress_func:
62 progress_func('u\n')
63 obj.u = pubkey.inverse(obj.p, obj.q)
64 obj.n = obj.p*obj.q
65
66 if progress_func:
67 progress_func('d\n')
68 obj.d=pubkey.inverse(obj.e, (obj.p-1)*(obj.q-1))
69
70 assert bits <= 1+obj.size(), "Generated key is too small"
71
72 return obj
73
74 class RSAobj(pubkey.pubkey):
75
76 def size(self):
77 """size() : int
78 Return the maximum number of bits that can be handled by this key.
79 """
80 return number.size(self.n) - 1
81
OLDNEW
« no previous file with comments | « third_party/google-endpoints/Crypto/PublicKey/_DSA.py ('k') | third_party/google-endpoints/Crypto/PublicKey/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698