質問

Objective-CでMD5を計算するには?

役に立ちましたか?

解決

MD5は、iPhone上で利用可能であり、以下のようなすなわちNSStringNSDataのための添加物として添加することができる。

MyAdditions.h

@interface NSString (MyAdditions)
- (NSString *)md5;
@end

@interface NSData (MyAdditions)
- (NSString*)md5;
@end

MyAdditions.m

#import "MyAdditions.h"
#import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access

@implementation NSString (MyAdditions)
- (NSString *)md5
{
    const char *cStr = [self UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (int)strlen(cStr), result ); // This is the md5 call
    return [NSString stringWithFormat:
        @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
        result[0], result[1], result[2], result[3], 
        result[4], result[5], result[6], result[7],
        result[8], result[9], result[10], result[11],
        result[12], result[13], result[14], result[15]
        ];  
}
@end

@implementation NSData (MyAdditions)
- (NSString*)md5
{
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5( self.bytes, (int)self.length, result ); // This is the md5 call
    return [NSString stringWithFormat:
        @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
        result[0], result[1], result[2], result[3], 
        result[4], result[5], result[6], result[7],
        result[8], result[9], result[10], result[11],
        result[12], result[13], result[14], result[15]
        ];  
}
@end

EDIT

を追加しましたNSDataのMD5私はそれを自分自身を必要とし、これはこの小さなスニペットを保存するのに適した場所であると思ったので...

これらの方法にNIST MD5テストベクトルを用いて検証されます http://www.nsrl.nist.gov/testdata/する

他のヒント

あなたはこれを行うには、組み込みの一般的な暗号化ライブラリを使用することができます。 インポートすることを忘れないでください:

#import <CommonCrypto/CommonDigest.h>

、その後ます:

- (NSString *) md5:(NSString *) input
{
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call

    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

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

    return  output;
}

パフォーマンスが重要な場合は、この最適化されたバージョンを使用することができます。 それはstringWithFormatまたはNSMutableStringを持つものに比べて約5倍の速さです。

これは、NSStringののカテゴリがあります。

- (NSString *)md5
{
    const char* cStr = [self UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, strlen(cStr), result);

    static const char HexEncodeChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    char *resultData = malloc(CC_MD5_DIGEST_LENGTH * 2 + 1);

    for (uint index = 0; index < CC_MD5_DIGEST_LENGTH; index++) {
        resultData[index * 2] = HexEncodeChars[(result[index] >> 4)];
        resultData[index * 2 + 1] = HexEncodeChars[(result[index] % 0x10)];
    }
    resultData[CC_MD5_DIGEST_LENGTH * 2] = 0;

    NSString *resultString = [NSString stringWithCString:resultData encoding:NSASCIIStringEncoding];
    free(resultData);

    return resultString;
}

さて人々は、ファイルストリーム版を求めたことから。私は、MD5、SHA1とSHA512で動作し、それがストリームを使用しているジョエル・ロペスダ・シルバによって作られた素敵な小さなスニペットを変更しました。そのiOSのために作られたが、OSXのaswellにだけ最小限の変更(ALAssetRepresentationメソッドを削除します)で動作します。これは、ファイルパスまたはALAssets(ALAssetRepresentationを使用して)指定されたファイルに対してチェックサムを行うことができます。それは関係なく、ファイルサイズ/資産規模のメモリへの影響が最小限作る小さなパッケージにデータをチャンクいます。

これは、現在ここにgithubの上に配置されます: https://github.com/leetal/FileHashする

アップルの実装を使用しない

何らかの理由:<のhref = "https://developer.apple.com/library/mac/documentation/Security/Conceptual/cryptoservices/GeneralPurposeCrypto/GeneralPurposeCrypto.html#//apple_ref/doc / UID / TP40011172-CH9-SW1" REL = "nofollowを"> https://developer.apple.com/library/mac/documentation/Security/Conceptual/cryptoservices/GeneralPurposeCrypto/GeneralPurposeCrypto.html#//apple_ref/doc/uid / TP40011172-CH9-SW1 の

アップルの開発者向けサイトで暗号化サービスガイドを検索します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top