我正在开发一个应用程序,它将文件和该文件的 Adler32 校验和发送到 Web 服务。
在我将文件发送到 Web 服务器后,回答说校验和失败。
这是我用来检查objective-c总和的代码:
//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 & 0x0ffff, b = (ck >> 16) & 0x0ffff;
a = (a + data) % MOD_ADLER;
b = (b + a) % MOD_ADLER;
return (b << 16) | a;
}
uint32_t adler32(const unsigned char *data, int data_len) {
uint64_t ck = 1;
for(int i=0; i<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<data_len; i++) {
a = (a + data[i]) % MOD_ADLER;
b = (b + a) % MOD_ADLER;
}
return (b << 16) | a;
}
而这个是 C# web 服务的代码:
//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);
有谁知道为什么我会得到不同的校验和结果?
Adler32 C#: 701ea682 (1881056898)
Adler32 对象-C: 70100C51 (1880099921)
谢谢!