




"""simple docstring"""


import argparse
import struct
import unittest


class _lowerCamelCase    :






   def __init__(		self       :		List[Any]   ,     UpperCamelCase       :		bytes   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			str   =	data

         # Initialize hash values
         lowerCAmelCase__      :			Any   =	[
             0X6a_09_e6_67,
             0Xbb_67_ae_85,
             0X3c_6e_f3_72,
             0Xa5_4f_f5_3a,
             0X51_0e_52_7f,
             0X9b_05_68_8c,
             0X1f_83_d9_ab,
             0X5b_e0_cd_19,
         ]

         # Initialize round constants
         lowerCAmelCase__      :			Optional[int]   =	[
             0X42_8a_2f_98,
             0X71_37_44_91,
             0Xb5_c0_fb_cf,
             0Xe9_b5_db_a5,
             0X39_56_c2_5b,
             0X59_f1_11_f1,
             0X92_3f_82_a4,
             0Xab_1c_5e_d5,
             0Xd8_07_aa_98,
             0X12_83_5b_01,
             0X24_31_85_be,
             0X55_0c_7d_c3,
             0X72_be_5d_74,
             0X80_de_b1_fe,
             0X9b_dc_06_a7,
             0Xc1_9b_f1_74,
             0Xe4_9b_69_c1,
             0Xef_be_47_86,
             0X0f_c1_9d_c6,
             0X24_0c_a1_cc,
             0X2d_e9_2c_6f,
             0X4a_74_84_aa,
             0X5c_b0_a9_dc,
             0X76_f9_88_da,
             0X98_3e_51_52,
             0Xa8_31_c6_6d,
             0Xb0_03_27_c8,
             0Xbf_59_7f_c7,
             0Xc6_e0_0b_f3,
             0Xd5_a7_91_47,
             0X06_ca_63_51,
             0X14_29_29_67,
             0X27_b7_0a_85,
             0X2e_1b_21_38,
             0X4d_2c_6d_fc,
             0X53_38_0d_13,
             0X65_0a_73_54,
             0X76_6a_0a_bb,
             0X81_c2_c9_2e,
             0X92_72_2c_85,
             0Xa2_bf_e8_a1,
             0Xa8_1a_66_4b,
             0Xc2_4b_8b_70,
             0Xc7_6c_51_a3,
             0Xd1_92_e8_19,
             0Xd6_99_06_24,
             0Xf4_0e_35_85,
             0X10_6a_a0_70,
             0X19_a4_c1_16,
             0X1e_37_6c_08,
             0X27_48_77_4c,
             0X34_b0_bc_b5,
             0X39_1c_0c_b3,
             0X4e_d8_aa_4a,
             0X5b_9c_ca_4f,
             0X68_2e_6f_f3,
             0X74_8f_82_ee,
             0X78_a5_63_6f,
             0X84_c8_78_14,
             0X8c_c7_02_08,
             0X90_be_ff_fa,
             0Xa4_50_6c_eb,
             0Xbe_f9_a3_f7,
             0Xc6_71_78_f2,
         ]

         lowerCAmelCase__      :			List[Any]   =	self.preprocessing(self.data   )
         self.final_hash()






   @staticmethod
   def 	_lowerCAmelCase   (		UpperCamelCase       :		bytes   )			->						bytes:

         """simple docstring"""
         lowerCAmelCase__      :			List[Any]   =	b"""\x80""" + (b"""\x00""" * (63 - (len(UpperCamelCase   ) + 8) % 64))
         lowerCAmelCase__      :			List[Any]   =	struct.pack(""">Q"""   ,     (len(UpperCamelCase   ) * 8)   )
         return data + padding + big_endian_integer






   def 	_lowerCAmelCase   (		self       :		List[str]   )			->						None:

         """simple docstring"""
         # Convert into blocks of 64 bytes
         lowerCAmelCase__      :			List[str]   =	[
             self.preprocessed_data[x : x + 64]
             for x in range(0   ,     len(self.preprocessed_data   )   ,     64   )
         ]

         for block in self.blocks:
               # Convert the given block into a list of 4 byte integers
               lowerCAmelCase__      :			str   =	list(struct.unpack(""">16L"""   ,     UpperCamelCase   )   )
               # add 48 0-ed integers
               words += [0] * 48

               lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__      :			Optional[int]   =	self.hashes

               for index in range(0   ,     64   ):
                     if index > 15:
                           # modify the zero-ed indexes at the end of the array
                           lowerCAmelCase__      :			List[str]   =	(
                               self.ror(words[index - 15]   ,     7   )
                               ^ self.ror(words[index - 15]   ,     18   )
                               ^ (words[index - 15] >> 3)
                           )
                           lowerCAmelCase__      :			int   =	(
                               self.ror(words[index - 2]   ,     17   )
                               ^ self.ror(words[index - 2]   ,     19   )
                               ^ (words[index - 2] >> 10)
                           )

                           lowerCAmelCase__      :			Tuple   =	(
                               words[index - 16] + sa + words[index - 7] + sa
                           ) % 0X1_00_00_00_00

                     # Compression
                     lowerCAmelCase__      :			Dict   =	self.ror(UpperCamelCase   ,     6   ) ^ self.ror(UpperCamelCase   ,     11   ) ^ self.ror(UpperCamelCase   ,     25   )
                     lowerCAmelCase__      :			Optional[Any]   =	(e & f) ^ ((~e & 0Xff_ff_ff_ff) & g)
                     lowerCAmelCase__      :			Optional[int]   =	(
                         h + sa + ch + self.round_constants[index] + words[index]
                     ) % 0X1_00_00_00_00
                     lowerCAmelCase__      :			Tuple   =	self.ror(UpperCamelCase   ,     2   ) ^ self.ror(UpperCamelCase   ,     13   ) ^ self.ror(UpperCamelCase   ,     22   )
                     lowerCAmelCase__      :			int   =	(a & b) ^ (a & c) ^ (b & c)
                     lowerCAmelCase__      :			Union[str, Any]   =	(sa + maj) % 0X1_00_00_00_00

                     lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__      :			Optional[int]   =	(
                         g,
                         f,
                         e,
                         ((d + tempa) % 0X1_00_00_00_00),
                         c,
                         b,
                         a,
                         ((tempa + tempa) % 0X1_00_00_00_00),
                     )

               lowerCAmelCase__      :			Any   =	[a, b, c, d, e, f, g, h]

               # Modify final values
               lowerCAmelCase__      :			Tuple   =	[
                   ((element + mutated_hash_values[index]) % 0X1_00_00_00_00)
                   for index, element in enumerate(self.hashes   )
               ]

         lowerCAmelCase__      :			Dict   =	"""""".join([hex(UpperCamelCase   )[2:].zfill(8   ) for value in self.hashes]   )






   def 	_lowerCAmelCase   (		self       :		Tuple   ,     UpperCamelCase       :		int   ,     UpperCamelCase       :		int   )			->						int:

         """simple docstring"""
         return 0Xff_ff_ff_ff & (value << (32 - rotations)) | (value >> rotations)






class _lowerCamelCase    (					unittest.TestCase						):






   def 	_lowerCAmelCase   (		self       :		int   )			->						None:

         """simple docstring"""
         import hashlib

         lowerCAmelCase__      :			Union[str, Any]   =	bytes("""Test String"""   ,     """utf-8"""   )
         self.assertEqual(SHAaaa(UpperCamelCase   ).hash   ,     hashlib.shaaaa(UpperCamelCase   ).hexdigest()   )






def 	lowercase_				(			)						->    None:
      import doctest

      doctest.testmod()

      lowerCAmelCase__      :			List[Any]   =	argparse.ArgumentParser()
      parser.add_argument(
          """-s"""    ,		"""--string"""    ,		dest="""input_string"""    ,		default="""Hello World!! Welcome to Cryptography"""    ,		help="""Hash the string"""    ,		)
      parser.add_argument(
          """-f"""    ,		"""--file"""    ,		dest="""input_file"""    ,		help="""Hash contents of a file"""				)

      lowerCAmelCase__      :			List[str]   =	parser.parse_args()

      lowerCAmelCase__      :			Any   =	args.input_string

      # hash input should be a bytestring
      if args.input_file:
            with open(args.input_file    ,		"""rb"""				) as f:
                  lowerCAmelCase__      :			Union[str, Any]   =	f.read()
      else:
            lowerCAmelCase__      :			str   =	bytes(__UpperCAmelCase    ,		"""utf-8"""				)

      print(SHAaaa(__UpperCAmelCase				).hash				)


if __name__ == "__main__":
  main()











"""simple docstring"""


from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule



def 	lowercase_				(			__UpperCAmelCase    ,		__UpperCAmelCase    ,		__UpperCAmelCase=1E-12				)						->    Dict:
      lowerCAmelCase__      :			Optional[int]   =	jnp.divide(emb_a.T    ,		jnp.clip(jnp.linalg.norm(__UpperCAmelCase    ,		axis=1				)    ,		a_min=__UpperCAmelCase				)				).T
      lowerCAmelCase__      :			Any   =	jnp.divide(emb_a.T    ,		jnp.clip(jnp.linalg.norm(__UpperCAmelCase    ,		axis=1				)    ,		a_min=__UpperCAmelCase				)				).T
      return jnp.matmul(__UpperCAmelCase    ,		norm_emb_a.T				)


class _lowerCamelCase    (					nn.Module						):
   _lowerCamelCase    :CLIPConfig
   _lowerCamelCase    :jnp.dtype     				=     jnp.floataa






   def 	_lowerCAmelCase   (		self       :		List[str]   )			->						Tuple:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[int]   =	FlaxCLIPVisionModule(self.config.vision_config   )
         lowerCAmelCase__      :			Dict   =	nn.Dense(self.config.projection_dim   ,     use_bias=UpperCamelCase   ,     dtype=self.dtype   )

         lowerCAmelCase__      :			Optional[int]   =	self.param("""concept_embeds"""   ,     jax.nn.initializers.ones   ,     (17, self.config.projection_dim)   )
         lowerCAmelCase__      :			Optional[int]   =	self.param(
             """special_care_embeds"""   ,     jax.nn.initializers.ones   ,     (3, self.config.projection_dim)   )

         lowerCAmelCase__      :			List[Any]   =	self.param("""concept_embeds_weights"""   ,     jax.nn.initializers.ones   ,     (17,)   )
         lowerCAmelCase__      :			List[Any]   =	self.param("""special_care_embeds_weights"""   ,     jax.nn.initializers.ones   ,     (3,)   )






   def __call__(		self       :		str   ,     UpperCamelCase       :		List[Any]   )			->						Union[str, Any]:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	self.vision_model(UpperCamelCase   )[1]
         lowerCAmelCase__      :			List[Any]   =	self.visual_projection(UpperCamelCase   )

         lowerCAmelCase__      :			str   =	jax_cosine_distance(UpperCamelCase   ,     self.special_care_embeds   )
         lowerCAmelCase__      :			Optional[int]   =	jax_cosine_distance(UpperCamelCase   ,     self.concept_embeds   )

         # increase this value to create a stronger `nfsw` filter
         # at the cost of increasing the possibility of filtering benign image inputs
         lowerCAmelCase__      :			Any   =	0.0

         lowerCAmelCase__      :			List[Any]   =	special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
         lowerCAmelCase__      :			Any   =	jnp.round(UpperCamelCase   ,     3   )
         lowerCAmelCase__      :			List[str]   =	jnp.any(special_scores > 0   ,     axis=1   ,     keepdims=UpperCamelCase   )
         # Use a lower threshold if an image has any special care concept
         lowerCAmelCase__      :			Tuple   =	is_special_care * 0.01

         lowerCAmelCase__      :			List[Any]   =	cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
         lowerCAmelCase__      :			Dict   =	jnp.round(UpperCamelCase   ,     3   )
         lowerCAmelCase__      :			List[str]   =	jnp.any(concept_scores > 0   ,     axis=1   )

         return has_nsfw_concepts






class _lowerCamelCase    (					a_						):
   _lowerCamelCase    :Optional[Any]     				=     CLIPConfig
   _lowerCamelCase    :Dict     				=     "clip_input"
   _lowerCamelCase    :int     				=     FlaxStableDiffusionSafetyCheckerModule






   def __init__(		self       :		int   ,     UpperCamelCase       :		CLIPConfig   ,     UpperCamelCase       :		Optional[Tuple] = None   ,     UpperCamelCase       :		int = 0   ,     UpperCamelCase       :		jnp.dtype = jnp.floataa   ,     UpperCamelCase       :		bool = True   ,     **UpperCamelCase       :		int   ,     )			->						str:

         """simple docstring"""
         if input_shape is None:
               lowerCAmelCase__      :			str   =	(1, 2_24, 2_24, 3)
         lowerCAmelCase__      :			Union[str, Any]   =	self.module_class(config=UpperCamelCase   ,     dtype=UpperCamelCase   ,     **UpperCamelCase   )
         super().__init__(UpperCamelCase   ,     UpperCamelCase   ,     input_shape=UpperCamelCase   ,     seed=UpperCamelCase   ,     dtype=UpperCamelCase   ,     _do_init=_do_init   )






   def 	_lowerCAmelCase   (		self       :		Dict   ,     UpperCamelCase       :		jax.random.KeyArray   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		FrozenDict = None   )			->						FrozenDict:

         """simple docstring"""
         # init input tensor
         lowerCAmelCase__      :			Union[str, Any]   =	jax.random.normal(UpperCamelCase   ,     UpperCamelCase   )

         lowerCAmelCase__ ,   lowerCAmelCase__      :			Union[str, Any]   =	jax.random.split(UpperCamelCase   )
         lowerCAmelCase__      :			List[Any]   =	{"""params""": params_rng, """dropout""": dropout_rng}

         lowerCAmelCase__      :			str   =	self.module.init(UpperCamelCase   ,     UpperCamelCase   )["""params"""]

         return random_params






   def __call__(		self       :		Dict   ,     UpperCamelCase       :		Tuple   ,     UpperCamelCase       :		dict = None   ,     )			->						List[Any]:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	jnp.transpose(UpperCamelCase   ,     (0, 2, 3, 1)   )

         return self.module.apply(
             {"""params""": params or self.params}   ,     jnp.array(UpperCamelCase   ,     dtype=jnp.floataa   )   ,     rngs={}   ,     )





