Coder Social home page Coder Social logo

magicalfish / python-bchlib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jkent/python-bchlib

0.0 0.0 0.0 88 KB

BCH library C Python module

Home Page: https://pypi.org/project/bchlib/

License: GNU General Public License v2.0

Shell 0.92% Python 7.90% C 91.17%

python-bchlib's Introduction

python-bchlib Build Status

This is a python module for encoding and correcting data using BCH codes.

Requirements

For Windows, python3.5 or greater required.
For Linux and MacOS, python2.7 or python3.4 or greater required.

Installing the latest release:

$ pip install bchlib

Installing from source:

Make sure you have python-dev setup. For Windows, this means you need Visual Studio 2015.

$ pip install .

Module Documentation

bchlib.BCH( polynomial, t[, reverse] ) → bch

Constructor creates a BCH object with given polynomial and t bit strength, reverse is an optional boolean that flips the bit order of data. The Galois field order is automatically determined from the polynomial.

bch.encode( data[, ecc] ) → ecc

Encodes data with an optional starting ecc and returns an ecc.

bch.decode( data, ecc ) → ( bitflips, data, ecc )

Corrects data using ecc and returns a tuple.

bch.decode_inplace( data, ecc ) → bitflips

Corrects data using ecc in place, returning the number of bitflips.

bch.decode_syndromes( data, syndromes ) → ( bitflips, data )

Corrects data using a sequence of syndromes, of t*2 elements, returning a tuple.

bch.compute_even_syndromes( syndromes ) → syndromes

Computes even syndromes from odd ones. Takes and returns a sequence of t*2 elements.

bch.ecc_bytes

A readonly field; the number of bytes an ecc takes up.

bch.ecc_bits

A readonly field; the number of bits an ecc takes up.

bch.m

A readonly field; the Galois field order.

bch.n

A readonly field; the maximum codeword size in bits.

bch.syndromes

A readonly field; a tuple of syndromes after performing a correct operation.

bch.t

A readonly field; the number bit errors that can be corrected.

Usage Example

import bchlib
import hashlib
import os
import random

# create a bch object
BCH_POLYNOMIAL = 8219
BCH_BITS = 16
bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS)

# random data
data = bytearray(os.urandom(512))

# encode and make a "packet"
ecc = bch.encode(data)
packet = data + ecc

# print hash of packet
sha1_initial = hashlib.sha1(packet)
print('sha1: %s' % (sha1_initial.hexdigest(),))

def bitflip(packet):
    byte_num = random.randint(0, len(packet) - 1)
    bit_num = random.randint(0, 7)
    packet[byte_num] ^= (1 << bit_num)

# make BCH_BITS errors
for _ in range(BCH_BITS):
    bitflip(packet)

# print hash of packet
sha1_corrupt = hashlib.sha1(packet)
print('sha1: %s' % (sha1_corrupt.hexdigest(),))

# de-packetize
data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:]

# correct
bitflips = bch.decode_inplace(data, ecc)
print('bitflips: %d' % (bitflips))

# packetize
packet = data + ecc

# print hash of packet
sha1_corrected = hashlib.sha1(packet)
print('sha1: %s' % (sha1_corrected.hexdigest(),))

if sha1_initial.digest() == sha1_corrected.digest():
    print('Corrected!')
else:
    print('Failed')

python-bchlib's People

Contributors

jkent avatar dmopalmer avatar

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.