Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compute crc of file in python

Tags:

python

hash

crc

I want to calculate the CRC of file and get output like: E45A12AC. Here's my code:

#!/usr/bin/env python  import os, sys import zlib  def crc(fileName):     fd = open(fileName,"rb")     content = fd.readlines()     fd.close()     for eachLine in content:         zlib.crc32(eachLine)  for eachFile in sys.argv[1:]:     crc(eachFile) 

This calculates the CRC for each line, but its output (e.g. -1767935985) is not what I want.

Hashlib works the way I want, but it computes the md5:

import hashlib m = hashlib.md5() for line in open('data.txt', 'rb'):     m.update(line) print m.hexdigest() 

Is it possible to get something similar using zlib.crc32?

like image 499
user203547 Avatar asked Nov 16 '09 15:11

user203547


People also ask

How do I calculate the CRC of a file?

Right-click the file you wish to get the CRC-32 for. A context menu appears. Select the CRC SHA submenu entry. Select any of the available algorithms: CRC-32, CRC-64, SHA-1 or SHA-256 to calculate the respective checksum, or select "*" to calculate all of them and additionally BLAKE2sp.

How does Python calculate CRC checksum?

crc32() method, we can compute the checksum for crc32 (Cyclic Redundancy Check) to a particular data. It will give 32-bit integer value as a result by using zlib. crc32() method. Return : Return the unsigned 32-bit checksum integer.

How do I manually calculate CRC?

A CRC is pretty simple; you take a polynomial represented as bits and the data, and divide the polynomial into the data (or you represent the data as a polynomial and do the same thing). The remainder, which is between 0 and the polynomial is the CRC.

What is crc32 Python?

crc32 computes CRC-32 on binary text data. By definition, CRC-32 refers to the 32-bit checksum of any piece of data. This method is used to compute 32-bit checksum of provided data. This algorithm is not a general hash algorithm.


1 Answers

A little more compact and optimized code

def crc(fileName):     prev = 0     for eachLine in open(fileName,"rb"):         prev = zlib.crc32(eachLine, prev)     return "%X"%(prev & 0xFFFFFFFF) 

PS2: Old PS is deprecated - therefore deleted -, because of the suggestion in the comment. Thank you. I don't get, how I missed this, but it was really good.

like image 184
kobor42 Avatar answered Oct 08 '22 22:10

kobor42