Package Crypto :: Package Util :: Module number
[frames] | no frames]

Module number

Functions
 
size(N)
size(N:long) : int Returns the size of the number N in bits.
 
getRandomNumber(N, randfunc=None)
Deprecated. Use getRandomInteger or getRandomNBitInteger instead.
 
getRandomInteger(N, randfunc=None)
getRandomInteger(N:int, randfunc:callable):long Return a random number with at most N bits.
 
getRandomRange(a, b, randfunc=None)
getRandomRange(a:int, b:int, randfunc:callable):long Return a random number n so that a <= n < b.
 
getRandomNBitInteger(N, randfunc=None)
getRandomInteger(N:int, randfunc:callable):long Return a random number with exactly N-bits, i.e. a random number between 2**(N-1) and (2**N)-1.
 
GCD(x, y)
GCD(x:long, y:long): long Return the GCD of x and y.
 
inverse(u, v)
inverse(u:long, v:long):long Return the inverse of u mod v.
 
getPrime(N, randfunc=None)
getPrime(N:int, randfunc:callable):long Return a random N-bit prime number.
 
getStrongPrime(N, e=0, false_positive_prob=1e-06, randfunc=None)
getStrongPrime(N:int, e:int, false_positive_prob:float, randfunc:callable):long Return a random strong N-bit prime number. In this context p is a strong prime if p-1 and p+1 have at least one large prime factor. N should be a multiple of 128 and > 512.
 
isPrime(N, false_positive_prob=1e-06, randfunc=None)
isPrime(N:long, false_positive_prob:float, randfunc:callable):bool Return true if N is prime.
 
long_to_bytes(n, blocksize=0)
long_to_bytes(n:long, blocksize:int) : string Convert a long integer to a byte string.
 
bytes_to_long(s)
bytes_to_long(string) : long Convert a byte string to a long integer.
 
long2str(n, blocksize=0)
 
str2long(s)
Variables
  __revision__ = '$Id$'
  sieve_base = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, ...
  __package__ = 'Crypto.Util'
Function Details

getRandomInteger(N, randfunc=None)

 

getRandomInteger(N:int, randfunc:callable):long Return a random number with at most N bits.

If randfunc is omitted, then Random.new().read is used.

This function is for internal use only and may be renamed or removed in the future.

getRandomRange(a, b, randfunc=None)

 

getRandomRange(a:int, b:int, randfunc:callable):long Return a random number n so that a <= n < b.

If randfunc is omitted, then Random.new().read is used.

This function is for internal use only and may be renamed or removed in the future.

getRandomNBitInteger(N, randfunc=None)

 

getRandomInteger(N:int, randfunc:callable):long Return a random number with exactly N-bits, i.e. a random number between 2**(N-1) and (2**N)-1.

If randfunc is omitted, then Random.new().read is used.

This function is for internal use only and may be renamed or removed in the future.

getPrime(N, randfunc=None)

 

getPrime(N:int, randfunc:callable):long Return a random N-bit prime number.

If randfunc is omitted, then Random.new().read is used.

getStrongPrime(N, e=0, false_positive_prob=1e-06, randfunc=None)

 

getStrongPrime(N:int, e:int, false_positive_prob:float, randfunc:callable):long Return a random strong N-bit prime number. In this context p is a strong prime if p-1 and p+1 have at least one large prime factor. N should be a multiple of 128 and > 512.

If e is provided the returned prime p-1 will be coprime to e and thus suitable for RSA where e is the public exponent.

The optional false_positive_prob is the statistical probability that true is returned even though it is not (pseudo-prime). It defaults to 1e-6 (less than 1:1000000). Note that the real probability of a false-positive is far less. This is just the mathematically provable limit.

randfunc should take a single int parameter and return that many random bytes as a string. If randfunc is omitted, then Random.new().read is used.

isPrime(N, false_positive_prob=1e-06, randfunc=None)

 

isPrime(N:long, false_positive_prob:float, randfunc:callable):bool Return true if N is prime.

The optional false_positive_prob is the statistical probability that true is returned even though it is not (pseudo-prime). It defaults to 1e-6 (less than 1:1000000). Note that the real probability of a false-positive is far less. This is just the mathematically provable limit.

If randfunc is omitted, then Random.new().read is used.

long_to_bytes(n, blocksize=0)

 

long_to_bytes(n:long, blocksize:int) : string Convert a long integer to a byte string.

If optional blocksize is given and greater than zero, pad the front of the byte string with binary zeros so that the length is a multiple of blocksize.

bytes_to_long(s)

 

bytes_to_long(string) : long Convert a byte string to a long integer.

This is (essentially) the inverse of long_to_bytes().


Variables Details

sieve_base

Value:
(2,
 3,
 5,
 7,
 11,
 13,
 17,
 19,
...