Implementation of OBJECTIVE-C SHA224/SHA256/SHA384/SHA512 encryption algorithm for iOS development

Source: Internet
Author: User
Tags sha1
Previously implemented in the blog SHA1 Security has not met the needs of users, today the implementation of the sha224/sha256/sha384/sha512 attached.
The SHA that Secure Hash algorithm (safe hashing algorithm) has many kinds of different bit number realization, common has sha224/sha256/sha384/sha512 and so on

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 sha_digest_length ibr> [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 sha_digest_length ibr> [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 sha_digest_length ibr> [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 sha_digest_length ibr> [Output appendformat:@ "%02x", Digest[i]];

return output;
}


Put together a great unity


//
Nsstring+sha.h
//

#import
#import
#import

@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 sha_digest_length ibr> [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_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 sha_digest_length ibr> [Output appendformat:@ "%02x", Digest[i]];

return output;
}

-(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 sha_digest_length ibr> [Output appendformat:@ "%02x", Digest[i]];

return output;
}

-(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 sha_digest_length ibr> [Output appendformat:@ "%02x", Digest[i]];

return output;
}

-(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 sha_digest_length ibr> [Output appendformat:@ "%02x", Digest[i]];

return output;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.