objective-c 生成字符串的message digest(md5)

来源:互联网 发布:社交网络 英文缩写 编辑:程序博客网 时间:2024/05/17 02:55

//

//  MD5.h

//  MacEncryptTool

//

//  Created by user on 12-1-17.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import<Foundation/Foundation.h>

#import<commoncrypto/CommonDigest.h>


@interface NSString (MyExtensions)


- (NSString *) md5;


@end




//

//  MD5.mm

//  MacEncryptTool

//

//  Created by user on 12-1-17.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import"MD5.h"


@implementation NSString (MyExtensions)


- (NSString *) md5 {

    const char *cStr = [selfUTF8String];

    unsigned char result[16];

    unsigned int cStrLen = (CC_LONG)strlen(cStr);

    CC_MD5( cStr, cStrLen, result );

    

   NSMutableString *target = [NSMutableStringstring];

    for(int i = 0; i < 16; ++ i) {

        [target appendFormat:@"%02x", result[i]];

    }

    return target;

}


@end