Search code examples
macosapp-storereceipt-validation

Mac App Store Receipt - Hash not matching. Why?


I'm trying to create a Mac application, and be gentle on me as this is my first attempt! Essentially transferring what I've done on iOS to the Mac and it's almost ready to go with one part remaining - App Store verification to prevent people from being able to copy it.

I have receipt verification code running in a couple of my iOS apps and they work find for me (for in-app purchase). With the Mac I simply want to verify the receipt and hope that's enough. Here is the basis of the code I have created:

char buf[512] = "";
get_platform_uuid(buf, sizeof(buf));

NSString *uuidString = [NSString stringWithUTF8String:buf];
NSUUID *myUUID = [[NSUUID UUID] initWithUUIDString:uuidString];

unsigned char uuidBytes[16];
[myUUID getUUIDBytes:uuidBytes];

NSMutableData *input = [NSMutableData data];
[input appendBytes:uuidBytes length:16];
[input appendData:[receipt objectForKey:kReceiptOpaqueValue]];
[input appendData:[receipt objectForKey:kReceiptBundleIdentifierData]];

NSMutableData *hash = [NSMutableData dataWithLength:SHA_DIGEST_LENGTH];
SHA1([input bytes], [input length], [hash mutableBytes]);

NSLog(@"kReceiptHash: %@", [receipt objectForKey:kReceiptHash]);
NSLog(@"hash: %@", hash);

The last two NSLog commands are just so that I can log this during debug, but the hash values would be compared. This is where I know the two are incorrect. "get_platform_uuid" is a function I've found for getting the UUID for the Mac and it seems to work fine.

I've taken the entire "input" variable and plugged the information in to it and ran it on iOS, and I've done the same from the iOS device and run it on the Mac. In both cases the calculated Hash is the same; I think the calculation is fine.

But on the Mac it is still different from the receipt hash. The iOS calculation and receipt hash matches for that app.

I've set up the App on iTunes Connect, it seems to pick up the right bundle identifier. I have logged out of the App Store on the Mac. I copy the App out from where Xcode builds the app to a different directory to run it. It then asks me to log in, which I do with a test account. Then repeatedly tries to rerun the app and exits (I copy it back to the debug folder and this breaks that rerunning cycle).

I've got the certificates set up for development and distribution for Mac set up in the Apple Developer site but I'm sure it's something that I've not got right with App permissions or how I'm running the app. But what I just can't figure out (and I have looked - for a long time!).

I'm sure it's something silly, but as a lone developer my only hope is to ask you what am I doing wrong?

Thanks.


Solution

  • After much searching I found another method to build and compare the hash value. But this also needed modification to run on the Mac as it was quite old code. What I have now is a hybrid between this method and it.

    What I can't explain is why the old code works on iOS but not on the Mac and I'm not a good enough programmer to look in to why. All I know is this hybrid method seems to be working.