1. 程式人生 > >iOS開發-MD5加密、SHA1加密

iOS開發-MD5加密、SHA1加密

digest ons size encoding gen nco inpu style data

1.MD5加密

///MD5加密

+ (NSString *)md5:(NSString *)inputString{

const char *cStr = [inputString UTF8String];

unsigned char digest[CC_MD5_DIGEST_LENGTH];

CC_MD5( cStr, (int)strlen(cStr), digest );

NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)

[output appendFormat:@"%02x", digest[i]];

///轉變成大寫

NSString *putString = [output uppercaseString];

return putString;

}

1.SHA1加密

///sha1加密

+(NSString *)sha1:(NSString *)inputString{

const char *cstr = [inputString cStringUsingEncoding:NSUTF8StringEncoding];

NSData *data = [NSData dataWithBytes:cstr length:inputString.length];

uint8_t digest[CC_SHA1_DIGEST_LENGTH];

CC_SHA1(data.bytes, (CC_LONG)data.length, digest);

NSMutableString* result = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {

[result appendFormat:@"%02x", digest[i]];

}

return result;

}

iOS開發-MD5加密、SHA1加密