title: different adler32 check sum on objective-c and C#
body: <p>I'm developing an app in objective-c that send a file and a Adler32 check sum of that file to a web service.</p>
  
  <p>After I send the file to the web server answers saying that the check sum failed.</p>
  
  <p>This is the code that I use to check sum on objective-c:</p>
  
  <pre><code>//Get Asset NSData
  ALAssetRepresentation *rep = [[p objectForKey:@"assetPath"] defaultRepresentation];
  Byte *buffer = (Byte*)malloc(rep.size);
  NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
  NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
  
  //I also need a path to the file so I have to save the NSData to a local file
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"png"];
  [data writeToFile:localFilePath atomically:YES];
  NSString *path = localFilePath;
  
  //Now call the Adler
  NSLog(@"checksum_fast:%@", [NSString stringWithFormat:@"%u", adler32_fast([data bytes], [data length])]);
  NSLog(@"checksum:%@", [NSString stringWithFormat:@"%u", adler32([data bytes], [data length])]);
  
  //Adler Lib
  #define MOD_ADLER 65521
  #define BUFFER_SIZE 1000
  
  uint32_t adler32_update(uint32_t ck, const unsigned char data) {
    uint32_t a = ck &amp; 0x0ffff, b = (ck &gt;&gt; 16) &amp; 0x0ffff;
    a = (a + data) % MOD_ADLER;
    b = (b + a) % MOD_ADLER;
    return (b &lt;&lt; 16) | a;
  }
  
  uint32_t adler32(const unsigned char *data, int data_len) {
    uint64_t ck = 1;
    for(int i=0; i&lt;data_len; i++) {
      ck = adler32_update(ck,data[i]);
    }
    return ck;
  }
  
  uint32_t adler32_fast(const unsigned char *data, int data_len) {
    uint32_t a = 1, b = 0;
    for(int i=0; i&lt;data_len; i++) {
      a = (a + data[i]) % MOD_ADLER;
      b = (b + a) % MOD_ADLER;
    }
    return (b &lt;&lt; 16) | a;
  }
  </code></pre>
  
  <p>And this one is the code of the C# web service:</p>
  
  <pre><code>//It use ICSharpCode.SharpZipLib.dll    
  byte[] buff = null;
  FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
  BinaryReader br = new BinaryReader(fs);
  long numBytes = new FileInfo(path).Length;
  buff = br.ReadBytes((int)numBytes);
  
  Adler32 adler = new Adler32();
  adler.Reset();
  adler.Update(buff, 0, buff.Length);
  fs.Close();
  fs.Dispose();
  br.Close();
  buff = null;
  return Convert.ToString(adler.Value, 16);
  </code></pre>
  
  <p>Do any one knows why I'm getting to different check sum results?</p>
  
  <p>Testing file: <a href="http://we.tl/gjqSj43238" rel="nofollow">http://we.tl/gjqSj43238</a></p>
  
  <p>Adler32 C#: 701ea682 (1881056898)</p>
  
  <p>Adler32 Obj-C: 70100C51 (1880099921)</p>
  
  <p>Thanks!</p>