NSString+VIMD5.m 615 B

123456789101112131415161718192021222324252627
  1. //
  2. // NSString+VIMD5.m
  3. // VIMediaCacheDemo
  4. //
  5. // Created by Vito on 21/11/2017.
  6. // Copyright © 2017 Vito. All rights reserved.
  7. //
  8. #import "NSString+VIMD5.h"
  9. #import <CommonCrypto/CommonDigest.h>
  10. @implementation NSString (VIMD5)
  11. - (NSString *)vi_md5 {
  12. const char* str = [self UTF8String];
  13. unsigned char result[CC_MD5_DIGEST_LENGTH];
  14. CC_MD5(str, (CC_LONG)strlen(str), result);
  15. NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
  16. for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
  17. [ret appendFormat:@"%02x",result[i]];
  18. }
  19. return ret;
  20. }
  21. @end