






'''simple docstring'''
import argparse
import struct
import unittest


class        _UpperCAmelCase						:




    def __init__(			self  :       int  ,     a  :       bytes			):






       '''simple docstring'''






       lowercase_	:    List[Any]           =     data

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

       # Initialize round constants
       lowercase_	:    List[Any]           =     [
           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,
       ]

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




    @staticmethod
    def 				lowerCAmelCase__				(			a  :       bytes			):






       '''simple docstring'''






       lowercase_	:    List[Any]           =     b"\x80" + (b"\x00" * (6_3 - (len(a			) + 8) % 6_4))
       lowercase_	:    List[str]           =     struct.pack(">Q"  ,     (len(a			) * 8)			)
       return data + padding + big_endian_integer




    def 				lowerCAmelCase__				(			self  :       List[str]			):






       '''simple docstring'''






       lowercase_	:    Union[str, Any]           =     [
           self.preprocessed_data[x : x + 6_4]
           for x in range(0  ,     len(self.preprocessed_data			)  ,     6_4			)
       ]

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

          lowercase_    ,		lowercase_    ,		lowercase_    ,		lowercase_    ,		lowercase_    ,		lowercase_    ,		lowercase_    ,		lowercase_	:    int           =     self.hashes

          for index in range(0  ,     6_4			):
             if index > 1_5:
                # modify the zero-ed indexes at the end of the array
                lowercase_	:    Optional[int]           =     (
                    self.ror(words[index - 1_5]  ,     7			)
                    ^ self.ror(words[index - 1_5]  ,     1_8			)
                    ^ (words[index - 1_5] >> 3)
                )
                lowercase_	:    Union[str, Any]           =     (
                    self.ror(words[index - 2]  ,     1_7			)
                    ^ self.ror(words[index - 2]  ,     1_9			)
                    ^ (words[index - 2] >> 1_0)
                )

                lowercase_	:    str           =     (
                    words[index - 1_6] + sa + words[index - 7] + sa
                ) % 0x1_0000_0000

             # Compression
             lowercase_	:    List[str]           =     self.ror(a  ,     6			) ^ self.ror(a  ,     1_1			) ^ self.ror(a  ,     2_5			)
             lowercase_	:    Optional[Any]           =     (e & f) ^ ((~e & 0xFFFF_FFFF) & g)
             lowercase_	:    Optional[int]           =     (
                 h + sa + ch + self.round_constants[index] + words[index]
             ) % 0x1_0000_0000
             lowercase_	:    List[Any]           =     self.ror(a  ,     2			) ^ self.ror(a  ,     1_3			) ^ self.ror(a  ,     2_2			)
             lowercase_	:    str           =     (a & b) ^ (a & c) ^ (b & c)
             lowercase_	:    Dict           =     (sa + maj) % 0x1_0000_0000

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

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

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

       lowercase_	:    Optional[int]           =     "".join([hex(a			)[2:].zfill(8			) for value in self.hashes]			)





    def 				lowerCAmelCase__				(			self  :       List[str]  ,     a  :       int  ,     a  :       int			):






       '''simple docstring'''






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


class        _UpperCAmelCase						(		unittest.TestCase			):




    def 				lowerCAmelCase__				(			self  :       str			):






       '''simple docstring'''






       import hashlib

       lowercase_	:    List[str]           =     bytes("Test String"  ,     "utf-8"			)
       self.assertEqual(SHAaaa(a			).hash  ,     hashlib.shaaaa(a			).hexdigest()			)




def 		__SCREAMING_SNAKE_CASE					(  ):


   """simple docstring"""




   import doctest

   doctest.testmod()

   lowercase_	:    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"							)

   lowercase_	:    str           =     parser.parse_args()

   lowercase_	:    List[Any]           =     args.input_string

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

   print(SHAaaa(_UpperCamelCase							).hash							)


if __name__ == "__main__":
 main()












'''simple docstring'''
import multiprocessing
import os
from typing import BinaryIO, Optional, Union

import fsspec

from .. import Dataset, Features, NamedSplit, config
from ..formatting import query_table
from ..packaged_modules.json.json import Json
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader


class        _UpperCAmelCase						(		snake_case			):




    def __init__(			self  :       Tuple  ,     a  :       NestedDataStructureLike[PathLike]  ,     a  :       Optional[NamedSplit] = None  ,     a  :       Optional[Features] = None  ,     a  :       str = None  ,     a  :       bool = False  ,     a  :       bool = False  ,     a  :       Optional[str] = None  ,     a  :       Optional[int] = None  ,     **a  :       List[Any]  ,     ):






       '''simple docstring'''






       super().__init__(
           a  ,     split=a  ,     features=a  ,     cache_dir=a  ,     keep_in_memory=a  ,     streaming=a  ,     num_proc=a  ,     **a  ,     )
       lowercase_	:    str           =     field
       lowercase_	:    Optional[Any]           =     path_or_paths if isinstance(a  ,     a			) else {self.split: path_or_paths}
       lowercase_	:    Any           =     Json(
           cache_dir=a  ,     data_files=a  ,     features=a  ,     field=a  ,     **a  ,     )





    def 				lowerCAmelCase__				(			self  :       Optional[int]			):






       '''simple docstring'''






       if self.streaming:
          lowercase_	:    Any           =     self.builder.as_streaming_dataset(split=self.split			)
       # Build regular (map-style) dataset
       else:
          lowercase_	:    Dict           =     None
          lowercase_	:    Optional[int]           =     None
          lowercase_	:    str           =     None
          lowercase_	:    str           =     None

          self.builder.download_and_prepare(
              download_config=a  ,     download_mode=a  ,     verification_mode=a  ,     base_path=a  ,     num_proc=self.num_proc  ,     )
          lowercase_	:    int           =     self.builder.as_dataset(
              split=self.split  ,     verification_mode=a  ,     in_memory=self.keep_in_memory			)
       return dataset


class        _UpperCAmelCase						:




    def __init__(			self  :       str  ,     a  :       Dataset  ,     a  :       Union[PathLike, BinaryIO]  ,     a  :       Optional[int] = None  ,     a  :       Optional[int] = None  ,     **a  :       List[Any]  ,     ):






       '''simple docstring'''






       if num_proc is not None and num_proc <= 0:
          raise ValueError(f"""num_proc {num_proc} must be an integer > 0."""			)

       lowercase_	:    Dict           =     dataset
       lowercase_	:    Optional[int]           =     path_or_buf
       lowercase_	:    List[Any]           =     batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
       lowercase_	:    Optional[Any]           =     num_proc
       lowercase_	:    List[Any]           =     "utf-8"
       lowercase_	:    List[str]           =     to_json_kwargs




    def 				lowerCAmelCase__				(			self  :       Optional[int]			):






       '''simple docstring'''






       lowercase_	:    str           =     self.to_json_kwargs.pop("path_or_buf"  ,     a			)
       lowercase_	:    Any           =     self.to_json_kwargs.pop("orient"  ,     "records"			)
       lowercase_	:    Any           =     self.to_json_kwargs.pop("lines"  ,     True if orient == "records" else False			)
       lowercase_	:    List[str]           =     self.to_json_kwargs.pop("index"  ,     False if orient in ["split", "table"] else True			)
       lowercase_	:    int           =     self.to_json_kwargs.pop("compression"  ,     a			)

       if compression not in [None, "infer", "gzip", "bz2", "xz"]:
          raise NotImplementedError(f"""`datasets` currently does not support {compression} compression"""			)

       if isinstance(self.path_or_buf  ,     (str, bytes, os.PathLike)			):
          with fsspec.open(self.path_or_buf  ,     "wb"  ,     compression=a			) as buffer:
             lowercase_	:    Dict           =     self._write(file_obj=a  ,     orient=a  ,     lines=a  ,     index=a  ,     **self.to_json_kwargs			)
       else:
          if compression:
             raise NotImplementedError(
                 f"""The compression parameter is not supported when writing to a buffer, but compression={compression}"""
                 " was passed. Please provide a local path instead."			)
          lowercase_	:    Dict           =     self._write(
              file_obj=self.path_or_buf  ,     orient=a  ,     lines=a  ,     index=a  ,     **self.to_json_kwargs			)
       return written




    def 				lowerCAmelCase__				(			self  :       Optional[int]  ,     a  :       List[str]			):






       '''simple docstring'''






       lowercase_    ,		lowercase_    ,		lowercase_    ,		lowercase_    ,		lowercase_	:    List[Any]           =     args

       lowercase_	:    Optional[int]           =     query_table(
           table=self.dataset.data  ,     key=slice(a  ,     offset + self.batch_size			)  ,     indices=self.dataset._indices  ,     )
       lowercase_	:    Dict           =     batch.to_pandas().to_json(
           path_or_buf=a  ,     orient=a  ,     lines=a  ,     index=a  ,     **a			)
       if not json_str.endswith("\n"			):
          json_str += "\n"
       return json_str.encode(self.encoding			)





    def 				lowerCAmelCase__				(			self  :       int  ,     a  :       BinaryIO  ,     a  :       int  ,     a  :       str  ,     a  :       Union[str, Any]  ,     **a  :       str  ,     ):






       '''simple docstring'''






       lowercase_	:    Union[str, Any]           =     0

       if self.num_proc is None or self.num_proc == 1:
          for offset in logging.tqdm(
              range(0  ,     len(self.dataset			)  ,     self.batch_size			)  ,     unit="ba"  ,     disable=not logging.is_progress_bar_enabled()  ,     desc="Creating json from Arrow format"  ,     ):
             lowercase_	:    Dict           =     self._batch_json((offset, orient, lines, index, to_json_kwargs)			)
             written += file_obj.write(a			)
       else:
          lowercase_    ,		lowercase_	:    Any           =     len(self.dataset			), self.batch_size
          with multiprocessing.Pool(self.num_proc			) as pool:
             for json_str in logging.tqdm(
                 pool.imap(
                     self._batch_json  ,     [(offset, orient, lines, index, to_json_kwargs) for offset in range(0  ,     a  ,     a			)]  ,     )  ,     total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size  ,     unit="ba"  ,     disable=not logging.is_progress_bar_enabled()  ,     desc="Creating json from Arrow format"  ,     ):
                written += file_obj.write(a			)

       return written




