Giter VIP home page Giter VIP logo

fletcher_checksum's Introduction

Fletcher_checksum

Implementation of fletcher-16, fletcher-32, and fletcher-64 checksums in Python3 for working with string and bytes data.

Description

The Fletcher checksum is an algorithm for computing a position-dependent checksum devised by John G. Fletcher (1934โ€“2012) at Lawrence Livermore Labs in the late 1970s. The objective of the Fletcher checksum was to provide error-detection properties approaching those of a cyclic redundancy check but with the lower computational effort associated with summation techniques.

API

This library has functions for working with string and byte data (which also allows you to calculate the checksum for your files). An example of usage is shown below.

# Importing classes with static methods for working with strings and bytes
from FletcherChecksumLib import FletcherChecksumStr, FletcherChecksumBytes

# Example of using string input
test_string_input = 'Hello, world!'
checksum_result = FletcherChecksumStr.get_fletcher16(test_string_input)
print(checksum_result)

>>>   { 
>>>     'Fletcher16_dec': 29069,
>>>     'Fletcher16_hex': '0x718d' 
>>>   }

# Example of using bytes input
test_bytes_input = b'\xd0\x91\xd0\xb0\xd0\xb9\xd1\x82\xd1\x8b'
checksum_result = FletcherChecksumBytes.get_fletcher32(test_bytes_input)
print(checksum_result)

>>>   { 
>>>     'Fletcher32_dec': 670893849,
>>>     'Fletcher32_hex': '0x27fd0719'
>>>   }

# Example of using file(bytes) input
test_file_input = open('test.txt', 'rb')
test_bytes_input = test_file_input.read()
checksum_result = FletcherChecksumBytes.get_fletcher64(test_bytes_input)
print(checksum_result)

>>>   { 
>>>     'Fletcher64_dec': 35532264440969,
>>>     'Fletcher64_hex': '0x205100000489'
>>>   }

fletcher_checksum's People

Contributors

grishnov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

jaybrown jairuz

fletcher_checksum's Issues

32 and 64-bit fletcher checksums are incorrect

It seems you aren't alone on the internet either: https://gchq.github.io/CyberChef/#recipe=Fletcher-64_Checksum()&input=YWJjZGVmZ2g (I'll submit an issue for them too)

The easiest way to demonstrate this is with your examples, where the upper 2 bytes (4 nibbles) are all 0's, when they should be filled with data like Wikipedia: https://en.wikipedia.org/wiki/Fletcher%27s_checksum#Test_vectors

The problem is that you are only using byte-sized chunks for the sums instead of uint16 or uint32 chunks, respectively.

Here is an example of a proper implementation:

import array

def get_fletcher64(data_bytes: bytes) -> dict:
    """
    Accepts bytes as input.
    Returns the Fletcher64 checksum value in decimal and hexadecimal format.
    32-bit implementation (64-bit checksum)
    """
    sum1, sum2 = int(), int()
    data = array.array('I', str.encode(data_bytes))
    print(len(data))
    for index in range(len(data)):
        sum1 = (sum1 + data[index]) % 4294967295
        sum2 = (sum2 + sum1) % 4294967295
    result = (sum2 << 32) | sum1
    return {"Fletcher64_dec": result, "Fletcher64_hex": hex(result)}

Which returns the proper value when using the wikipedia test string.

test_string_input = 'abcdefgh'
checksum_result = FletcherChecksumStr.get_fletcher64(test_string_input)
print(checksum_result)
2
{'Fletcher64_dec': 3543817411021686982, 'Fletcher64_hex': '0x312e2b28cccac8c6'}
  • This doesn't work for strings/bytes where the length isn't a multiple of the checksum size,
  • The above is using signed int, and not unsigned int, which will run into overflow issues.

I'll leave the above as an exercise for the reader ๐Ÿ˜‰

Wrong modulo value for integer truncation operations

Hi!

All modulo operations used to truncate the length of each integer to fit in a certain numbers of bit are wrong. For example:

for index in range(len(data)): sum1 = (sum1 + data[index]) % 255

Should be corrected to:

for index in range(len(data)): sum1 = (sum1 + data[index]) % 256

Because the modulo operation in the form of (A % B) is going to let A be in the range of 0 to B - 1. Using modulo % 255 for modulo will limit integers up to the value of 254 instead of 255 as desired.

The same applies to all other values.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.