Algorytm MD5 w Objective C

Jak obliczyć MD5 w celu C ?

Author: ThomasW, 2009-10-06

5 answers

Md5 jest dostępny na iPhone i może być dodany jako dodatek dla ie NSString i NSData Jak Poniżej.

Moje Warunki.h

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

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

Moje Warunki.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

Dodano nsdata md5, ponieważ sam go potrzebowałem i pomyślałem, że to dobre miejsce, aby zapisać ten mały fragment...

Metody te są weryfikowane przy użyciu wektorów testowych NIST MD5 w http://www.nsrl.nist.gov/testdata/

 216
Author: epatel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-09 11:50:18

Możesz użyć wbudowanej wspólnej biblioteki kryptograficznej. Pamiętaj, aby zaimportować:

#import <CommonCrypto/CommonDigest.h>

A następnie:

- (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;
}
 49
Author: Bruno Koga,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-02 10:37:45

Jeśli wydajność jest ważna, możesz użyć tej zoptymalizowanej wersji. Jest około 5 razy szybszy niż te z stringWithFormat lub NSMutableString.

Jest to kategoria 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;
}
 8
Author: Pavel Alexeev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-03 09:49:26

Cóż, skoro ludzie pytali o wersję strumienia plików. Zmodyfikowałem ładny mały fragment Joela Lopesa da Silvę, który działa z MD5, SHA1 i SHA512 i używa strumieni. Został stworzony dla iOS, ale działa z minimalnymi zmianami na OSX (Usuń metodę ALAssetRepresentation). Może tworzyć sumy kontrolne dla plików o podanej ścieżce lub ALAssets (używając Assetrepresentation). Gromadzi dane w małych pakietach, minimalizując wpływ na pamięć niezależnie od rozmiaru pliku/zasobu rozmiar.

Obecnie znajduje się na GitHubie tutaj: https://github.com/leetal/FileHash

 0
Author: Alexander W,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-20 11:47:13

Wszelkie powody, aby nie korzystać z implementacji Apple: https://developer.apple.com/library/mac/documentation/Security/Conceptual/cryptoservices/GeneralPurposeCrypto/GeneralPurposeCrypto.html#//apple_ref/doc/uid/TP40011172-CH9-SW1{[2]

Wyszukaj Przewodnik po usługach kryptograficznych na stronie dewelopera Apple.

 0
Author: vpathak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-13 09:49:56