iOS MD5加密

.h文件

/**
 - MD5加密
 */
//头文件
#import 

@interface iOSMD5 : NSObject {

}

/**
 MD5加密
 */
+(NSString *) md5: (NSString *) inPutText ;
/**
 md5加撒盐加密
 */
+ (NSString *) md5Salt:(NSString *) inPutText;
@end

.m文件

//.m文件
#import "iOSMD5.h"
#import "CommonCrypto/CommonDigest.h"

@implementation MyMD5

+(NSString *) md5: (NSString *) inPutText
{
    const char *cStr = [inPutText UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, strlen(cStr), result);

    return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3],
             result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],
             result[12], result[13], result[14], result[15]
             ] lowercaseString];
}

+ (NSString *) md5Salt:(NSString *) inPutText{
    NSMutableString *md5Str = [NSMutableString stringWithFormat:[iOSMD5 md5: inPutText]];
    NSRange range1 = {arc4random()%5,arc4random()%6};
    NSRange range2 = {arc4random()%5 + 4,arc4random()%6};
    NSString *str1 = [md5Str substringWithRange:range1];
    NSString *str2 = [md5Str substringWithRange:range2];
    [md5Str replaceCharactersInRange:range2 withString:str1];
    [md5Str replaceCharactersInRange:range1 withString:str2];
    return md5Str;
}
@end

你可能感兴趣的