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. 5、CSS基础part-3

    1.CSS列表 ①类型 ul.disc {list-style-type: disc} ②位置 ul.inside {list-style-position: inside} ③列表图像 2.表格

  2. 看似不是dfs的dfs HDU-1455

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. PostgreSQL 数组类型

    PostgreSQL 支持表的字段使用定长或可变长度的一维或多维数组,数组的类型可以是任何数据库内建的类型.用户自定义的类型.枚举类型, 以及组合类型.但目前还不支持 domain 类型. 数组类型的 ...

  4. python re 模块小结

    前言: 本人环境windows 7 64位,python2.7 re是什么: regular expression缩写,意为正则表达式,是python的众多模块之一 re用途: 从文本中有选择的批量抽 ...

  5. Centos定时自动执行脚本

    检查本机crond的基本情况 1.crond的运行状况 2.crond是否开机自启动 如何将脚本添加进自动运行的时间内 直接编辑 vim /etc/crontab ,默认的文件形式如后面的图:   我 ...

  6. Spring MVC请求参数绑定

    所谓请求参数绑定,就是在控制器方法中,将请求参数绑定到方法参数上 @RequestParam 绑定单个请求参数到方法参数上 @RequestParam("id") Integer ...

  7. String类型的XML文件的格式化

    在接收到的xml报文中,未经过格式化,不好看 package org.zln.xml.format; import org.dom4j.Document; import org.dom4j.Docum ...

  8. ZigBee学习四 无线+UART通信

    ZigBee学习四 无线+UART通信 1) 协调器编程 修改coordinator.c文件 byte GenericApp_TransID; // This is the unique messag ...

  9. ICPC World Finals 2018 Problem H Single Cut of Failure

    题目链接 题解视频 题解文档 解法概要: 问题可以转化为 考虑一个长为 $2n$ 的数组 $A$,$1$ 到 $n$ 这 $n$ 个整数每个恰在 $A$ 中出现 $2$ 次.判断是否存在一个长为 $n ...

  10. php中session的生成机制、回收机制和存储机制探究

    1.php中session的生成机制 我们先来分析一下PHP中是怎么生成一个session的.设计出session的目的是保持每一个用户的各种状态来弥补HTTP协议的不足(无状态).我们现在有一个疑问 ...