home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- '''cryptomath module
-
- This module has basic math/crypto code.'''
- import os
- import math
- import base64
- import binascii
- import sha
- from compat import *
-
- try:
- from M2Crypto import m2
- m2cryptoLoaded = True
- except ImportError:
- m2cryptoLoaded = False
-
-
- try:
- import cryptlib_py
-
- try:
- cryptlib_py.cryptInit()
- except cryptlib_py.CryptException:
- e = None
- if e[0] != cryptlib_py.CRYPT_ERROR_INITED:
- raise
- e[0] != cryptlib_py.CRYPT_ERROR_INITED
-
- cryptlibpyLoaded = True
- except ImportError:
- cryptlibpyLoaded = False
-
-
- try:
- import gmpy
- gmpyLoaded = True
- except ImportError:
- gmpyLoaded = False
-
-
- try:
- import Crypto.Cipher.AES as Crypto
- pycryptoLoaded = True
- except ImportError:
- pycryptoLoaded = False
-
-
- try:
- os.urandom(1)
-
- def getRandomBytes(howMany):
- return stringToBytes(os.urandom(howMany))
-
- prngName = 'os.urandom'
- except:
- pass
-
-
- def bytesToNumber(bytes):
- total = 0x0L
- multiplier = 0x1L
- for count in range(len(bytes) - 1, -1, -1):
- byte = bytes[count]
- total += multiplier * byte
- multiplier *= 256
-
- return total
-
-
- def numberToBytes(n):
- howManyBytes = numBytes(n)
- bytes = createByteArrayZeros(howManyBytes)
- for count in range(howManyBytes - 1, -1, -1):
- bytes[count] = int(n % 256)
- n >>= 8
-
- return bytes
-
-
- def bytesToBase64(bytes):
- s = bytesToString(bytes)
- return stringToBase64(s)
-
-
- def base64ToBytes(s):
- s = base64ToString(s)
- return stringToBytes(s)
-
-
- def numberToBase64(n):
- bytes = numberToBytes(n)
- return bytesToBase64(bytes)
-
-
- def base64ToNumber(s):
- bytes = base64ToBytes(s)
- return bytesToNumber(bytes)
-
-
- def stringToNumber(s):
- bytes = stringToBytes(s)
- return bytesToNumber(bytes)
-
-
- def numberToString(s):
- bytes = numberToBytes(s)
- return bytesToString(bytes)
-
-
- def base64ToString(s):
-
- try:
- return base64.decodestring(s)
- except binascii.Error:
- e = None
- raise SyntaxError(e)
- except binascii.Incomplete:
- e = None
- raise SyntaxError(e)
-
-
-
- def stringToBase64(s):
- return base64.encodestring(s).replace('\n', '')
-
-
- def mpiToNumber(mpi):
- if ord(mpi[4]) & 128 != 0:
- raise AssertionError()
- ord(mpi[4]) & 128 != 0
- bytes = stringToBytes(mpi[4:])
- return bytesToNumber(bytes)
-
-
- def numberToMPI(n):
- bytes = numberToBytes(n)
- ext = 0
- if numBits(n) & 7 == 0:
- ext = 1
-
- length = numBytes(n) + ext
- bytes = concatArrays(createByteArrayZeros(4 + ext), bytes)
- bytes[0] = length >> 24 & 255
- bytes[1] = length >> 16 & 255
- bytes[2] = length >> 8 & 255
- bytes[3] = length & 255
- return bytesToString(bytes)
-
-
- def numBytes(n):
- if n == 0:
- return 0
- bits = numBits(n)
- return int(math.ceil(bits / 8))
-
-
- def hashAndBase64(s):
- return stringToBase64(sha.sha(s).digest())
-
-
- def getBase64Nonce(numChars = 22):
- bytes = getRandomBytes(numChars)
- bytesStr = []([ chr(b) for b in bytes ])
- return stringToBase64(bytesStr)[:numChars]
-
-
- def getRandomNumber(low, high):
- if low >= high:
- raise AssertionError()
- low >= high
- howManyBits = numBits(high)
- howManyBytes = numBytes(high)
- lastBits = howManyBits % 8
- while None:
- bytes = getRandomBytes(howManyBytes)
- if lastBits:
- bytes[0] = bytes[0] % (1 << lastBits)
-
- n = bytesToNumber(bytes)
- if n >= low and n < high:
- return n
- continue
- return None
-
-
- def gcd(a, b):
- a = max(a, b)
- b = min(a, b)
- while b:
- a = b
- b = a % b
- return a
-
-
- def lcm(a, b):
- return a * b / gcd(a, b)
-
-
- def invMod(a, b):
- c = a
- d = b
- (uc, ud) = (1, 0)
- while c != 0:
- q = d / c
- c = d - q * c
- d = c
- uc = ud - q * uc
- ud = uc
- if d == 1:
- return ud % b
- return 0
-
- if gmpyLoaded:
-
- def powMod(base, power, modulus):
- base = gmpy.mpz(base)
- power = gmpy.mpz(power)
- modulus = gmpy.mpz(modulus)
- result = pow(base, power, modulus)
- return long(result)
-
- else:
-
- def powMod(base, power, modulus):
- nBitScan = 5
- negativeResult = False
- if power < 0:
- power *= -1
- negativeResult = True
-
- exp2 = 2 ** nBitScan
- mask = exp2 - 1
- nibbles = None
- while power:
- nibbles = (int(power & mask), nibbles)
- power = power >> nBitScan
- lowPowers = [
- 1]
- for i in xrange(1, exp2):
- lowPowers.append(lowPowers[i - 1] * base % modulus)
-
- (nib, nibbles) = nibbles
- prod = lowPowers[nib]
- while nibbles:
- (nib, nibbles) = nibbles
- for i in xrange(nBitScan):
- prod = prod * prod % modulus
-
- if nib:
- prod = prod * lowPowers[nib] % modulus
- continue
- if negativeResult:
- prodInv = invMod(prod, modulus)
- if prod * prodInv % modulus != 1:
- raise AssertionError()
- prod * prodInv % modulus != 1
- return prodInv
- return prod
-
-
- def makeSieve(n):
- sieve = range(n)
- for count in range(2, int(math.sqrt(n))):
- if sieve[count] == 0:
- continue
-
- x = sieve[count] * 2
- while x < len(sieve):
- sieve[x] = 0
- x += sieve[count]
-
- sieve = _[1]
- return sieve
-
- sieve = makeSieve(1000)
-
- def isPrime(n, iterations = 5, display = False):
- for x in sieve:
- if x >= n:
- return True
- if n % x == 0:
- return False
-
- s = n - 1
- t = 0
- while s % 2 == 0:
- s = s / 2
- t = t + 1
- continue
- None if display else x >= n
- a = 2
- for count in range(iterations):
- v = powMod(a, s, n)
- if v == 1:
- continue
-
- i = 0
- while v != n - 1:
- if i == t - 1:
- return False
- v = powMod(v, 2, n)
- i = i + 1
- continue
- i == t - 1
- a = getRandomNumber(2, n)
-
- return True
-
-
- def getRandomPrime(bits, display = False):
- if bits < 10:
- raise AssertionError()
- bits < 10
- low = 0x2L ** (bits - 1) * 3 / 2
- high = 0x2L ** bits - 30
- p = getRandomNumber(low, high)
- p += 29 - p % 30
- while display:
- print '.',
- p += 30
- if p >= high:
- p = getRandomNumber(low, high)
- p += 29 - p % 30
-
- if isPrime(p, display = display):
- return p
- continue
-
-
- def getRandomSafePrime(bits, display = False):
- if bits < 10:
- raise AssertionError()
- bits < 10
- low = 2 ** (bits - 2) * 3 / 2
- high = 2 ** (bits - 1) - 30
- q = getRandomNumber(low, high)
- q += 29 - q % 30
- while display:
- print '.',
- q += 30
- if q >= high:
- q = getRandomNumber(low, high)
- q += 29 - q % 30
-
- if isPrime(q, 0, display = display):
- p = 2 * q + 1
- if isPrime(p, display = display):
- if isPrime(q, display = display):
- return p
-
- isPrime(p, display = display)
- continue
-
-