title: SHA1 hashes not matching between my Rails and Cocoa apps
body: <p>I have a Cocoa app send some data along with a SHA1 hash of that data to a Rails app which verifies that the data and the hash match, but they do not.</p>
  
  <p>To be sure I have logged hex versions of the data that is hashed to the console at both the Rails and Cocoa sides, and they match exactly.</p>
  
  <p>Here's the Cocoa part:</p>
  
  <pre><code>#import &lt;CommonCrypto/CommonDigest.h&gt;
  
  //...
  
  - (NSData *)dataOfSHA1Hash
  {
      unsigned char hashBytes[CC_SHA1_DIGEST_LENGTH];
      CC_SHA1([self bytes], CC_SHA1_DIGEST_LENGTH, hashBytes);
  
      return [NSData dataWithBytes:hashBytes length:CC_SHA1_DIGEST_LENGTH];
  }
  
  //...
  
  NSData *signatureData = [signedData dataOfSHA1Hash];
  NSString *signature = [signatureData hexadecimalString];
  </code></pre>
  
  <p>..and Rails:</p>
  
  <pre><code>Digest::SHA1.hexdigest(signed_data)
  </code></pre>
  
  <p>Note that -hexadecimalString is a custom extension to NSData (didn't seem to be built in), which I've tested. That method does not seem to be the problem.</p>
  
  <p>So, why don't my SHA1 hashes match on the same data? Any ideas?</p>
  
  <p><strong>Edit: example</strong></p>
  
  <p>For the string "Hello World!"</p>
  
  <pre>Cocoa: f98ee9c814c2f3c66ccdca641298d12cd26741ec
  Rails: 2ef7bde608ce5404e97d5f042f95f89f1c232871</pre>
  
  <p>Cocoa:</p>
  
  <pre><code>NSMutableData *signedData = [[NSMutableData alloc] init];
  [signedData appendData:[@"Hello World!" dataUsingEncoding:NSUTF8StringEncoding]];
  
  NSData *signatureData = [signedData dataOfSHA1Hash];
  [signedData release];
  NSString *signature = [signatureData hexadecimalString];
  
  NSLog(@"Signature: %@", signature);
  // output: Signature: f98ee9c814c2f3c66ccdca641298d12cd26741ec
  </code></pre>
  
  <p>Ruby:</p>
  
  <pre><code>&gt;&gt; Digest::SHA1.hexdigest("Hello World!")
  =&gt; "2ef7bde608ce5404e97d5f042f95f89f1c232871"
  </code></pre>