سؤال

كيفية حساب MD5 في الهدف - ج؟

هل كانت مفيدة؟

المحلول

MD5 متاح على iPhone ويمكن إضافته كإضافة ل IE NSString و NSData مثل أدناه.

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 لأنني كنت بحاجة إلى ذلك بنفسي ويعتقد أن هذا مكان جيد لإنقاذ هذا المقتطف الصغير ...

يتم التحقق من هذه الطرق باستخدام ناقلات اختبار 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;
}

إذا كان الأداء مهما، فيمكنك استخدام هذا الإصدار الأمثل. انها حوالي 5 مرات أسرع من تلك مع stringWithFormat أو NSMutableString.

هذه فئة من 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;
}

حسنا منذ أن طلب الناس إصدار دفق الملفات. لقد قمت بتعديل مقتطف صغير لطيف مصنوع من جويل LOPES DA Silva الذي يعمل مع MD5 و SHA1 و SHA512 ويستخدم الجداول. صنعها ل iOS ولكنها تعمل مع الحد الأدنى من التغييرات على OSX ASWELL (إزالة طريقة AlassetRefresentation). يمكن أن يجعل المجموع الاختباري للملفات التي تعطى filepath أو alassets (باستخدام AlassetRefresentation). إنها بيانات شاملة في حزم صغيرة تجعل تأثير الذاكرة ضئيلا بغض النظر عن حجم الملف / الأصول.

يقع حاليا في GitHub هنا: https://github.com/leetal/filehash.

أي سبب لعدم استخدام تطبيق Apple: https:veloper.apple.com/library/mac/documentation/security/conceptual/cryptoservices/generalpurprosecrypto.html#//apple_ref/doc/uid/tp40011172-ch9-sw1.

البحث عن دليل خدمات التشفير على موقع مطور أبل.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top