Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 0.62 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Objective-C SHA2 hash not working correctly with non-ASCII
  2. -(NSString*) sha512:(NSString*)input
  3. {
  4.     const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
  5.     NSData *data = [NSData dataWithBytes:cstr length:input.length];
  6.  
  7.     uint8_t digest[CC_SHA512_DIGEST_LENGTH];
  8.  
  9.     CC_SHA512(data.bytes, data.length, digest);
  10.  
  11.     NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
  12.  
  13.     for (int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++) {
  14.         [output appendFormat:@"%02x", digest[i]];
  15.     }
  16.  
  17.     return output;
  18. }
  19.        
  20. NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];