美文网首页
MD5加密实现

MD5加密实现

作者: 萝BAIBAI | 来源:发表于2015-11-14 01:39 被阅读88次

通过NSString分类的方式实现

NSString+MD5.h

#import <Foundation/Foundation.h>

@interface NSString (MD5)

- (NSString *)MD5;

@end

NSString+MD5.m

#import "NSString+MD5.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (MD5)

- (NSString *)MD5 {
    
    const char * pointer = [self UTF8String];
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(pointer, (CC_LONG)strlen(pointer), md5Buffer);
    
    NSMutableString *string = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [string appendFormat:@"%02x",md5Buffer[i]];
    
    return string;
}

@end

相关文章

网友评论

      本文标题:MD5加密实现

      本文链接:https://www.haomeiwen.com/subject/packhttx.html