美文网首页
iOS - MD5加密

iOS - MD5加密

作者: th先生 | 来源:发表于2017-10-13 14:05 被阅读0次

    做一个类别吧,以后用来很方便。代码如下:

    .h

    #import <Foundation/Foundation.h>
    
    @interface NSString (Additions)
    
    - (NSString *) MD5;
    
    @end
    

    .m

    #import "NSString+Additions.h"
    #import <sys/xattr.h>
    #import <CommonCrypto/CommonDigest.h>
    
    @implementation NSString (Additions)
    
    - (NSString *) MD5 {
        // Create pointer to the string as UTF8
        const char* ptr = [self UTF8String];
        unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
        
        // Create 16 byte MD5 hash value, store in buffer
        CC_MD5(ptr, (CC_LONG)strlen(ptr), md5Buffer);
        
        // Convert MD5 value in the buffer to NSString of hex values
        NSMutableString* output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
        for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
            [output appendFormat:@"%02x",md5Buffer[i]];
        }
        
        return output;
    }
    @end
    

    相关文章

      网友评论

          本文标题:iOS - MD5加密

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