Python Forum

Full Version: How can I use CRC libs in python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi! I'm trying to use the checksum algorithm CRC in my code (python), but I'm facing several dificulties. I tried to implement a CRC algorithm by my own but it's completely bad, a file of 20MB cost me 10+ minutes to calculate the CRC, add to the bitvector and resolve the bitvector.

After that I tried to use some librarys but i dont know how to use them at all. Searching in the web I founded some examples that helped me to calculate the CRC, but i dont know how to add it in the bitvector and resolve.

Can someone help me? I just need some examples of how to use one library or an algorithm that really work to implement.

I tried to use the the crc8, crccheck, crcmod and pycrc libraries.

I'm trying to open a file -> transform into bytes -> calculate the CRC -> send -> recieve -> verify the CRC -> open the file.

My actual code:

import base64
import sys
from os import startfile

def codifica():    
    print("Digite o Nome.extensão ou Diretorio do arquivo:")
    arquivo = input()
    data_input = open(arquivo, "rb")
    data = base64.b64encode(data_input.read())
    data_input.close()

    check = arquivo[0] + arquivo[1] + arquivo[2]
    if(check == 'C:\\'):
        D = arquivo.split('\\')
        auxname = D[len(D) - 1]
        Nome = auxname.split('.')
    else:
         Nome = arquivo.split('.')
    saida = Nome[0] +'LI-FI.' + Nome[1]
    print(saida)
    return data

def decodifica(data):
    new_output = base64.b64decode(data)
    new_data = open('saida.png','wb')
    new_data.write(new_output)
    new_data.close()
    startfile('saida.png')

def crc (dados_str):
    poly_str = '1111110'
    n = len(dados_str)-2
    crc_n = len(poly_str)-1
    dados = int(dados_str,2)
    dados = dados << crc_n