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;
}