#include "crc32.h" namespace Multihash { int* crc_table = 0; void generate_table() { int crc; crc_table = new int[256]; for(int i = 0; i < 256; i++) { crc = i << 24; for(int j = 0; j < 8; j++) { if(crc & 0x80000000) { crc = (crc << 1) ^ 0x04c11db7; } else { crc = crc << 1; } } crc_table[i] = crc; } } int reflect(int data, int bits) { int x = 0; for(int i = 0; i < bits; i++) { x = x << 1; x |= data & 1; data = data >> 1; } return x; } CRC32::CRC32(Hash* n) : Hash(n) { if(!crc_table) { generate_table(); } crc_ctx = 0; } void CRC32::hash_update(const char* data, int length) { int crc = ~reflect(crc_ctx, 32); for (int i = 0; i < length; i++) { crc = (crc << 8) ^ crc_table[((crc >> 24) ^ reflect(data[i], 8)) & 0xff]; } crc_ctx = ~reflect(crc, 32); } std::string CRC32::hash_digest() { return Hex::hex(crc_ctx); } }