iOS SHA512 Hex String

Here's an Objective C method that'll return a SHA512 hex of a given string:

  1.  
  2. -(NSString*) sha512:(NSString*)input {
  3. const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
  4. NSData *data = [NSData dataWithBytes:cstr length:input.length];
  5.  
  6. uint8_t digest[CC_SHA512_DIGEST_LENGTH];
  7.  
  8. CC_SHA512(data.bytes, data.length, digest);
  9.  
  10. NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
  11.  
  12. for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
  13. [output appendFormat:@"%02x", digest[i]];
  14.  
  15. return output;
  16.  
  17. }
  18.  

You'll need to include:

  1.  
  2. #include <CommonCrypto/CommonDigest.h>
  3.  

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>