6

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

using this example what is the

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]

7 Answers 7

33

I just spent like 4 hours Googling and looking for ways to calculate an unkeyed SHA1 on the iPhone that would match the results of the sha1() function in php. Here was 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

Hopefully this will help others who are struggling with SHA1 on the iPhone

3
  • this is so awesome! it only took me 30 mins to an hour or so of googling to find this (as opposed to 4) so thanks a ton!! :)
    – taber
    Oct 20, 2010 at 1:50
  • 3
    Definitely works :-) - but isn't there a more elegant way to turn NSData into NSString?
    – Mat Ryer
    May 29, 2011 at 12:25
  • see stackoverflow.com/questions/1305225/… @MatRyer
    – MrJre
    Jun 21, 2013 at 9:34
5

If you are calling the Amazon web service too look up prices or product details, your Amazon web service key will be disabled and your app will stop working.

Look at the terms of service of the Amazon Web Services, use by mobile clients is strictly disallowed:

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

I found this out the hard way when my own application had my AWS key disabled in a production app. I had read the TOS, but it was not really there as you can see by the link above to some other obscure detail of use. You wouldn't think the affiliate program would have anything to do with the API, but it does.

You can find details of other apps blocked at this TechCrunch article:

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

Just giving you a heads up and hopefully saving you a lot of work.

3
  • Where does it say "use by mobile clients is strictly disallowed" aws.amazon.com/agreement?
    – jeff7091
    Nov 8, 2009 at 9:36
  • I know from the actual experience of using the AWS and having Amazon revoke my key from a production app. I managed to find the clause once, but I can't find it again - annoyingly it was not in the AWS TOS you linked to (I had read through that before I even started developing) Nov 8, 2009 at 23:06
  • I added links with more detail proving my point. Anyone care to remove the downvote since I am in fact being helpful and not misleading after all? Nov 9, 2009 at 1:35
2
// This is my code used in my Twitter connection, and working well for me.
// KeithF's 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
4
  • I implemented your method kind of. I used SHA512 instead. Now my question is how to send the authentication hash to the Server. I initialized an NSString with the data of the hash. But it consists of so many upside down question marks, that I don't believe that this will turn out well. Can I send the raw data? Dec 7, 2012 at 10:02
  • @Julian encrypted data is binary by default. You have to be able to send binary data. This means your communication protocol should support binary data transferring. If it supports only text data (such as plain HTTP headers), you need to encode binary data into string. Trivial choice is BASE64 or hex-code encoding which is well-defined, and widely used.
    – eonil
    Dec 7, 2012 at 15:43
  • Some languages lacks ability to handle binary data usually returns Any implementation which returns string instead of binary is actually performs that encoding before return. Anyway they usually defines how they encode it, so refer the language specification for specific encoding method.
    – eonil
    Dec 7, 2012 at 15:45
  • Thanks! Since PHP's hash_hmac function returns HEX as default, I decided to HEX-Encode the data. I totally forgot about binary to string conversion… :)) Thanks Dec 7, 2012 at 15:56
0

Take a look at CocoaCryptoHashing for the SHA1 encoding

0

I posted one solution to this here, that returns the Base64 encoded data that AWS requests.

0

Apple's iOS developer library has provided an excellent sample titled CryptoExercise which includes a simple function:

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

You can see this maybe it helps you.

1
  • Rather than just providing a link, it would be preferable to include the essential parts of the answer here, and just provide the link for additional reference. If you're not up to this task, you should consider simply leaving a comment on the question instead of posting an answer. Mar 11, 2014 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.