IPhone and encoding HMAC-SHA-1

im trying to get a call to the amazon web service and i'm stuck on getting a signature, looked at it but i still have a question.

using this example what is

NSData *keyData; NSData *clearTextData 

? what do i need to pass for these two values?

 /* inputs: NSData *keyData; NSData *clearTextData */ uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0}; CCHmacContext hmacContext; CCHmacInit(&hmacContext, kCCHmacAlgSHA1, keyData.bytes, keyData.length); CCHmacUpdate(&hmacContext, clearTextData.bytes, clearTextData.length); CCHmacFinal(&hmacContext, digest); NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH] 
+6
iphone hmac sha1
source share
7 answers

I just spent like 4 hours googling and was looking for ways to calculate unkeyed SHA1 on an iPhone that would match the results of the sha1 () function in php. Here is the result:

  #import <CommonCrypto/CommonDigest.h> NSString *hashkey = <your data here>; // PHP uses ASCII encoding, not UTF const char *s = [hashkey cStringUsingEncoding:NSASCIIStringEncoding]; NSData *keyData = [NSData dataWithBytes:s length:strlen(s)]; // This is the destination uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0}; // This one function does an unkeyed SHA1 hash of your hash data CC_SHA1(keyData.bytes, keyData.length, digest); // Now convert to NSData structure to make it usable again NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH]; // description converts to hex but puts <> around it and spaces every 4 bytes NSString *hash = [out description]; hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""]; hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""]; hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""]; // hash is now a string with just the 40char hash value in it 

Hope this helps others struggling with SHA1 on the iPhone.

+33
source share

If you call the Amazon web service, look at prices or product information, your Amazon web service will be disconnected and your application will stop working.

Look at the Amazon Web Services Terms of Service, using mobile clients is strictly prohibited:

https://affiliate-program.amazon.com/gp/advertising/api/detail/agreement.html

I found this with difficulty when my own application turned off my AWS key in the production application. I read the TOS, but actually it was not like you can see from the link above, with some other obscure usage details. You do not think that the affiliate program will have anything to do with the API, but it is.

You can find information about other applications blocked in this TechCrunch article:

http://www.techcrunch.com/2009/07/07/amazon-killing-mobile-apps-that-use-its-data/

Just give you a head and hopefully saving you a lot of work.

+5
source share
 // This is my code used in my Twitter connection, and working well for me. // KeithF code was a big help! // // This is a category added to NSData. @implementation NSData (EOUtil) - (NSData*)dataByHmacSHA1EncryptingWithKey:(NSData*)key { void* buffer = malloc(CC_SHA1_DIGEST_LENGTH); CCHmac(kCCHmacAlgSHA1, [key bytes], [key length], [self bytes], [self length], buffer); return [NSData dataWithBytesNoCopy:buffer length:CC_SHA1_DIGEST_LENGTH freeWhenDone:YES]; } @end 
+2
source share

See CocoaCryptoHashing for SHA1 Encoding

0
source share

I have posted one solution for this here that returns the Base64 encoded data that AWS requests.

0
source share

The Apple iOS Developer Library provided an excellent sample called CryptoExercise, which includes a simple function:

 - (NSData *)getHashBytes:(NSData *)plainText" to get a SHA-1 hash. 
0
source share

You can see it is possible, it will help you.

-one
source share

All Articles