iOS 字符串加密至MD5

 #import <CommonCrypto/CommonDigest.h>

 + (NSString *) md5:(NSString *)str 
{
const char *cStr = [str UTF8String];
unsigned char result[];
CC_MD5( cStr, strlen(cStr), result );
return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[], result[], result[], result[],
result[], result[], result[], result[],
result[], result[], result[], result[],
result[], result[], result[], result[]
];
}

iPhone处理大文件检测MD5的代码

 +(NSString*)fileMD5:(NSString*)path
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
if( handle== nil ) return @"ERROR GETTING FILE MD5"; // file didnt exist CC_MD5_CTX md5; CC_MD5_Init(&md5); BOOL done = NO;
while(!done)
{
NSData* fileData = [handle readDataOfLength: CHUNK_SIZE ];
CC_MD5_Update(&md5, [fileData bytes], [fileData length]);
if( [fileData length] == ) done = YES;
}
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5_Final(digest, &md5);
NSString* s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
digest[], digest[],
digest[], digest[],
digest[], digest[],
digest[], digest[],
digest[], digest[],
digest[], digest[],
digest[], digest[],
digest[], digest[]];
return s;
}

In my project i need to get the MD_5 hash code of the file in iphone. uptill now i have found the following code to get md_5 of any image/any file.

-(NSString*)getMD5FromString:(NSString*)source{constchar*src =[source UTF8String];unsignedchar result[CC_MD5_DIGEST_LENGTH];
CC_MD5(src, strlen(src), result);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]]lowercaseString];}

using this code to get the ByteContent of the image and then get the md_5 of that image byte array string

UIImage*image =[UIImage imageNamed:@"sf_small.png"];NSData*data =UIImagePNGRepresentation(image);NSString*str =[NSString stringWithFormat:@"%@",data];NSString*temp =[self getMD5FromString:str];

now i am getting a hash code succesfully But when on the web side i get the md_5 hash code of same file then it gives me diferent hash code. in Web side i am using PHP code

 md5_file(string $filename);

this PHP code gives me differnet hash code and iphone code gives me different hash code for same image. Please tell me what can be the problem..

Thanks alot!

   

There are 2 causes. The first is because
the raw bytes → string → UTF-8 process corrupted some non-ASCII
characters. Note that you can get a pointer to the bytes from an NSData directly:

UIImage* image =[UIImage imageNamed:@"sf_small.png"];NSData* data =UIImagePNGRepresentation(image);constvoid* src =[data bytes];NSUInteger len =[data length];
CC_MD5(src, len, result);...

The second cause is because of the PNG → raw image → PNG process. There is no guarentee that the same image will compress to the same PNG representation in different libraries, and of course you'll have different MD5. You could just avoid reading the file as image altogether, as it's possible read the file directly as data:

NSData* data =[NSData dataWithContentsOfFile:@"sf_small.png"];

const void* src =[data bytes];

NSUInteger len =[data length];
CC_MD5(src, len, result);

iOS使用MD5 - 字符串加密至MD5&获取文件MD5的更多相关文章

  1. C#获取文件MD5字符串

    备注 哈希函数将任意长度的二进制字符串映射为固定长度的小型二进制字符串.加密哈希函数有这样一个属性:在计算不大可能找到散列为相同的值的两个不同的输入:也就是说,两组数据的哈希值仅在对应的数据也匹配时才 ...

  2. MD5字符串加密

    MD5字符串加密 Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.该算法的文件号为RFC 132 ...

  3. JAVA中获取文件MD5值的四种方法

    JAVA中获取文件MD5值的四种方法其实都很类似,因为核心都是通过JAVA自带的MessageDigest类来实现.获取文件MD5值主要分为三个步骤,第一步获取文件的byte信息,第二步通过Messa ...

  4. QT 获取文件MD5值

    /* 方法1 */ QFile theFile(fileNamePath); theFile.open(QIODevice::ReadOnly); QByteArray ba = QCryptogra ...

  5. c#获取文件MD5算法

    //获取文件MD5算法 private static string GetMD5FromFile(string fileName) { try { FileStream file = new File ...

  6. C# 获取文件MD5值的方法

    可用于对比文件是否相同 /// <summary> /// 获取文件MD5值 /// </summary> /// <param name="fileName& ...

  7. 基于js-spark-md5前端js类库,快速获取文件Md5值

    js-spark-md5是歪果仁开发的东西,有点多,但是我们只要一个js文件即可,具体类包我存在自己的oschina上,下载地址:https://git.oschina.net/jianqingwan ...

  8. 获取文件MD5值(JS、JAVA)

    文章HTML代码翻译于地址:https://www.cnblogs.com/linyihai/p/7040786.html           文件MD5有啥用?                  文 ...

  9. iOS字符串加密至MD5&及获取文件MD5

    iOS 字符串加密至MD5 #import <CommonCrypto/CommonDigest.h> + (NSString *) md5:(NSString *)str { const ...

随机推荐

  1. CSS滚动插件

    http://www.dowebok.com/131.html  wow.js http://www.jq22.com/jquery-info499 smoove.js http://www.lanr ...

  2. Linux之如何进行固定IP、DNS等设置

    前提:虚拟机Linux centOS6.6 Linux如何设置固定IP.dns.网关 1.切换到root账号 2.#cd  /etc/sysconfig/network-scripts 进入网卡的设置 ...

  3. python 学习分享-面向对象

    好激动,学了四个月的面向对象,我终于勉勉强强的把作业做完了,现在来重构我的面向对象的知识! 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 ...

  4. 聊聊、RabbitMQ 配置文件

    windows 下面安装 rabbitMQ 比较简单,但是想去改端口相关信息却找不到配置文件在哪.Linux 在 /etc/rabbitmq/rabbitmq.config 下面就可以找到.来看看 w ...

  5. BZOJ 4826 [Hnoi2017]影魔 ——扫描线 单调栈

    首先用单调栈和扫描线处理出每一个数左面最近的比他大的数在$l[i]$,右面最近的比他大的数$r[i]$. 然后就可以考虑每种贡献是在什么时候产生的. 1.$(l[i],r[i])$产生$p1$的贡献 ...

  6. SQL中GETDATE()一些操作

    Sql Server 中一个非常强大的日期格式化函数Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVE ...

  7. 微信小程序统一服务消息下发接口 公众号和小程序消息都可以在一个接口推送了

    昨天晚上,2018年9月11日,微信官方又更新了一大波的小程序功能.重点我们来谈谈这个功能,微信叫做统一服务消息下发接口. 这个是官方的文档  统一服务消息 · 小程序https://develope ...

  8. 提高Java开发效率,Idea必装的几款插件

    1.codeMaker 重复代码生成工具 domian和dto两个类基本差不多,这个工具就可以通过一个类快速生成另一个类. 2.gsonFormat json转换成Java类 很多时候根据别人接口给的 ...

  9. 用IHTMLDocument2接口获取页面上想要的数据,代替正则表达式

    原文发布时间为:2010-07-01 -- 来源于本人的百度文章 [由搬家工具导入] 1. 用 IHTMLDocument2::all 获得所有元素; 2. 用 IHTMLElementCollect ...

  10. [论文]A Link-Based Approach to the Cluster Ensemble Problem

    论文作者:Natthakan Iam-On, Tossapon Boongoen, Simon Garrett, and Chris Price 下次还是在汇报前先写了论文总结,不然有些点汇报时容易忘 ...