【转】iOS: [NSString hash]出现同样的hash值问题 –计算string的MD5值

问题原因:

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

  • (NSString )sha1 { NSData data = [self dataUsingEncoding:NSUTF8StringEncoding]; uint8 _t digest[CC_SHA1 _DIGEST_LENGTH]; CC_SHA1(data.bytes, data.length, digest); NSMutableString *output = [NSMutableString stringWithCapacity:CC _SHA1_DIGEST_LENGTH * 2]; for (int i = 0; i < CC _SHA1_DIGEST_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

PS:

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

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. uint8_t digest[CC_SHA1_DIGEST_LENGTH];
  6. CC_SHA1(data.bytes, data.length, digest);
  7. NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
  8. for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
  9. [output appendFormat:@”%02x”, digest[i]];
  10. return output;
  11. }

MD5 –

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

Hope it will help someone!!

Original: https://www.cnblogs.com/wi100sh/p/14197303.html
Author: wi100sh
Title: 【转】iOS: [NSString hash]出现同样的hash值问题 –计算string的MD5值

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/553912/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Eclipse崩溃后无法启动的问题解决

    一次Eclipse发生内存溢出(ADT环境,多打开几个xml文件内存占用就会飚升),强制结束任务,再次启动Eclipse发现闪退。查看workspace/.metadata/.lo…

    技术杂谈 2023年5月30日
    082
  • C++Protobuf

    Protobuf protobuf (protocol buffer) 是谷歌内部的混合语言数据标准。通过将结构化的数据进行序列化(串行化),用于通讯协议、数据存储等领域的语言无关…

    技术杂谈 2023年7月24日
    0115
  • mybatis整合springboot 以及需要注意的细节

    具体怎么整合的网上有很多优秀的博客介绍,这里就直接引用一篇个人觉得非常详细的教程: https://blog.csdn.net/winter_chen001/article/det…

    技术杂谈 2023年7月11日
    069
  • MySQL学习-eclipse导入jar包

    导包先有包 !!!一定要下载和自己MySQL版本一样的jar包!!! !!!一定要下载和自己MySQL版本一样的jar包!!! !!!一定要下载和自己MySQL版本一样的jar包!…

    技术杂谈 2023年6月21日
    095
  • FLASH CS4 制作渐变 动画 有补间动画 传统补间

    下了个FLASH CS4 ,也许太久没玩FLASH了,很多东西都物是人非了,要弄个动画渐变一直不成功。 点来点去,发现有个”添加传统补间”,试一下,可以进行…

    技术杂谈 2023年7月10日
    061
  • 对象的合并及拷贝

    Object.assign() Object.assign() 方法用于对象的合并,将所有自身的(非继承的)可枚举属性的值从一个或多个源对象拷贝到目标对象。返回目标对象。目标对象自…

    技术杂谈 2023年5月31日
    0102
  • 一句话介绍100部国学经典,一生一定要读一次

    我国古代将古籍按内容分为四大部类: 经、史、子、集。 经:经书,是指儒家经典著作; 史:史书,即正史; 子:先秦百家著作,宗教; 集:文集,即诗词汇编。 “经史子集&#…

    技术杂谈 2023年6月1日
    075
  • Battle:你会TLAB,我会逃逸分析

    “噔噔噔……”传来一阵敲门声,把我从美梦中惊醒了。 朦胧间听到有人在说话”阿Q,在家不?” “来…

    技术杂谈 2023年7月10日
    067
  • 十二、异常处理机制(完结)

    十二、异常处理机制 12.1 异常处理的引入 public class Exception01 { public static void main(String[] args) {…

    技术杂谈 2023年7月11日
    062
  • 初读鸟哥的linux私房菜的收获

    搞了十几年开发,一直是在windows下面搞.net开发,有一些不甘心,所以转行去搞java开发,当然也少不了要学习一下linux嘛。前同事波神是这方面的高手,给我推荐去读《鸟哥的…

    技术杂谈 2023年7月11日
    065
  • 通过tinc实现准sdwan

    原文: 通过Tinc实现内网穿透 简介 Tinc是一个开源的组网软件,相比之前的《通过ZeroTier实现内网穿透》,Tinc的配置较为繁琐(ZeroTier配置更简单,只需要注册…

    技术杂谈 2023年5月31日
    0111
  • java可变参数

    可变参数 用法: public void test(int… i){} //类型后边加… 本质是数组参考文档: 方法中有多个参数是,可变参数必须放在最后 例…

    技术杂谈 2023年7月11日
    044
  • Sentinel 整合OpenFeign降级

    package com.wsm.stock.controller; import org.springframework.beans.factory.annotation.Valu…

    技术杂谈 2023年5月31日
    071
  • Selenium

    Selenium 简介 Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同是Selenium…

    技术杂谈 2023年6月21日
    092
  • 如何正确地在Axis、Axis2和Apache CXF之间抉择?

    新一代的 Web Services 框架如 Axis2、CXF 都是由现有的项目中逐渐演化而来的,Axis2 是由大家熟悉的 Axis 1.x 系列演化过来,而 Apache CX…

    技术杂谈 2023年5月31日
    076
  • 一句话的需求怎么测?需求文档的三种现状及应对策略

    转载请注明出处❤️ 你好,我是测试蔡坨坨。 今天,我们来聊聊需求文档那些事儿…… 众所周知,软件需求是软件项目研发的开始,是组建研发团队后第一次集体讨论的事…

    技术杂谈 2023年7月11日
    061
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球