initial checksum commit

This commit is contained in:
Remzi Arpaci-Dusseau 2020-05-31 12:36:12 -05:00
parent 27b764f48b
commit c536fa3652
2 changed files with 198 additions and 0 deletions

107
file-integrity/README.md Normal file
View File

@ -0,0 +1,107 @@
# Overview
The program `checksum.py` is very simple: it can be used to calculate checksums
of different flavors (addition, xor-based, or fletcher) on either random
values (as generated by the program) or on values you pass in as a series of
bytes.
When run in the default mode:
```sh
prompt> ./checksum.py
OPTIONS seed 0
OPTIONS data_size 4
OPTIONS data
Decimal: 216 194 107 66
Hex: 0xd8 0xc2 0x6b 0x42
Bin: 0b11011000 0b11000010 0b01101011 0b01000010
Add: ?
Xor: ?
Fletcher: ?
prompt>
```
In this example, the program produces a random set of four numbers (the
"data") of 216 194 107 66 (decimal). The numbers are also shown in hex and
binary.
The program then asks you to compute the additive, xor-based, and fletcher
checksums. The addition is just the result of adding each of the bytes
together, with the result modulo 256 (it's just a single byte checksum).
The xor-based checksum is the result of xor'ing each byte together (it is also
a single byte). Finally, fletcher is the result of computing the two parts of
the fletcher checksum (as described in the chapter), which is two bytes in
total.
You can change the seed to get a different problem:
```sh
prompt> ./checksum.py -s 1
OPTIONS seed 1
OPTIONS data_size 4
OPTIONS data
Decimal: 34 216 195 65
Hex: 0x22 0xd8 0xc3 0x41
Bin: 0b00100010 0b11011000 0b11000011 0b01000001
Add: ?
Xor: ?
Fletcher: ?
prompt>
```
You can specify a different length for the random data:
```sh
prompt> ./checksum.py -D 2
...
You can also specify your own data string:
prompt> ./checksum.py -D 1,2,3,4
OPTIONS seed 0
OPTIONS data_size 4
OPTIONS data 1,2,3,4
Decimal: 1 2 3 4
Hex: 0x01 0x02 0x03 0x04
Bin: 0b00000001 0b00000010 0b00000011 0b00000100
Add: ?
Xor: ?
Fletcher: ?
prompt>
```
Finally, you can use `-c` to have the program compute the checksums for you.
```sh
prompt> ./checksum.py -D 1,2,3,4 -c
OPTIONS seed 0
OPTIONS data_size 4
OPTIONS data 1,2,3,4
Decimal: 1 2 3 4
Hex: 0x01 0x02 0x03 0x04
Bin: 0b00000001 0b00000010 0b00000011 0b00000100
Add: 10 (0b00001010)
Xor: 4 (0b00000100)
Fletcher(a,b): 10, 20 (0b00001010,0b00010100)
prompt>
```
Thus ends the worst README in this collection of READMEs.

91
file-integrity/checksum.py Executable file
View File

@ -0,0 +1,91 @@
#! /usr/bin/env python
from __future__ import print_function
import random
from optparse import OptionParser
# to make Python2 and Python3 act the same -- how dumb
def random_seed(seed):
try:
random.seed(seed, version=1)
except:
random.seed(seed)
return
def print_hex(v):
if v < 16:
return '0x0%x' % v
else:
return '0x%x' % v
def print_bin(word):
v = bin(word)
o = '0b'
o += ('0' * (10 - len(str(v))))
o += str(v)[2:]
return o
parser = OptionParser()
parser.add_option('-s', '--seed', default='0', help='Random seed', action='store', type='int', dest='seed')
parser.add_option('-d', '--data_size', default='4', help='Number of bytes in data word', action='store', type='int', dest='data_size')
parser.add_option('-D', '--data', default='', help='Data in comma separated form', action='store', type='string', dest='data')
parser.add_option('-c', '--compute', help='compute answers for me', action='store_true', default=False, dest='solve')
(options, args) = parser.parse_args()
print('')
print('OPTIONS seed', options.seed)
print('OPTIONS data_size', options.data_size)
print('OPTIONS data', options.data)
print('')
random_seed(options.seed)
values = []
if options.data != '':
tmp = options.data.split(',')
for t in tmp:
values.append(int(t))
else:
for t in range(int(options.data_size)):
values.append(int(random.random() * 256))
add = 0
xor = 0
fletcher_a, fletcher_b = 0, 0
for value in values:
add = (add + value) % 256
xor = xor ^ value
fletcher_a = (fletcher_a + value) % 255
fletcher_b = (fletcher_b + fletcher_a) % 255
print('Decimal: ', end=' ')
for word in values:
print('%10s' % str(word), end=' ')
print('')
print('Hex: ', end=' ')
for word in values:
print(' ', print_hex(word), end=' ')
print('')
print('Bin: ', end=' ')
for word in values:
print(print_bin(word), end=' ')
print('')
print('')
if options.solve:
print('Add: ', '%3d ' % add, '(%s)' % print_bin(add))
print('Xor: ', '%3d ' % xor, '(%s)' % print_bin(xor))
print('Fletcher(a,b): ', '%3d,%3d ' % (fletcher_a, fletcher_b), '(%s,%s)' % (print_bin(fletcher_a), print_bin(fletcher_b)))
else:
print('Add: ?')
print('Xor: ?')
print('Fletcher: ?')
print('')