How to calculate 32 bit CRC in Ruby on rails?
You could use Ruby’s Zlib module. require ‘zlib’ crc32 = Zlib::crc32(‘input field value’)
You could use Ruby’s Zlib module. require ‘zlib’ crc32 = Zlib::crc32(‘input field value’)
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.
Dan Story and ergosys provided good answers (apparently I was looking in the wrong place, that’s why the headaches) but while I’m at it I wanted to provide a copy&paste solution for the function in my question for future googlers: #include <boost/crc.hpp> uint32_t GetCrc32(const string& my_string) { boost::crc_32_type result; result.process_bytes(my_string.data(), my_string.length()); return result.checksum(); }
Duplicate of Expected collisions for perfect 32bit crc The answer referenced this article: http://arstechnica.com/civis/viewtopic.php?f=20&t=149670 Found the image below from: http://preshing.com/20110504/hash-collision-probabilities
It’s not a research topic. It’s really well understood: http://en.wikipedia.org/wiki/Cyclic_redundancy_check The math is pretty simple. An 8-bit CRC boils all messages down to one of 256 values. If your message is more than a few bytes long, the possibility of multiple messages having the same hash value goes up higher and higher. A 16-bit CRC, … Read more
There are several details you need to ‘match up’ with for a particular CRC implementation – even using the same polynomial there can be different results because of minor differences in how data bits are handled, using a particular initial value for the CRC (sometimes it’s zero, sometimes 0xffff), and/or inverting the bits of the … Read more
I suggest you have a look to this SO page, CRC vs MD5/SHA1. Speed and collisions are discussed in this other thread. And as always Wikipedia is your friend. If I had to choose, there is an important question to answer: do you want that in any case there is no collision – or, at … Read more
Unless you’re using a really complicated and/or slow hash, loading the data from the disk is going to take much longer than computing the hash (unless you use RAM disks or top-end SSDs). So to compare two files, use this algorithm: Compare sizes Compare dates (be careful here: this can give you the wrong answer; … Read more
CRC works fine for detecting random errors in data that might occur, for example, from network interference, line noise, distortion, etc. CRC is computationally much less complex than MD5 or SHA1. Using a hash function like MD5 is probably overkill for random error detection. However, using CRC for any kind of security check would be … Read more