-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc.py
61 lines (42 loc) · 1.51 KB
/
crc.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
import binascii
info = '''A program which can be run to return the CRC32 value of a specified
file (in hex and dec).
Run with Python 3 installed and follow the instructions.
'''
todo = '''Have it loop around unless a certain filename is entered (or blank).
'''
def load_binary_data_from_file(filepath):
'''rb = Read in Binary format.
IOError exception may be raised if file cannot be read.
'''
with open(filepath, 'rb') as f:
return f.read()
def get_crc32(in_data):
'''Must be in binary form such as the string: b"hello world".
'''
return binascii.crc32(in_data) & 0xffffffff
def get_crc32_of_file(filepath):
return get_crc32(load_binary_data_from_file(filepath))
def format_crc32(crc_value):
'''Hex value zero padded to 8 digits long.
'''
# 'hex=%08X dec=%d' % (crc_value, crc_value)
return 'hex={0:08X} dec={0:d}'.format(crc_value)
def get_formatted_crc32(in_data):
return format_crc32(get_crc32(in_data))
def get_formatted_crc32_of_file(filepath):
return format_crc32(get_crc32_of_file(filepath))
if __name__ == "__main__":
print('CRC32 Calculator')
while True:
filepath = input('\nEnter filepath (blank to exit): ').strip()
if not filepath:
break
print()
try:
crc_formatted = get_formatted_crc32_of_file(filepath)
except IOError:
print('File does not exist: %s' % (filepath))
else:
print('CRC of "%s":\n%s' % (filepath, crc_formatted))