简体   繁体   中英

Can't get MD5 checksum from security framework on Mac OS X Mountain Lion

    NSString *curFourChanFilePath = [currentSubFile stringByAppendingPathComponent:curFourChanFile];

    NSData *imageData = [NSData dataWithContentsOfFile:curFourChanFilePath];

    CFErrorRef theError;

    SecTransformRef testTransform = SecDigestTransformCreate(kSecDigestMD5,0,&theError);

    CFDataRef theDataRef = (__bridge CFDataRef)imageData;

    SecTransformSetAttribute(testTransform, kSecTransformInputAttributeName, theDataRef, &theError);

    NSData *resultingData = (__bridge NSData *)(SecTransformExecute(testTransform, &theError));

    NSString *resultingString = [[NSString alloc] initWithData:resultingData encoding:NSUTF8StringEncoding];

    NSLog(@"%@",resultingString);

    [checksumMapTable setObject:resultingData forKey:curFourChanFile];

Here's the code I'm having issues with. This code is in a nested nested loop, and all the code works fine till it get to turning the data into an NSString. It seems to have trouble with UTF8. All the strings turn into (null), but the strange thing is, it isn't (null). When I change the encoding to UTF16 or UTF32, I get text. Not readable text, it's all garbled as you'd expect from using the wrong encoding, but it's clearly there, I just can't seem to get at it in what I thought was the proper encoding, UTF8. Any help would be appreciated. Just to reiterate, again, all the code seems to be working fine until this point. The Security framework is still a bit new to me.

Actually, I just answered my own question. using SecEncodeTransformCreate(NULL, NULL) and sending the data through that, I got the checksum. Problem solved.

Change your encoding to one that can decode any byte value. For example NSISOLatin1StringEncoding . This should give you an output similar to openssl md5 -binary .

Not that the result will make much sense though…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM