建立好一个自定义的分类
#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
{
const char *cStr = [input UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
}
@end
本文介绍了一种在Objective-C中实现字符串MD5加密的方法。通过扩展NSString类,该方法可以方便地对任意输入字符串进行MD5转换,并返回转换后的十六进制字符串。这种实现方式适用于iOS和其他Objective-C应用开发场景。
502

被折叠的 条评论
为什么被折叠?



