Provides option 'string' or 'file' to take input and prints the calculated SHA-256 hash
()
| 207 | |
| 208 | |
| 209 | def 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 | |
| 247 | if __name__ == "__main__": |