MD5

由NSString、NSData 生成的MD5字符串

@interface NSString (MD5)
-(NSString*)MD5;

@end
@interface NSData (MD5)
-(NSString*)MD5;
@end
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (MD5)

-(NSString * )MD5{
    if (!self.length) {
        NSLog(@"string not be nil");
        return nil;
    }
    // Create pointer to the string as UTF8
    const char * ptr = [self UTF8String];
    // Create byte array of unsigned chars
    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

@implementation NSData (MD5)

-(NSString * )MD5{
    if (!self.length) {
         NSLog(@"data not be nil");
        return nil;
    }
    // Create byte array of unsigned chars
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
     // Create 16 byte MD5 hash value, store in buffer
    CC_MD5(self.bytes, (CC_LONG)self.length, md5Buffer);
     // Convert unsigned char 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


   转载规则


《MD5》 志鹏 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
CFBundleName等关键字解析 CFBundleName等关键字解析
CFBundleName: CFBundleName指定了该束的简称。简称应该小于16个字符并且适合在菜单和“关于”中显示。通过把它加入到适当的.lproj子文件夹下的InfoPlist.strings文件中,该关键字可以被本地化。如果您本
2016-07-28 志鹏
下一篇 
python基础之标识符 python基础之标识符
标识符 标识符是计算机语言中允许用为名字的有效字符串集合。有一部分是关键字,构成语言的标识符。这样的标识符是保留字,不能用于其他用途,否则会引起语法错误。python中的“内建”标识符也不推荐使用。 合法的标识符 第一个字符必须是字母或下
2016-07-14 志鹏
  目录