1. 程式人生 > >ios 自定義蘋果原生方法 md5加密

ios 自定義蘋果原生方法 md5加密

建立好一個自定義的分類

#import <Foundation/Foundation.h>

#import <CommonCrypto/CommonDigest.h>

@interface NSString (MD5)

- (NSString *)md5:(NSString *)input;

@end


實現方法:

#import "NSString+MD5.h"

@implementation NSString (MD5)

- (NSString *)md5:(NSString *)input

{

constchar *cStr = [input

UTF8String];

unsignedchar digest[CC_MD5_DIGEST_LENGTH];

CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call

NSMutableString *output = [NSMutableStringstringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)

        [output appendFormat:@"%02x", digest[i]];

return  output;

}

@end