zoukankan      html  css  js  c++  java
  • 【转】iOS: [NSString hash]出现同样的hash值问题 --计算string的MD5值

    原文网址:https://blog.csdn.net/ouyangtianhan/article/details/43672667

    问题原因:

    At least there are special circumstances for which this unreliability kicks in.

    Comparing [a hash] and [b hash] of two different NSString is safe when:

    • the strings' length is shorter or equal to 96 characters.
    • [a length] is different to [b length].
    • the concatinated first, middle, and last 32 characters of a differ to the concatinated components of b.

    Otherwise every difference between the first and the middle 32 chars, as well as every difference between the middle and the last 32 characters, are not used while producing the [NSString hash] value.

    解决方法:

    For my case, the solution was pretty easy. I made use of a sha1 hash, which produced 
    a fingerprint from the whole string instead of pieces only. I got mine inspired by 
    the SHA1 method of Saurabh Sharma

    lang:objc SHA1

    • (NSString *)sha1 { NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding]; uint8t digest[CCSHA1DIGESTLENGTH];

      CC_SHA1(data.bytes, data.length, digest);

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

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

      return output; }

    The SHA1 was for the name of a cached file, generated by giving the complete URL of the resource to be cached. Works perfect. I already filed a pull request for

    http://www.makebetterthings.com/iphone/how-to-get-md5-and-sha1-in-objective-c-ios-sdk/

    PS:

    How to get md5 and SHA1 in objective c (iOS sdk)

    Calculating the md5 and sha1 hash in iOS sdk is pretty simple -

    Step 1 – The very first thing you need to do is import CommonCrypto’s CommonDigest.h

    #import <CommonCrypto/CommonDigest.h>
    

    Step 2 – Here is the real code for calculating SHA1 and MD5 hash -

    SHA1 -

    1.  
      -(NSString*) sha1:(NSString*)input
    2.  
      {
    3.  
      const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    4.  
      NSData *data = [NSData dataWithBytes:cstr length:input.length];
    5.  
       
    6.  
      uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    7.  
       
    8.  
      CC_SHA1(data.bytes, data.length, digest);
    9.  
       
    10.  
      NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    11.  
       
    12.  
      for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
    13.  
      [output appendFormat:@"%02x", digest[i]];
    14.  
       
    15.  
      return output;
    16.  
       
    17.  
      }

    MD5 -

    1.  
      - (NSString *) md5:(NSString *) input
    2.  
      {
    3.  
      const char *cStr = [input UTF8String];
    4.  
      unsigned char digest[16];
    5.  
      CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
    6.  
       
    7.  
      NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    8.  
       
    9.  
      for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    10.  
      [output appendFormat:@"%02x", digest[i]];
    11.  
       
    12.  
      return output;
    13.  
       
    14.  
      }

    Hope it will help someone!!

  • 相关阅读:
    《构建之法》4、17章精读
    2016012005+小学四则运算练习软件项目报告
    Week2-作业一——《构建之法》三章精读之想
    虚拟到现实
    脚踏实地,莫问前程
    2016012010 赵瑞雪 散列函数的应用及其安全性
    结对项目报告
    《构建之法》第四章、第十七章读书笔记
    2016012010+小学四则运算练习软件项目报告
    《构建之法》第一、二、十六章读书笔记
  • 原文地址:https://www.cnblogs.com/wi100sh/p/14197303.html
Copyright © 2011-2022 走看看