Programming

Objective-C의 MD5 알고리즘

procodes 2020. 6. 27. 14:35
반응형

Objective-C의 MD5 알고리즘


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

편집하다

NSData md5가 추가되어 직접 필요 하고이 작은 발췌문을 저장하기에 좋은 곳이라고 생각했기 때문에 ...

이러한 방법은 http://www.nsrl.nist.gov/testdata/ 의 NIST MD5 테스트 벡터를 사용하여 확인됩니다 .


내장 된 Common Crypto 라이브러리를 사용하여 그렇게 할 수 있습니다. 가져와야합니다.

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

성능이 중요한 경우이 최적화 된 버전을 사용할 수 있습니다. 그것은 약 5 배 빠른 속도를 가진 사람보다 stringWithFormatNSMutableString.

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

Well since people asked for a file-stream version. I have modified a nice little snippet made by Joel Lopes Da Silva that works with MD5, SHA1 and SHA512 AND it is using streams. Its made for iOS but works with just minimal changes on OSX aswell (remove the ALAssetRepresentation method). It can make checksums for files given a filepath or ALAssets (using ALAssetRepresentation). It's chunking data into small packages making memory impact minimal regardless of the filesize/asset size.

It's currently located on github here: https://github.com/leetal/FileHash


Any reason not to use the Apple implementation: https://developer.apple.com/library/mac/documentation/Security/Conceptual/cryptoservices/GeneralPurposeCrypto/GeneralPurposeCrypto.html#//apple_ref/doc/uid/TP40011172-CH9-SW1

Search for Cryptographic Services Guide on Apple developer site.

참고URL : https://stackoverflow.com/questions/1524604/md5-algorithm-in-objective-c

반응형