




"""simple docstring"""





import argparse
import struct
import unittest



class      A				:


			def __init__(      self      :    Tuple      ,  UpperCamelCase_      :    bytes):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					data

						# Initialize hash values
						__UpperCAmelCase	: Any		=					[
						    0x6A09_E667,
						    0xBB67_AE85,
						    0x3C6E_F372,
						    0xA54F_F53A,
						    0x510E_527F,
						    0x9B05_688C,
						    0x1F83_D9AB,
						    0x5BE0_CD19,
						]

						# Initialize round constants
						__UpperCAmelCase	: Optional[int]		=					[
						    0x428A_2F98,
						    0x7137_4491,
						    0xB5C0_FBCF,
						    0xE9B5_DBA5,
						    0x3956_C25B,
						    0x59F1_11F1,
						    0x923F_82A4,
						    0xAB1C_5ED5,
						    0xD807_AA98,
						    0x1283_5B01,
						    0x2431_85BE,
						    0x550C_7DC3,
						    0x72BE_5D74,
						    0x80DE_B1FE,
						    0x9BDC_06A7,
						    0xC19B_F174,
						    0xE49B_69C1,
						    0xEFBE_4786,
						    0x0FC1_9DC6,
						    0x240C_A1CC,
						    0x2DE9_2C6F,
						    0x4A74_84AA,
						    0x5CB0_A9DC,
						    0x76F9_88DA,
						    0x983E_5152,
						    0xA831_C66D,
						    0xB003_27C8,
						    0xBF59_7FC7,
						    0xC6E0_0BF3,
						    0xD5A7_9147,
						    0x06CA_6351,
						    0x1429_2967,
						    0x27B7_0A85,
						    0x2E1B_2138,
						    0x4D2C_6DFC,
						    0x5338_0D13,
						    0x650A_7354,
						    0x766A_0ABB,
						    0x81C2_C92E,
						    0x9272_2C85,
						    0xA2BF_E8A1,
						    0xA81A_664B,
						    0xC24B_8B70,
						    0xC76C_51A3,
						    0xD192_E819,
						    0xD699_0624,
						    0xF40E_3585,
						    0x106A_A070,
						    0x19A4_C116,
						    0x1E37_6C08,
						    0x2748_774C,
						    0x34B0_BCB5,
						    0x391C_0CB3,
						    0x4ED8_AA4A,
						    0x5B9C_CA4F,
						    0x682E_6FF3,
						    0x748F_82EE,
						    0x78A5_636F,
						    0x84C8_7814,
						    0x8CC7_0208,
						    0x90BE_FFFA,
						    0xA450_6CEB,
						    0xBEF9_A3F7,
						    0xC671_78F2,
						]

						__UpperCAmelCase	: Any		=					self.preprocessing(self.data)
						self.final_hash()


			@staticmethod
			def       a_     (      UpperCamelCase_      :    bytes):


						"""simple docstring"""




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


			def       a_     (      self      :    List[str]):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					[
						    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
									__UpperCAmelCase	: List[str]		=					list(struct.unpack(">16L"      ,  __lowerCamelCase))
									# add 48 0-ed integers
									words += [0] * 48

									__UpperCAmelCase	: Optional[int]		=					self.hashes

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

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

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

												__UpperCAmelCase	: List[Any]		=					(
												    g,
												    f,
												    e,
												    ((d + tempa) % 0x1_0000_0000),
												    c,
												    b,
												    a,
												    ((tempa + tempa) % 0x1_0000_0000),
												)

									__UpperCAmelCase	: List[str]		=					[a, b, c, d, e, f, g, h]

									# Modify final values
									__UpperCAmelCase	: Any		=					[
									    ((element + mutated_hash_values[index]) % 0x1_0000_0000)
									    for index, element in enumerate(self.hashes)
									]

						__UpperCAmelCase	: List[str]		=					"".join([hex(__lowerCamelCase)[2:].zfill(8) for value in self.hashes])


			def       a_     (      self      :    Optional[int]      ,  UpperCamelCase_      :    int      ,  UpperCamelCase_      :    int):


						"""simple docstring"""




						return 0xFFFF_FFFF & (value << (32 - rotations)) | (value >> rotations)






class      A				(	unittest.TestCase   ):


			def       a_     (      self      :    List[Any]):


						"""simple docstring"""




						import hashlib

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






def 					_UpperCamelCase    (			)			->			int:


			"""simple docstring"""
			import doctest

			doctest.testmod()

			__UpperCAmelCase	: List[str]		=					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"							)

			__UpperCAmelCase	: Optional[Any]		=					parser.parse_args()

			__UpperCAmelCase	: List[str]		=					args.input_string

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

			print(SHAaaa(_A							).hash							)


if __name__ == "__main__":
					main()








"""simple docstring"""





import json
import logging
import os
import socket

import git
import numpy as np
import torch


logging.basicConfig(
    format="""%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d -  %(message)s""",
    datefmt="""%m/%d/%Y %H:%M:%S""",
    level=logging.INFO,
)
A   =       logging.getLogger(__name__)

def 					_UpperCamelCase    (			UpperCamelCase							)			->			Any:


			"""simple docstring"""
			__UpperCAmelCase	: Optional[Any]		=					git.Repo(search_parent_directories=UpperCamelCase							)
			__UpperCAmelCase	: Any		=					{
			    "repo_id": str(UpperCamelCase							),
			    "repo_sha": str(repo.head.object.hexsha							),
			    "repo_branch": str(repo.active_branch							),
			}

			with open(os.path.join(UpperCamelCase				,       "git_log.json"							)				,       "w"							) as f:
						json.dump(UpperCamelCase				,       UpperCamelCase				,       indent=4							)

def 					_UpperCamelCase    (			UpperCamelCase							)			->			str:


			"""simple docstring"""
			if params.n_gpu <= 0:
						__UpperCAmelCase	: str		=					0
						__UpperCAmelCase	: Dict		=					-1
						__UpperCAmelCase	: Tuple		=					True
						__UpperCAmelCase	: List[str]		=					False
						return

			assert torch.cuda.is_available()

			logger.info("Initializing GPUs"							)
			if params.n_gpu > 1:
						assert params.local_rank != -1

						__UpperCAmelCase	: Optional[int]		=					int(os.environ["WORLD_SIZE"]							)
						__UpperCAmelCase	: Union[str, Any]		=					int(os.environ["N_GPU_NODE"]							)
						__UpperCAmelCase	: Optional[int]		=					int(os.environ["RANK"]							)

						# number of nodes / node ID
						__UpperCAmelCase	: int		=					params.world_size // params.n_gpu_per_node
						__UpperCAmelCase	: Optional[int]		=					params.global_rank // params.n_gpu_per_node
						__UpperCAmelCase	: Tuple		=					True

						assert params.n_nodes == int(os.environ["N_NODES"]							)
						assert params.node_id == int(os.environ["NODE_RANK"]							)

			# local job (single GPU)
			else:
						assert params.local_rank == -1

						__UpperCAmelCase	: Union[str, Any]		=					1
						__UpperCAmelCase	: Dict		=					0
						__UpperCAmelCase	: List[Any]		=					0
						__UpperCAmelCase	: Tuple		=					0
						__UpperCAmelCase	: Any		=					1
						__UpperCAmelCase	: str		=					1
						__UpperCAmelCase	: Dict		=					False

			# sanity checks
			assert params.n_nodes >= 1
			assert 0 <= params.node_id < params.n_nodes
			assert 0 <= params.local_rank <= params.global_rank < params.world_size
			assert params.world_size == params.n_nodes * params.n_gpu_per_node

			# define whether this is the master process / if we are in multi-node distributed mode
			__UpperCAmelCase	: Union[str, Any]		=					params.node_id == 0 and params.local_rank == 0
			__UpperCAmelCase	: Dict		=					params.n_nodes > 1

			# summary
			__UpperCAmelCase	: Any		=					f"--- Global rank: {params.global_rank} - "
			logger.info(PREFIX + "Number of nodes: %i" % params.n_nodes							)
			logger.info(PREFIX + "Node ID        : %i" % params.node_id							)
			logger.info(PREFIX + "Local rank     : %i" % params.local_rank							)
			logger.info(PREFIX + "World size     : %i" % params.world_size							)
			logger.info(PREFIX + "GPUs per node  : %i" % params.n_gpu_per_node							)
			logger.info(PREFIX + "Master         : %s" % str(params.is_master							)							)
			logger.info(PREFIX + "Multi-node     : %s" % str(params.multi_node							)							)
			logger.info(PREFIX + "Multi-GPU      : %s" % str(params.multi_gpu							)							)
			logger.info(PREFIX + "Hostname       : %s" % socket.gethostname()							)

			# set GPU device
			torch.cuda.set_device(params.local_rank							)

			# initialize multi-GPU
			if params.multi_gpu:
						logger.info("Initializing PyTorch distributed"							)
						torch.distributed.init_process_group(
						    init_method="env://"				,       backend="nccl"				,       )





def 					_UpperCamelCase    (			UpperCamelCase							)			->			Tuple:


			"""simple docstring"""
			np.random.seed(args.seed							)
			torch.manual_seed(args.seed							)
			if args.n_gpu > 0:
						torch.cuda.manual_seed_all(args.seed							)


