본문 바로가기
ios 뽀개기/objective-c

ios objective c sha256 해쉬 sha 512 함수

by 인생여희 2018. 12. 5.
반응형


ios objective c sha256 해쉬 sha 512 함수


#import <CommonCrypto/CommonDigest.h>


-(NSString*) sha256:(NSString *)clear{

    

    const char *s=[clear cStringUsingEncoding:NSASCIIStringEncoding];

   

    NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];

    

    uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};

    CC_SHA256(keyData.bytes, keyData.length, digest);

    NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];

    NSString *hash=[out description];

    hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];

    hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];

    hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];

    return hash;

}



-(NSString *)createSHA512:(NSString *)string

{

    const char *cstr = [string cStringUsingEncoding:NSUTF8StringEncoding];

    NSData *data = [NSData dataWithBytes:cstr length:string.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;

}



반응형

댓글