playcrypt.tools module

playcrypt.tools.add_int_to_string(s, num, block_size_bytes)[source]

Adds a number to a string

Parameters
  • s – String of length block_size_bytes

  • num – A number s.t. 0 <= num < block_size_bytes*8

  • block_size_bytes – Length of s in bytes

Returns

A string

playcrypt.tools.bitwise_complement_string(s)[source]

Returns the bitwise complement of s.

Parameters

s – string obejct to complement

Returns

result of bitwuise complement

playcrypt.tools.egcd(a, b)[source]

Computes the extended GCD for (a,b). That is, it computes integers x and y such that ax + by = gcd(a, b) as well as gcd(a, b).

Parameters
  • a – First parameter for GCD.

  • b – Second parameter for GCD.

Returns

playcrypt.tools.exp(a, n, N)[source]
playcrypt.tools.find_generator_Z_N_star(N, pstatements=False)[source]

Returns a generator of Z_N_star.

Parameters

N – N

Returns

The generator.

playcrypt.tools.in_Z_N_star(a, N)[source]

Determines whether a is in Z_n^*.

Parameters
  • a – number to be checked

  • N – N

Returns

Boolean

playcrypt.tools.int_to_string(x, l=None)[source]

Converts a number to a string with length l

Parameters
  • x – A number between 0 and 2 ** l

  • l – The length of the string to be returned

Returns

string

playcrypt.tools.is_prime(n, _precision_for_huge_n=16)[source]
playcrypt.tools.join(s)[source]

Turns an array of strings into one string.

Parameters

s – Array of string to join.

Returns

One string made by connecting all strings in the array.

playcrypt.tools.modinv(a, m)[source]

Computes the modular inverse of a mod m. http://stackoverflow.com/questions/4798654/modular-multiplicative-inverse-function-in-python

Parameters
  • a – a in a^-1 mod m

  • m – m in a^-1 mod m

Returns

a^-1 mod m or None if no inverse exists

playcrypt.tools.prime_between(s, e)[source]
playcrypt.tools.random_Z_N(N)[source]

Returns the a random element in Z_N.

Parameters

N – N

Returns

random element

playcrypt.tools.random_Z_N_star(N)[source]

Returns the a random element in Z_N^*.

Parameters

N – N

Returns

random element

playcrypt.tools.split(s, n=None)[source]

Split a string into portions of length n. If n is not supplied the string is split in half. Some examples:

from playcrypt.tools import split

print split("ABCDEF", 1)
print split("ABCDEF", 2)
print split("ABCDEF")
['A', 'B', 'C', 'D', 'E', 'F']
['AB', 'CD', 'EF']
['ABC', 'DEF']
Parameters
  • s – A string

  • n – the length of string portions

Return s[]

an array of the portions of s

playcrypt.tools.string_to_int(s)[source]

Converts s to an int

Parameters

s – A string

Returns

The integer representation of the string s.

playcrypt.tools.xor_strings(s1, s2)[source]

Returns the bitwise XOR of s1 and s2. If len(s1) != len(s2) the resulting XOR operation will be the size of the bigger string.

Parameters
  • s1 – first string in XOR

  • s2 – second string in XOR

Returns

result of bitwise XOR of s1 and s2.