banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

iOS開発におけるObjective-CのSHA224/SHA256/SHA384/SHA512暗号アルゴリズムの実装

以前のブログ記事で実装した SHA1 の安全性はユーザーのニーズを満たしていないため、今日は SHA224/SHA256/SHA384/SHA512 の実装を一緒に添付します。SHA は Secure Hash Algorithm(安全なハッシュアルゴリズム)の略で、さまざまなビット数の実装があります。一般的なものには SHA224/SHA256/SHA384/SHA512 などがあります。

SHA224:

- (NSString*) sha224
{
const char *cstr = [self cStringUsingEncoding];
NSData *data = [NSData dataWithBytes length.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];
NSData *data = [NSData dataWithBytes length.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];
NSData *data = [NSData dataWithBytes length.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];
NSData *data = [NSData dataWithBytes length.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 #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];
    NSData *data = [NSData dataWithBytes length.length];

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes, data.length, digest);

    NSMutableString* output = [NSMutableString stringWithCapacity_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];
    NSData *data = [NSData dataWithBytes length.length];

    uint8_t digest[CC_SHA224_DIGEST_LENGTH];

    CC_SHA224(data.bytes, data.length, digest);

    NSMutableString* output = [NSMutableString stringWithCapacity_SHA224_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_SHA224_DIGEST_LENGTH; i++)
    [output appendFormat:@"%02x", digest[i]];

    return output;
    }

  • (NSString*) sha256
    {
    const char *cstr = [self cStringUsingEncoding];
    NSData *data = [NSData dataWithBytes length.length];

    uint8_t digest[CC_SHA256_DIGEST_LENGTH];

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

    NSMutableString* output = [NSMutableString stringWithCapacity_SHA256_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++)
    [output appendFormat:@"%02x", digest[i]];

    return output;
    }

  • (NSString*) sha384
    {
    const char *cstr = [self cStringUsingEncoding];
    NSData *data = [NSData dataWithBytes length.length];

    uint8_t digest[CC_SHA384_DIGEST_LENGTH];

    CC_SHA384(data.bytes, data.length, digest);

    NSMutableString* output = [NSMutableString stringWithCapacity_SHA384_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_SHA384_DIGEST_LENGTH; i++)
    [output appendFormat:@"%02x", digest[i]];

    return output;
    }

  • (NSString*) sha512
    {
    const char *cstr = [self cStringUsingEncoding];
    NSData *data = [NSData dataWithBytes length.length];

    uint8_t digest[CC_SHA512_DIGEST_LENGTH];

    CC_SHA512(data.bytes, data.length, digest);

    NSMutableString* output = [NSMutableString stringWithCapacity_SHA512_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
    [output appendFormat:@"%02x", digest[i]];

    return output;
    }

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。