ios開發——Objective-c的SHA224/SHA256/SHA384/SHA512密碼編譯演算法的實現__演算法

來源:互聯網
上載者:User

之前在博文中實現的SHA1的安全性已經滿足不了使用者需求,今天把SHA224/SHA256/SHA384/SHA512的實現一併附上。 SHA即Secure Hash Algorithm(安全散列演算法)有多種不同位元的實現,常見的有SHA224/SHA256/SHA384/SHA512等 

SHA224:

 
- (NSString*) sha224{  const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];  NSData *data = [NSData dataWithBytes:cstr length:self.length];   uint8_t digest[CC_SHA224_DIGEST_LENGTH];   CC_SHA224(data.bytes, data.length, digest);   NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA224_DIGEST_LENGTH * 2];   for(int i = 0; i < CC_SHA224_DIGEST_LENGTH; i++)    [output appendFormat:@"%02x", digest[i]];   return output;}

SHA256:

 
- (NSString*) sha256{  const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];  NSData *data = [NSData dataWithBytes:cstr length:self.length];   uint8_t digest[CC_SHA256_DIGEST_LENGTH];   CC_SHA256(data.bytes, data.length, digest);   NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];   for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++)    [output appendFormat:@"%02x", digest[i]];   return output;}

SHA384:

 
- (NSString*) sha384{  const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];  NSData *data = [NSData dataWithBytes:cstr length:self.length];   uint8_t digest[CC_SHA384_DIGEST_LENGTH];   CC_SHA384(data.bytes, data.length, digest);   NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA384_DIGEST_LENGTH * 2];   for(int i = 0; i < CC_SHA384_DIGEST_LENGTH; i++)    [output appendFormat:@"%02x", digest[i]];   return output;}

SHA512:

 
- (NSString*) sha512{  const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];  NSData *data = [NSData dataWithBytes:cstr length:self.length];   uint8_t digest[CC_SHA512_DIGEST_LENGTH];   CC_SHA512(data.bytes, data.length, digest);   NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];   for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)    [output appendFormat:@"%02x", digest[i]];   return output;}

放到一起大團結

 
////NSString+SHA.h// #import <Foundation/Foundation.h>#import <CommonCrypto/CommonDigest.h>#import <CommonCrypto/CommonCryptor.h> @interface NSString(SHA)-(NSString *) sha1;-(NSString *) sha224;-(NSString *) sha256;-(NSString *) sha384;-(NSString *) sha512;@end ////NSString+SHA.m// #import "NSString+SHA.h" @implementation NSString(SHA) - (NSString*) sha1{  const char *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*) sha224{  const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];  NSData *data = [NSData dataWithBytes:cstr length:self.length];   uint8_t digest[CC_SHA
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.