iOS开发之Objective-c的SHA224/SHA256/SHA384/SHA512加密算法的实现

参考链接 http://www.lidaren.com/archives/1466

之前在实现的SHA1的安全性已经满足不了用户需求,今天把SHA224/SHA256/SHA384/SHA512的实现一并附上。

SHA即Secure Hash Algorithm(安全散列算法)有多种不同位数的实现,常见的有SHA224/SHA256/SHA384/SHA512等

SHA224:

iOS开发之Objective-c的SHA224/SHA256/SHA384/SHA512加密算法的实现_第1张图片

-(NSString*)sha224{constchar*cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];NSData*data=[NSDatadataWithBytes:cstr length:self.length];  uint8_t digest[CC_SHA224_DIGEST_LENGTH];  CC_SHA224(data.bytes, data.length, digest);NSMutableString*output=[NSMutableStringstringWithCapacity:CC_SHA224_DIGEST_LENGTH*2];for(inti=0; i < CC_SHA224_DIGEST_LENGTH; i++)[output appendFormat:@"%02x", digest[i]];returnoutput;}

SHA256:


iOS开发之Objective-c的SHA224/SHA256/SHA384/SHA512加密算法的实现_第2张图片

-(NSString*)sha256{constchar*cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];NSData*data=[NSDatadataWithBytes:cstr length:self.length];  uint8_t digest[CC_SHA256_DIGEST_LENGTH];  CC_SHA256(data.bytes, data.length, digest);NSMutableString*output=[NSMutableStringstringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];for(inti=0; i < CC_SHA256_DIGEST_LENGTH; i++)[output appendFormat:@"%02x", digest[i]];returnoutput;}


iOS开发之Objective-c的SHA224/SHA256/SHA384/SHA512加密算法的实现_第3张图片

-(NSString*)sha384{constchar*cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];NSData*data=[NSDatadataWithBytes:cstr length:self.length];  uint8_t digest[CC_SHA384_DIGEST_LENGTH];  CC_SHA384(data.bytes, data.length, digest);NSMutableString*output=[NSMutableStringstringWithCapacity:CC_SHA384_DIGEST_LENGTH*2];for(inti=0; i < CC_SHA384_DIGEST_LENGTH; i++)[output appendFormat:@"%02x", digest[i]];returnoutput;}


iOS开发之Objective-c的SHA224/SHA256/SHA384/SHA512加密算法的实现_第4张图片

-(NSString*)sha512{constchar*cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];NSData*data=[NSDatadataWithBytes:cstr length:self.length];  uint8_t digest[CC_SHA512_DIGEST_LENGTH];  CC_SHA512(data.bytes, data.length, digest);NSMutableString*output=[NSMutableStringstringWithCapacity:CC_SHA512_DIGEST_LENGTH*2];for(inti=0; i < CC_SHA512_DIGEST_LENGTH; i++)[output appendFormat:@"%02x", digest[i]];returnoutput;}


总结

网上整理 格式有点乱

////NSString+SHA.h//#import #import #import @interfaceNSString(SHA)-(NSString*)sha1;-(NSString*)sha224;-(NSString*)sha256;-(NSString*)sha384;-(NSString*)sha512;@end////NSString+SHA.m//#import "NSString+SHA.h"@implementationNSString(SHA)-(NSString*)sha1{constchar*cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];NSData*data=[NSDatadataWithBytes:cstr length:self.length];  uint8_t digest[CC_SHA1_DIGEST_LENGTH];  CC_SHA1(data.bytes, data.length, digest);NSMutableString*output=[NSMutableStringstringWithCapacity:CC_SHA1_DIGEST_LENGTH*2];for(inti=0; i < CC_SHA1_DIGEST_LENGTH; i++)[output appendFormat:@"%02x", digest[i]];returnoutput;}-(NSString*)sha224{constchar*cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];NSData*data=[NSDatadataWithBytes:cstr length:self.length];  uint8_t digest[CC_SHA224_DIGEST_LENGTH];  CC_SHA224(data.bytes, data.length, digest);NSMutableString*output=[NSMutableStringstringWithCapacity:CC_SHA224_DIGEST_LENGTH*2];for(inti=0; i < CC_SHA224_DIGEST_LENGTH; i++)[output appendFormat:@"%02x", digest[i]];returnoutput;}-(NSString*)sha256{constchar*cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];NSData*data=[NSDatadataWithBytes:cstr length:self.length];  uint8_t digest[CC_SHA256_DIGEST_LENGTH];  CC_SHA256(data.bytes, data.length, digest);NSMutableString*output=[NSMutableStringstringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];for(inti=0; i < CC_SHA256_DIGEST_LENGTH; i++)[output appendFormat:@"%02x", digest[i]];returnoutput;}-(NSString*)sha384{constchar*cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];NSData*data=[NSDatadataWithBytes:cstr length:self.length];  uint8_t digest[CC_SHA384_DIGEST_LENGTH];  CC_SHA384(data.bytes, data.length, digest);NSMutableString*output=[NSMutableStringstringWithCapacity:CC_SHA384_DIGEST_LENGTH*2];for(inti=0; i < CC_SHA384_DIGEST_LENGTH; i++)[output appendFormat:@"%02x", digest[i]];returnoutput;}-(NSString*)sha512{constchar*cstr=[self cStringUsingEncoding:NSUTF8StringEncoding];NSData*data=[NSDatadataWithBytes:cstr length:self.length];  uint8_t digest[CC_SHA512_DIGEST_LENGTH];  CC_SHA512(data.bytes, data.length, digest);NSMutableString*output=[NSMutableStringstringWithCapacity:CC_SHA512_DIGEST_LENGTH*2];for(inti=0; i < CC_SHA512_DIGEST_LENGTH; i++)[output appendFormat:@"%02x", digest[i]];returnoutput;}

你可能感兴趣的:(iOS开发之Objective-c的SHA224/SHA256/SHA384/SHA512加密算法的实现)