【问题标题】:How to calculate the hash MD5 of each content of a NSMutableArray?如何计算 NSMutableArray 的每个内容的哈希 MD5?
【发布时间】:2012-06-18 15:56:24
【问题描述】:

我有这个:

NSString *string1 = ...;
NSString *string2 = ...;

NSMutableArray *array = [NSMutableArray alloc]inithWithObjects:string1, string2]autorelease];

如何计算array 的每个内容的MD5 哈希(或其他更合适的哈希),以便进一步比较?

谢谢!

【问题讨论】:

    标签: objective-c cocoa-touch cocoa hash nsmutablearray


    【解决方案1】:

    您可以在数组的每个字符串上使用此方法:

    - (NSString*)md5HexDigest:(NSString*)input {
    const char* str = [input UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, strlen(str), result);
    
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
    for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
    }
    

    别忘了包括:

    #import <CommonCrypto/CommonDigest.h>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 2011-12-08
      • 2021-06-19
      • 1970-01-01
      • 2011-08-27
      • 2015-12-18
      • 2017-01-27
      相关资源
      最近更新 更多