-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchecksum_file.py
More file actions
42 lines (32 loc) · 1.14 KB
/
checksum_file.py
File metadata and controls
42 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import hashlib
import os.path
filename = input("Enter the input file name: ")
def sha256_chk(filename):
sha256_hash = hashlib.sha256()
with open(filename,"rb") as f:
# Read and update hash string value in blocks of 4K
for byte_block in iter(lambda: f.read(4096),b""):
sha256_hash.update(byte_block)
print(sha256_hash.hexdigest())
def md5_chk(filename):
md5_hash = hashlib.md5()
with open(filename,"rb") as f:
# Read and update hash string value in blocks of 4K
for byte_block in iter(lambda: f.read(4096),b""):
md5_hash.update(byte_block)
print(md5_hash.hexdigest())
def main():
if os.path.isfile(filename):
hashfunction = input("Enter the input hash function(md5 or sha256): ")
if hashfunction == "md5":
md5_chk(filename)
elif hashfunction == "sha256":
sha256_chk(filename)
else:
Raise("{} is an invalid hash function. Please Enter MD5 or SHA256")
else:
print ("File not exist")
if __name__ == "__main__":
main()