MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / main

Function main

hashes/sha256.py:209–244  ·  view source on GitHub ↗

Provides option 'string' or 'file' to take input and prints the calculated SHA-256 hash

()

Source from the content-addressed store, hash-verified

207
208
209def main() -> None:
210 """
211 Provides option 'string' or 'file' to take input
212 and prints the calculated SHA-256 hash
213 """
214
215 # unittest.main()
216
217 import doctest
218
219 doctest.testmod()
220
221 parser = argparse.ArgumentParser()
222 parser.add_argument(
223 "-s",
224 "--string",
225 dest="input_string",
226 default="Hello World!! Welcome to Cryptography",
227 help="Hash the string",
228 )
229 parser.add_argument(
230 "-f", "--file", dest="input_file", help="Hash contents of a file"
231 )
232
233 args = parser.parse_args()
234
235 input_string = args.input_string
236
237 # hash input should be a bytestring
238 if args.input_file:
239 with open(args.input_file, "rb") as f:
240 hash_input = f.read()
241 else:
242 hash_input = bytes(input_string, "utf-8")
243
244 print(SHA256(hash_input).hash)
245
246
247if __name__ == "__main__":

Callers 1

sha256.pyFile · 0.70

Calls 1

SHA256Class · 0.85

Tested by

no test coverage detected