- Objective-c实现MD5和SHA1算法相对还是比较简单的,可以直接调用系统的C/C++共享库来实现调用
- MD5即Message Digest Algorithm 5(信息-摘要算法 5),用于确保信息传输完整一致。是计算机广泛使用的杂凑算法之一
- SHA即Secure Hash Algorithm(安全散列算法) 是美国国家安全局 (NSA) 设计,美国国家标准与技术研究院 (NIST) 发布的一系列密码散列函数。
- 使用方式如下:
- MD5加密方式
- SString *) md5
- {
- const charchar *cStr = [self UTF8String];
- unsigned char digest[CC_MD5_DIGEST_LENGTH];
- CC_MD5( cStr, 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]];
- return output;
- }
- SHA1加密方式
- - (NSString*) sha1
- {
- const charchar *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
- NSData *data = [NSData dataWithBytes:cstr length:self.length];
- uint8_t digest[CC_SHA1_DIGEST_LENGTH];
- CC_SHA1(data.bytes, data.length, digest);
- NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
- for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
- [output appendFormat:@"%02x", digest[i]];
- return output;
- }
- 当然也可以结合BASE64来使用,这里的BASE64编码使用 GTMBase64实现,需要导入
- - (NSString *) sha1_base64
- {
- const charchar *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
- NSData *data = [NSData dataWithBytes:cstr length:self.length];
- uint8_t digest[CC_SHA1_DIGEST_LENGTH];
- CC_SHA1(data.bytes, data.length, digest);
- NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
- base64 = [GTMBase64 encodeData:base64];
- NSString * output = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
- return output;
- }
- - (NSString *) md5_base64
- {
- const charchar *cStr = [self UTF8String];
- unsigned char digest[CC_MD5_DIGEST_LENGTH];
- CC_MD5( cStr, strlen(cStr), digest );
- NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_MD5_DIGEST_LENGTH];
- base64 = [GTMBase64 encodeData:base64];
- NSString * output = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
- return output;
- }
- 通过拓展NSString,实现完整功能,全部代码
- @interface NSString (encrypto)
- - (NSString *) md5;
- - (NSString *) sha1;
- - (NSString *) sha1_base64;
- - (NSString *) md5_base64;
- - (NSString *) base64;
- @end
- @implementation NSString (encrypto)
- - (NSString*) sha1
- {
- const charchar *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
- NSData *data = [NSData dataWithBytes:cstr length:self.length];
- uint8_t digest[CC_SHA1_DIGEST_LENGTH];
- CC_SHA1(data.bytes, data.length, digest);
- NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
- for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
- [output appendFormat:@"%02x", digest[i]];
- return output;
- }
- -(NSString *) md5
- {
- const charchar *cStr = [self UTF8String];
- unsigned char digest[CC_MD5_DIGEST_LENGTH];
- CC_MD5( cStr, 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]];
- return output;
- }
- - (NSString *) sha1_base64
- {
- const charchar *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
- NSData *data = [NSData dataWithBytes:cstr length:self.length];
- uint8_t digest[CC_SHA1_DIGEST_LENGTH];
- CC_SHA1(data.bytes, data.length, digest);
- NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
- base64 = [GTMBase64 encodeData:base64];
- NSString * output = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
- return output;
- }
- - (NSString *) md5_base64
- {
- const charchar *cStr = [self UTF8String];
- unsigned char digest[CC_MD5_DIGEST_LENGTH];
- CC_MD5( cStr, strlen(cStr), digest );
- NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_MD5_DIGEST_LENGTH];
- base64 = [GTMBase64 encodeData:base64];
- NSString * output = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
- return output;
- }
- - (NSString *) base64
- {
- NSData * data = [self dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
- data = [GTMBase64 encodeData:data];
- NSString * output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- return output;
- }
- @end
- 实现时候不要忘记导入CC相关的库的头文件
- CommonCrypto/CommonDigest.h
iOS开发之Objective-c的MD5/SHA1加密算法的实现
最新推荐文章于 2024-08-27 20:24:56 发布