home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / Crypto / PublicKey / qNEW.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.4 KB  |  171 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __revision__ = '$Id: qNEW.py,v 1.8 2003/04/04 15:13:35 akuchling Exp $'
  5. from Crypto.PublicKey import pubkey
  6. from Crypto.Util.number import *
  7. from Crypto.Hash import SHA
  8.  
  9. class error(Exception):
  10.     pass
  11.  
  12. HASHBITS = 160
  13.  
  14. def generate(bits, randfunc, progress_func = None):
  15.     """generate(bits:int, randfunc:callable, progress_func:callable)
  16.  
  17.     Generate a qNEW key of length 'bits', using 'randfunc' to get
  18.     random data and 'progress_func', if present, to display
  19.     the progress of the key generation.
  20.     """
  21.     obj = qNEWobj()
  22.     if progress_func:
  23.         progress_func('p,q\n')
  24.     
  25.     while None:
  26.         obj.q = getPrime(160, randfunc)
  27.         obj.seed = S = long_to_bytes(obj.q)
  28.         C = 0
  29.         N = 2
  30.         V = { }
  31.         n = (bits - 1) / HASHBITS
  32.         b = (bits - 1) % HASHBITS
  33.         powb = 0x2L << b
  34.         powL1 = pow(long(2), bits - 1)
  35.         while C < 4096:
  36.             for k in range(0, n + 1):
  37.                 V[k] = bytes_to_long(SHA.new(S + str(N) + str(k)).digest())
  38.             
  39.             p = V[n] % powb
  40.             for k in range(n - 1, -1, -1):
  41.                 p = (p << long(HASHBITS)) + V[k]
  42.             
  43.             p = p + powL1
  44.             p = p - p % 2 * obj.q - 1
  45.             if powL1 <= p and isPrime(p):
  46.                 break
  47.             
  48.             C = C + 1
  49.             N = N + n + 1
  50.         if C < 4096:
  51.             break
  52.         
  53.         if progress_func:
  54.             progress_func('4096 values of p tried\n')
  55.             continue
  56.         continue
  57.         obj.p = p
  58.         power = (p - 1) / obj.q
  59.         if progress_func:
  60.             progress_func('h,g\n')
  61.         
  62.     while None:
  63.         h = bytes_to_long(randfunc(bits)) % (p - 1)
  64.         g = pow(h, power, p)
  65.         if h < h:
  66.             pass
  67.         elif h < p - 1 and g > 1:
  68.             break
  69.             continue
  70.         continue
  71.         obj.g = g
  72.         if progress_func:
  73.             progress_func('x,y\n')
  74.         
  75.     while None:
  76.         x = bytes_to_long(randfunc(20))
  77.         if x < x:
  78.             pass
  79.         elif x < obj.q:
  80.             break
  81.             continue
  82.         continue
  83.         obj.x = x
  84.         obj.y = pow(g, x, p)
  85.         return obj
  86.  
  87.  
  88. def construct(tuple):
  89.     '''construct(tuple:(long,long,long,long)|(long,long,long,long,long)
  90.     Construct a qNEW object from a 4- or 5-tuple of numbers.
  91.     '''
  92.     obj = qNEWobj()
  93.     if len(tuple) not in (4, 5):
  94.         raise error, 'argument for construct() wrong length'
  95.     len(tuple) not in (4, 5)
  96.     for i in range(len(tuple)):
  97.         field = obj.keydata[i]
  98.         setattr(obj, field, tuple[i])
  99.     
  100.     return obj
  101.  
  102.  
  103. class qNEWobj(pubkey.pubkey):
  104.     keydata = [
  105.         'p',
  106.         'q',
  107.         'g',
  108.         'y',
  109.         'x']
  110.     
  111.     def _sign(self, M, K = ''):
  112.         if self.q <= K:
  113.             raise error, 'K is greater than q'
  114.         self.q <= K
  115.         if M < 0:
  116.             raise error, 'Illegal value of M (<0)'
  117.         M < 0
  118.         if M >= pow(2, 0xA1L):
  119.             raise error, 'Illegal value of M (too large)'
  120.         M >= pow(2, 0xA1L)
  121.         r = pow(self.g, K, self.p) % self.q
  122.         s = (K - r * M * self.x % self.q) % self.q
  123.         return (r, s)
  124.  
  125.     
  126.     def _verify(self, M, sig):
  127.         (r, s) = sig
  128.         if r <= 0 and r >= self.q and s <= 0 or s >= self.q:
  129.             return 0
  130.         if M < 0:
  131.             raise error, 'Illegal value of M (<0)'
  132.         M < 0
  133.         if M <= 0 or M >= pow(2, 0xA1L):
  134.             return 0
  135.         v1 = pow(self.g, s, self.p)
  136.         v2 = pow(self.y, M * r, self.p)
  137.         v = v1 * v2 % self.p
  138.         v = v % self.q
  139.         if v == r:
  140.             return 1
  141.         return 0
  142.  
  143.     
  144.     def size(self):
  145.         '''Return the maximum number of bits that can be handled by this key.'''
  146.         return 160
  147.  
  148.     
  149.     def has_private(self):
  150.         '''Return a Boolean denoting whether the object contains
  151.         private components.'''
  152.         return hasattr(self, 'x')
  153.  
  154.     
  155.     def can_sign(self):
  156.         '''Return a Boolean value recording whether this algorithm can generate signatures.'''
  157.         return 1
  158.  
  159.     
  160.     def can_encrypt(self):
  161.         '''Return a Boolean value recording whether this algorithm can encrypt data.'''
  162.         return 0
  163.  
  164.     
  165.     def publickey(self):
  166.         '''Return a new key object containing only the public information.'''
  167.         return construct((self.p, self.q, self.g, self.y))
  168.  
  169.  
  170. object = qNEWobj
  171.