pa02

py

School

University of Central Florida *

*We aren’t endorsed by this school

Course

CIS 3360

Subject

Computer Science

Date

Dec 6, 2023

Type

py

Pages

2

Uploaded by JusticeHawk21536

Report
''' /*============================================================================= | Assignment: pa02 - Calculating an 8, 16, or 32 bit | checksum on an ASCII input file | | Author: Jose Alves Muniz Neto | Language: Python | | To Compile: python pa02.py //Caution - expecting input parameters | | To Execute: python-> python3 pa02.py inputFile.txt 8 | where inputFile.txt is an ASCII input file | and the number 8 could also be 16 or 32 | which are the valid checksum sizes, all | other values are rejected with an error message | and program termination | | Note: All input files are simple 8 bit ASCII input | | Class: CIS3360 - Security in Computing - Fall 2023 | Instructor: McAlpin | Due Date: Nov 12 2023 | +=============================================================================*/ ''' import sys #to be able to read from cmd input_file = sys.argv[1] #retrieving input file from cmd checksum_size = int(sys.argv[2]) #retrieving size from cmd #function to print only 80 letters per line def line_range(result): for i in range(0,len(result),80): print() print(result[i:i+80], end='') #function to calculate the checksum def calculate_checksum(data, checksum_size): #checksum for 8bit if checksum_size == 8: checksum = sum(ord(char) for char in data) & 0xFF #checksum for 16 bit elif checksum_size == 16: #pad data if its length is odd if len(data) % 2 == 1: data += 'X' checksum = sum(ord(data[i]) << 8 | ord(data[i + 1]) for i in range(0, len(data), 2)) & 0xFFFF #checksum for 32 bit elif checksum_size == 32: checksum = 0 #pad data if its length is odd if(len(data) <= 4): for char in data: checksum = (checksum << 8 | ord(char)) & 0xFFFFFFFF
if(len(data)>4): while ((len(data) % 4) != 0): data += 'X' checksum = sum((ord(data[i]) << 24 | ord(data[i + 1]) << 16 | ord(data[i + 2]) << 8 | ord(data[i + 3])) for i in range(0, len(data), 4)) & 0xFFFFFFFF return checksum def main(): #checksum size handler if checksum_size not in [8, 16, 32]: sys.stderr.write("Valid checksum sizes are 8, 16, or 32\n") sys.exit(1) #open the file and get the data try: with open(input_file, 'r', encoding="utf-8") as file: data = file.read() except FileNotFoundError: sys.stderr.write(f"File not found: {input_file}\n") sys.exit(1) #rows of 80 characters line_range(data) #calculate the checksum checksum = calculate_checksum(data, checksum_size) #calculate padding padding = len(data) % (checksum_size // 8) if padding > 0: padding = checksum_size // 8 - padding data += 'X' * padding checksum = calculate_checksum(data, checksum_size) padding = 'X' * padding #print the result if len(padding) > 0: print(padding) else: print("") print(f" {checksum_size} bit checksum is {checksum:{checksum_size//4}x} for all {len(data)} chars") main() ''' /*============================================================================= | I Jose Alves Muniz Neto (5535042) affirm that this program is | entirely my own work and that I have neither developed my code together with | any another person, nor copied any code from any other person, nor permitted | my code to be copied or otherwise used by any other person, nor have I | copied, modified, or otherwise used programs created by others. I acknowledge | that any violation of the above terms will be treated as academic dishonesty. +============================================================================*/ '''
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help