


import unittest

import numpy as np
import PIL
import torch

from diffusers.image_processor import VaeImageProcessor

class      lowerCamelCase	(  unittest.TestCase     ):

       @property
       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : int	  =      1
        snake_case   : int	  =      3
        snake_case   : Any	  =      8
        snake_case   : Any	  =      8

        snake_case   : str	  =      torch.rand((batch_size, num_channels, height, width)					)

        return sample

       def 				UpperCAmelCase				(			self	,						a__					):






        '''simple docstring'''






        if isinstance(image[0]	,						PIL.Image.Image					):
         return np.stack([np.array(a__					) for i in image]	,						axis=0					)
        elif isinstance(a__	,						torch.Tensor					):
         return image.cpu().numpy().transpose(0	,						2	,						3	,						1					)
        return image

       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : Optional[Any]	  =      VaeImageProcessor(do_resize=a__	,						do_normalize=a__					)

        snake_case   : int	  =      self.dummy_sample
        snake_case   : Union[str, Any]	  =      self.to_np(a__					)

        for output_type in ["pt", "np", "pil"]:
         snake_case   : int	  =      image_processor.postprocess(
             image_processor.preprocess(a__					)	,						output_type=a__	,						)
         snake_case   : str	  =      self.to_np(a__					)
         snake_case   : Tuple	  =      (input_np * 2_55).round() if output_type == '''pil''' else input_np
         assert (
             np.abs(in_np - out_np					).max() < 1e-6
         ), f'''decoded output does not match input for output_type {output_type}'''

       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : int	  =      VaeImageProcessor(do_resize=a__	,						do_normalize=a__					)
        snake_case   : Any	  =      self.dummy_sample.cpu().numpy().transpose(0	,						2	,						3	,						1					)

        for output_type in ["pt", "np", "pil"]:
         snake_case   : Union[str, Any]	  =      image_processor.postprocess(image_processor.preprocess(a__					)	,						output_type=a__					)

         snake_case   : Dict	  =      self.to_np(a__					)
         snake_case   : Tuple	  =      (input_np * 2_55).round() if output_type == '''pil''' else input_np
         assert (
             np.abs(in_np - out_np					).max() < 1e-6
         ), f'''decoded output does not match input for output_type {output_type}'''

       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : Any	  =      VaeImageProcessor(do_resize=a__	,						do_normalize=a__					)

        snake_case   : Dict	  =      self.dummy_sample.cpu().numpy().transpose(0	,						2	,						3	,						1					)
        snake_case   : str	  =      image_processor.numpy_to_pil(a__					)

        for output_type in ["pt", "np", "pil"]:
         snake_case   : Dict	  =      image_processor.postprocess(image_processor.preprocess(a__					)	,						output_type=a__					)
         for i, o in zip(a__	,						a__					):
          snake_case   : Union[str, Any]	  =      np.array(a__					)
          snake_case   : Optional[Any]	  =      self.to_np(a__					) if output_type == '''pil''' else (self.to_np(a__					) * 2_55).round()
          assert (
              np.abs(in_np - out_np					).max() < 1e-6
          ), f'''decoded output does not match input for output_type {output_type}'''

       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : str	  =      VaeImageProcessor(do_resize=a__	,						do_normalize=a__					)

        snake_case   : Any	  =      self.dummy_sample
        snake_case   : Tuple	  =      input_pt_ad.squeeze(0					)

        snake_case   : Any	  =      image_processor.postprocess(
            image_processor.preprocess(a__					)	,						output_type='''np'''	,						)
        snake_case   : str	  =      image_processor.postprocess(
            image_processor.preprocess(a__					)	,						output_type='''np'''	,						)

        snake_case   : Union[str, Any]	  =      self.to_np(self.dummy_sample					)
        snake_case   : int	  =      input_np_ad.squeeze(0					)

        snake_case   : Tuple	  =      image_processor.postprocess(
            image_processor.preprocess(a__					)	,						output_type='''np'''	,						)
        snake_case   : Optional[Any]	  =      image_processor.postprocess(
            image_processor.preprocess(a__					)	,						output_type='''np'''	,						)

        assert np.abs(out_pt_ad - out_pt_ad					).max() < 1e-6
        assert np.abs(out_np_ad - out_np_ad					).max() < 1e-6



       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






        snake_case   : Optional[Any]	  =      VaeImageProcessor(do_resize=a__	,						do_normalize=a__					)

        snake_case   : Dict	  =      self.dummy_sample
        snake_case   : List[Any]	  =      list(a__					)

        snake_case   : str	  =      image_processor.postprocess(
            image_processor.preprocess(a__					)	,						output_type='''np'''	,						)

        snake_case   : Dict	  =      image_processor.postprocess(
            image_processor.preprocess(a__					)	,						output_type='''np'''	,						)

        snake_case   : Any	  =      self.to_np(self.dummy_sample					)
        list(a__					)

        snake_case   : Optional[int]	  =      image_processor.postprocess(
            image_processor.preprocess(a__					)	,						output_type='''np'''	,						)

        snake_case   : Optional[Any]	  =      image_processor.postprocess(
            image_processor.preprocess(a__					)	,						output_type='''np'''	,						)

        assert np.abs(out_pt_ad - out_pt_list					).max() < 1e-6
        assert np.abs(out_np_ad - out_np_list					).max() < 1e-6










import os
import sys

from . import rsa_key_generator as rkg

a				:							str					=			128
a				:							str					=			256






def    _UpperCAmelCase	(   __lowerCamelCase ,     __lowerCamelCase = DEFAULT_BLOCK_SIZE       )		->list[int]:


 '''simple docstring'''
 snake_case   : List[str]	  =      message.encode('''ascii'''       )

 snake_case   : List[Any]	  =      []
 for block_start in range(0 ,     len(__lowerCamelCase       ) ,     __lowerCamelCase       ):
  snake_case   : int	  =      0
  for i in range(__lowerCamelCase ,     min(block_start + block_size ,     len(__lowerCamelCase       )       )       ):
   block_int += message_bytes[i] * (BYTE_SIZE ** (i % block_size))
  block_ints.append(__lowerCamelCase       )
 return block_ints






def    _UpperCAmelCase	(   __lowerCamelCase ,     __lowerCamelCase ,     __lowerCamelCase = DEFAULT_BLOCK_SIZE       )		->str:


 '''simple docstring'''
 snake_case   : list[str]	  =      []
 for block_int in block_ints:
  snake_case   : list[str]	  =      []
  for i in range(block_size - 1 ,     -1 ,     -1       ):
   if len(__lowerCamelCase       ) + i < message_length:
    snake_case   : Any	  =      block_int // (BYTE_SIZE**i)
    snake_case   : Optional[Any]	  =      block_int % (BYTE_SIZE**i)
    block_message.insert(0 ,     chr(__lowerCamelCase       )       )
  message.extend(__lowerCamelCase       )
 return "".join(__lowerCamelCase       )






def    _UpperCAmelCase	(   __lowerCamelCase ,     __lowerCamelCase ,     __lowerCamelCase = DEFAULT_BLOCK_SIZE       )		->list[int]:


 '''simple docstring'''
 snake_case   : str	  =      []
 snake_case  ,					snake_case   : Optional[Any]	  =      key

 for block in get_blocks_from_text(__lowerCamelCase ,     __lowerCamelCase       ):
  encrypted_blocks.append(pow(__lowerCamelCase ,     __lowerCamelCase ,     __lowerCamelCase       )       )
 return encrypted_blocks






def    _UpperCAmelCase	(   __lowerCamelCase ,     __lowerCamelCase ,     __lowerCamelCase ,     __lowerCamelCase = DEFAULT_BLOCK_SIZE ,     )		->str:


 '''simple docstring'''
 snake_case   : List[Any]	  =      []
 snake_case  ,					snake_case   : List[str]	  =      key
 for block in encrypted_blocks:
  decrypted_blocks.append(pow(__lowerCamelCase ,     __lowerCamelCase ,     __lowerCamelCase       )       )
 return get_text_from_blocks(__lowerCamelCase ,     __lowerCamelCase ,     __lowerCamelCase       )






def    _UpperCAmelCase	(   __lowerCamelCase       )		->tuple[int, int, int]:


 '''simple docstring'''
 with open(__lowerCamelCase       ) as fo:
  snake_case   : List[Any]	  =      fo.read()
 snake_case  ,					snake_case  ,					snake_case   : Dict	  =      content.split(''','''       )
 return (int(__lowerCamelCase       ), int(__lowerCamelCase       ), int(__lowerCamelCase       ))






def    _UpperCAmelCase	(   __lowerCamelCase ,     __lowerCamelCase ,     __lowerCamelCase ,     __lowerCamelCase = DEFAULT_BLOCK_SIZE ,     )		->str:


 '''simple docstring'''
 snake_case  ,					snake_case  ,					snake_case   : List[Any]	  =      read_key_file(__lowerCamelCase       )
 if key_size < block_size * 8:
  sys.exit(
      '''ERROR: Block size is {} bits and key size is {} bits. The RSA cipher '''
      '''requires the block size to be equal to or greater than the key size. '''
      '''Either decrease the block size or use different keys.'''.format(
          block_size * 8 ,     __lowerCamelCase       )       )

 snake_case   : Any	  =      [str(__lowerCamelCase       ) for i in encrypt_message(__lowerCamelCase ,     (n, e) ,     __lowerCamelCase       )]

 snake_case   : List[str]	  =      ''','''.join(__lowerCamelCase       )
 snake_case   : str	  =      f'''{len(__lowerCamelCase       )}_{block_size}_{encrypted_content}'''
 with open(__lowerCamelCase ,     '''w'''       ) as fo:
  fo.write(__lowerCamelCase       )
 return encrypted_content






def    _UpperCAmelCase	(   __lowerCamelCase ,     __lowerCamelCase       )		->str:


 '''simple docstring'''
 snake_case  ,					snake_case  ,					snake_case   : str	  =      read_key_file(__lowerCamelCase       )
 with open(__lowerCamelCase       ) as fo:
  snake_case   : Dict	  =      fo.read()
 snake_case  ,					snake_case  ,					snake_case   : Union[str, Any]	  =      content.split('''_'''       )
 snake_case   : str	  =      int(__lowerCamelCase       )
 snake_case   : int	  =      int(__lowerCamelCase       )

 if key_size < block_size * 8:
  sys.exit(
      '''ERROR: Block size is {} bits and key size is {} bits. The RSA cipher '''
      '''requires the block size to be equal to or greater than the key size. '''
      '''Did you specify the correct key file and encrypted file?'''.format(
          block_size * 8 ,     __lowerCamelCase       )       )

 snake_case   : str	  =      []
 for block in encrypted_message.split(''','''       ):
  encrypted_blocks.append(int(__lowerCamelCase       )       )

 return decrypt_message(__lowerCamelCase ,     __lowerCamelCase ,     (n, d) ,     __lowerCamelCase       )






def    _UpperCAmelCase	(   )		->None:


 '''simple docstring'''
 snake_case   : List[Any]	  =      '''encrypted_file.txt'''
 snake_case   : Optional[int]	  =      input(R'''Encrypt\Decrypt [e\d]: '''       )

 if response.lower().startswith('''e'''       ):
  snake_case   : Dict	  =      '''encrypt'''
 elif response.lower().startswith('''d'''       ):
  snake_case   : Dict	  =      '''decrypt'''

 if mode == "encrypt":
  if not os.path.exists('''rsa_pubkey.txt'''       ):
   rkg.make_key_files('''rsa''' ,     1024       )

  snake_case   : List[Any]	  =      input('''\nEnter message: '''       )
  snake_case   : Union[str, Any]	  =      '''rsa_pubkey.txt'''
  print(f'''Encrypting and writing to {filename}...'''       )
  snake_case   : List[str]	  =      encrypt_and_write_to_file(__lowerCamelCase ,     __lowerCamelCase ,     __lowerCamelCase       )

  print('''\nEncrypted text:'''       )
  print(__lowerCamelCase       )

 elif mode == "decrypt":
  snake_case   : str	  =      '''rsa_privkey.txt'''
  print(f'''Reading from {filename} and decrypting...'''       )
  snake_case   : int	  =      read_from_file_and_decrypt(__lowerCamelCase ,     __lowerCamelCase       )
  print('''writing decryption to rsa_decryption.txt...'''       )
  with open('''rsa_decryption.txt''' ,     '''w'''       ) as dec:
   dec.write(__lowerCamelCase       )

  print('''\nDecryption:'''       )
  print(__lowerCamelCase       )


if __name__ == "__main__":
  main()






