



'''simple docstring'''




import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

UpperCamelCase__ :			str												=				3


def        lowerCAmelCase_     (			_lowerCamelCase:     Union[str, Any]      ):
		print("""Generating primitive root of p"""      )
		while True:
				__SCREAMING_SNAKE_CASE :	Any					 =	random.randrange(3 ,   lowerCamelCase_      )
				if pow(lowerCamelCase_ ,   2 ,   lowerCamelCase_      ) == 1:
						continue
				if pow(lowerCamelCase_ ,   lowerCamelCase_ ,   lowerCamelCase_      ) == 1:
						continue
				return g


def        lowerCAmelCase_     (			_lowerCamelCase:     Union[str, Any]      ):
		print("""Generating prime p..."""      )
		__SCREAMING_SNAKE_CASE :	List[str]					 =	rabin_miller.generate_large_prime(lowerCamelCase_      )  # select large prime number.
		__SCREAMING_SNAKE_CASE :	Any					 =	primitive_root(lowerCamelCase_      )  # one primitive root on modulo p.
		__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	random.randrange(3 ,   lowerCamelCase_      )  # private_key -> have to be greater than 2 for safety.
		__SCREAMING_SNAKE_CASE :	Dict					 =	cryptomath.find_mod_inverse(pow(lowerCamelCase_ ,   lowerCamelCase_ ,   lowerCamelCase_      ) ,   lowerCamelCase_      )

		__SCREAMING_SNAKE_CASE :	List[Any]					 =	(key_size, e_a, e_a, p)
		__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	(key_size, d)

		return public_key, private_key


def        lowerCAmelCase_     (			_lowerCamelCase:     Optional[Any] ,   _lowerCamelCase:     Tuple      ):
		if os.path.exists(F"{name}_pubkey.txt"      ) or os.path.exists(F"{name}_privkey.txt"      ):
				print("""\nWARNING:"""      )
				print(
				    F"\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n"
				    """Use a different name or delete these files and re-run this program."""      )
				sys.exit()

		__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	generate_key(lowerCamelCase_      )
		print(F"\nWriting public key to file {name}_pubkey.txt..."      )
		with open(F"{name}_pubkey.txt" ,   """w"""      ) as fo:
				fo.write(F"{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}"      )

		print(F"Writing private key to file {name}_privkey.txt..."      )
		with open(F"{name}_privkey.txt" ,   """w"""      ) as fo:
				fo.write(F"{private_key[0]},{private_key[1]}"      )


def        lowerCAmelCase_     (			):
		print("""Making key files..."""      )
		make_key_files("""elgamal""" ,   20_48      )
		print("""Key files generation successful"""      )


if __name__ == "__main__":
						main()







'''simple docstring'''




from unittest.mock import Mock, patch

from file_transfer.send_file import send_file


@patch("""socket.socket"""      )
@patch("""builtins.open"""      )
def        lowerCAmelCase_     (			_lowerCamelCase:     Dict ,   _lowerCamelCase:     List[Any]      ):
		# ===== initialization =====
		__SCREAMING_SNAKE_CASE :	Optional[int]					 =	Mock()
		__SCREAMING_SNAKE_CASE :	Optional[int]					 =	conn, Mock()
		__SCREAMING_SNAKE_CASE :	str					 =	iter([1, None]      )
		__SCREAMING_SNAKE_CASE :	Any					 =	lambda _lowerCamelCase      : next(_lowerCamelCase      )

		# ===== invoke =====
		send_file(filename="""mytext.txt""" ,   testing=_lowerCamelCase      )

		# ===== ensurance =====
		sock.assert_called_once()
		sock.return_value.bind.assert_called_once()
		sock.return_value.listen.assert_called_once()
		sock.return_value.accept.assert_called_once()
		conn.recv.assert_called_once()

		file.return_value.__enter__.assert_called_once()
		file.return_value.__enter__.return_value.read.assert_called()

		conn.send.assert_called_once()
		conn.close.assert_called_once()
		sock.return_value.shutdown.assert_called_once()
		sock.return_value.close.assert_called_once()