NSString+MD5Addition.m 803 B

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // NSString+MD5Addition.m
  3. // UIDeviceAddition
  4. //
  5. // Created by Georg Kitz on 20.08.11.
  6. // Copyright 2011 Aurora Apps. All rights reserved.
  7. //
  8. #import "NSString+MD5Addition.h"
  9. #import <CommonCrypto/CommonDigest.h>
  10. @implementation NSString(MD5Addition)
  11. - (NSString *) stringFromMD5
  12. {
  13. if(self == nil || [self length] == 0)
  14. return nil;
  15. const char *value = [self UTF8String];
  16. unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
  17. CC_MD5(value, strlen(value), outputBuffer);
  18. NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
  19. for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
  20. [outputString appendFormat:@"%02x",outputBuffer[count]];
  21. }
  22. return outputString ;
  23. }
  24. @end