6

Possible Duplicate:
Get CRC checksum of an NSData in Objective-C

I can't find any implementation of CRC32 algoryghm in xcode. Can anybody help me calculate it?

Community
  • 1
  • 1
Funky Kat
  • 61
  • 1
  • 4

1 Answers1

16

libz has a crc32() function. To use it with NSData, try this simple category:

Your header:

@interface NSData (CRC32)
- (uint32_t)CRC32Value;
@end

Your implementation:

#include "your header"

#include <zlib.h>

@implementation NSData (CRC32)
- (uint32_t)CRC32Value {
    uLong crc = crc32(0L, Z_NULL, 0);
    crc = crc32(crc, [self bytes], [self length]);
    return crc;
}
@end
Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • 1
    Is there a way to do this for streaming data? Can I just repeatedly call crc32() when I receive data. I would like to do this for an NSURLConnectionDelegate to checksum the results of large downloads streamed directly to disk. – StCredZero Dec 12 '12 at 20:48
  • 1
    See the documentation for `crc32()`. Effectively, yes. – Jonathan Grynspan Dec 13 '12 at 13:43