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 / tlslite / utils / cryptomath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  11.1 KB  |  357 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''cryptomath module
  5.  
  6. This module has basic math/crypto code.'''
  7. import os
  8. import math
  9. import base64
  10. import binascii
  11. import sha
  12. from compat import *
  13.  
  14. try:
  15.     from M2Crypto import m2
  16.     m2cryptoLoaded = True
  17. except ImportError:
  18.     m2cryptoLoaded = False
  19.  
  20.  
  21. try:
  22.     import cryptlib_py
  23.     
  24.     try:
  25.         cryptlib_py.cryptInit()
  26.     except cryptlib_py.CryptException:
  27.         e = None
  28.         if e[0] != cryptlib_py.CRYPT_ERROR_INITED:
  29.             raise 
  30.         e[0] != cryptlib_py.CRYPT_ERROR_INITED
  31.  
  32.     cryptlibpyLoaded = True
  33. except ImportError:
  34.     cryptlibpyLoaded = False
  35.  
  36.  
  37. try:
  38.     import gmpy
  39.     gmpyLoaded = True
  40. except ImportError:
  41.     gmpyLoaded = False
  42.  
  43.  
  44. try:
  45.     import Crypto.Cipher.AES as Crypto
  46.     pycryptoLoaded = True
  47. except ImportError:
  48.     pycryptoLoaded = False
  49.  
  50.  
  51. try:
  52.     os.urandom(1)
  53.     
  54.     def getRandomBytes(howMany):
  55.         return stringToBytes(os.urandom(howMany))
  56.  
  57.     prngName = 'os.urandom'
  58. except:
  59.     pass
  60.  
  61.  
  62. def bytesToNumber(bytes):
  63.     total = 0x0L
  64.     multiplier = 0x1L
  65.     for count in range(len(bytes) - 1, -1, -1):
  66.         byte = bytes[count]
  67.         total += multiplier * byte
  68.         multiplier *= 256
  69.     
  70.     return total
  71.  
  72.  
  73. def numberToBytes(n):
  74.     howManyBytes = numBytes(n)
  75.     bytes = createByteArrayZeros(howManyBytes)
  76.     for count in range(howManyBytes - 1, -1, -1):
  77.         bytes[count] = int(n % 256)
  78.         n >>= 8
  79.     
  80.     return bytes
  81.  
  82.  
  83. def bytesToBase64(bytes):
  84.     s = bytesToString(bytes)
  85.     return stringToBase64(s)
  86.  
  87.  
  88. def base64ToBytes(s):
  89.     s = base64ToString(s)
  90.     return stringToBytes(s)
  91.  
  92.  
  93. def numberToBase64(n):
  94.     bytes = numberToBytes(n)
  95.     return bytesToBase64(bytes)
  96.  
  97.  
  98. def base64ToNumber(s):
  99.     bytes = base64ToBytes(s)
  100.     return bytesToNumber(bytes)
  101.  
  102.  
  103. def stringToNumber(s):
  104.     bytes = stringToBytes(s)
  105.     return bytesToNumber(bytes)
  106.  
  107.  
  108. def numberToString(s):
  109.     bytes = numberToBytes(s)
  110.     return bytesToString(bytes)
  111.  
  112.  
  113. def base64ToString(s):
  114.     
  115.     try:
  116.         return base64.decodestring(s)
  117.     except binascii.Error:
  118.         e = None
  119.         raise SyntaxError(e)
  120.     except binascii.Incomplete:
  121.         e = None
  122.         raise SyntaxError(e)
  123.  
  124.  
  125.  
  126. def stringToBase64(s):
  127.     return base64.encodestring(s).replace('\n', '')
  128.  
  129.  
  130. def mpiToNumber(mpi):
  131.     if ord(mpi[4]) & 128 != 0:
  132.         raise AssertionError()
  133.     ord(mpi[4]) & 128 != 0
  134.     bytes = stringToBytes(mpi[4:])
  135.     return bytesToNumber(bytes)
  136.  
  137.  
  138. def numberToMPI(n):
  139.     bytes = numberToBytes(n)
  140.     ext = 0
  141.     if numBits(n) & 7 == 0:
  142.         ext = 1
  143.     
  144.     length = numBytes(n) + ext
  145.     bytes = concatArrays(createByteArrayZeros(4 + ext), bytes)
  146.     bytes[0] = length >> 24 & 255
  147.     bytes[1] = length >> 16 & 255
  148.     bytes[2] = length >> 8 & 255
  149.     bytes[3] = length & 255
  150.     return bytesToString(bytes)
  151.  
  152.  
  153. def numBytes(n):
  154.     if n == 0:
  155.         return 0
  156.     bits = numBits(n)
  157.     return int(math.ceil(bits / 8))
  158.  
  159.  
  160. def hashAndBase64(s):
  161.     return stringToBase64(sha.sha(s).digest())
  162.  
  163.  
  164. def getBase64Nonce(numChars = 22):
  165.     bytes = getRandomBytes(numChars)
  166.     bytesStr = []([ chr(b) for b in bytes ])
  167.     return stringToBase64(bytesStr)[:numChars]
  168.  
  169.  
  170. def getRandomNumber(low, high):
  171.     if low >= high:
  172.         raise AssertionError()
  173.     low >= high
  174.     howManyBits = numBits(high)
  175.     howManyBytes = numBytes(high)
  176.     lastBits = howManyBits % 8
  177.     while None:
  178.         bytes = getRandomBytes(howManyBytes)
  179.         if lastBits:
  180.             bytes[0] = bytes[0] % (1 << lastBits)
  181.         
  182.         n = bytesToNumber(bytes)
  183.         if n >= low and n < high:
  184.             return n
  185.         continue
  186.         return None
  187.  
  188.  
  189. def gcd(a, b):
  190.     a = max(a, b)
  191.     b = min(a, b)
  192.     while b:
  193.         a = b
  194.         b = a % b
  195.     return a
  196.  
  197.  
  198. def lcm(a, b):
  199.     return a * b / gcd(a, b)
  200.  
  201.  
  202. def invMod(a, b):
  203.     c = a
  204.     d = b
  205.     (uc, ud) = (1, 0)
  206.     while c != 0:
  207.         q = d / c
  208.         c = d - q * c
  209.         d = c
  210.         uc = ud - q * uc
  211.         ud = uc
  212.     if d == 1:
  213.         return ud % b
  214.     return 0
  215.  
  216. if gmpyLoaded:
  217.     
  218.     def powMod(base, power, modulus):
  219.         base = gmpy.mpz(base)
  220.         power = gmpy.mpz(power)
  221.         modulus = gmpy.mpz(modulus)
  222.         result = pow(base, power, modulus)
  223.         return long(result)
  224.  
  225. else:
  226.     
  227.     def powMod(base, power, modulus):
  228.         nBitScan = 5
  229.         negativeResult = False
  230.         if power < 0:
  231.             power *= -1
  232.             negativeResult = True
  233.         
  234.         exp2 = 2 ** nBitScan
  235.         mask = exp2 - 1
  236.         nibbles = None
  237.         while power:
  238.             nibbles = (int(power & mask), nibbles)
  239.             power = power >> nBitScan
  240.         lowPowers = [
  241.             1]
  242.         for i in xrange(1, exp2):
  243.             lowPowers.append(lowPowers[i - 1] * base % modulus)
  244.         
  245.         (nib, nibbles) = nibbles
  246.         prod = lowPowers[nib]
  247.         while nibbles:
  248.             (nib, nibbles) = nibbles
  249.             for i in xrange(nBitScan):
  250.                 prod = prod * prod % modulus
  251.             
  252.             if nib:
  253.                 prod = prod * lowPowers[nib] % modulus
  254.                 continue
  255.         if negativeResult:
  256.             prodInv = invMod(prod, modulus)
  257.             if prod * prodInv % modulus != 1:
  258.                 raise AssertionError()
  259.             prod * prodInv % modulus != 1
  260.             return prodInv
  261.         return prod
  262.  
  263.  
  264. def makeSieve(n):
  265.     sieve = range(n)
  266.     for count in range(2, int(math.sqrt(n))):
  267.         if sieve[count] == 0:
  268.             continue
  269.         
  270.         x = sieve[count] * 2
  271.         while x < len(sieve):
  272.             sieve[x] = 0
  273.             x += sieve[count]
  274.     
  275.     sieve = _[1]
  276.     return sieve
  277.  
  278. sieve = makeSieve(1000)
  279.  
  280. def isPrime(n, iterations = 5, display = False):
  281.     for x in sieve:
  282.         if x >= n:
  283.             return True
  284.         if n % x == 0:
  285.             return False
  286.     
  287.     s = n - 1
  288.     t = 0
  289.     while s % 2 == 0:
  290.         s = s / 2
  291.         t = t + 1
  292.         continue
  293.         None if display else x >= n
  294.     a = 2
  295.     for count in range(iterations):
  296.         v = powMod(a, s, n)
  297.         if v == 1:
  298.             continue
  299.         
  300.         i = 0
  301.         while v != n - 1:
  302.             if i == t - 1:
  303.                 return False
  304.             v = powMod(v, 2, n)
  305.             i = i + 1
  306.             continue
  307.             i == t - 1
  308.         a = getRandomNumber(2, n)
  309.     
  310.     return True
  311.  
  312.  
  313. def getRandomPrime(bits, display = False):
  314.     if bits < 10:
  315.         raise AssertionError()
  316.     bits < 10
  317.     low = 0x2L ** (bits - 1) * 3 / 2
  318.     high = 0x2L ** bits - 30
  319.     p = getRandomNumber(low, high)
  320.     p += 29 - p % 30
  321.     while display:
  322.         print '.',
  323.     p += 30
  324.     if p >= high:
  325.         p = getRandomNumber(low, high)
  326.         p += 29 - p % 30
  327.     
  328.     if isPrime(p, display = display):
  329.         return p
  330.     continue
  331.  
  332.  
  333. def getRandomSafePrime(bits, display = False):
  334.     if bits < 10:
  335.         raise AssertionError()
  336.     bits < 10
  337.     low = 2 ** (bits - 2) * 3 / 2
  338.     high = 2 ** (bits - 1) - 30
  339.     q = getRandomNumber(low, high)
  340.     q += 29 - q % 30
  341.     while display:
  342.         print '.',
  343.     q += 30
  344.     if q >= high:
  345.         q = getRandomNumber(low, high)
  346.         q += 29 - q % 30
  347.     
  348.     if isPrime(q, 0, display = display):
  349.         p = 2 * q + 1
  350.         if isPrime(p, display = display):
  351.             if isPrime(q, display = display):
  352.                 return p
  353.         
  354.     isPrime(p, display = display)
  355.     continue
  356.  
  357.