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. luogu2093 [国家集训队]JZPFAR

    题面不符?-- #include <algorithm> #include <iostream> #include <cstdio> using namespace ...

  2. jxl教程图文详解

    近来学习了下jxl的操作Excel报表功能,现有的API基本可以满足当前的需要,抽空做了一个学生成绩查询报表的例子. 先看效果图: 从图中可以看到这是一个交叉报表,横向到Q列,纵向有22行,全部是通过 ...

  3. 第一次接触php

    一.什么是PHP PHP的中文意思:超文本预处理器,英文名字: HyperText Preprocessor. PHP通常有两层含义: (1)PHP是一个编程语言. (2)PHP是处理PHP编程语言的 ...

  4. 【Gray Code】cpp

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  5. IOS开发学习笔记019-动态创建控件

    动态创建控件 一.按钮 二.文本输入框 三.lable标签 注意: 只是简单的拖拽控件会毁了你,所以最好还是手动通过代码创建控件. 如果要通过代码生成按钮的话,可以在系统自带的函数viewDidLoa ...

  6. Python+Selenium基础篇之3-打开和关闭IE/Chrome浏览器

    前面文章介绍了,如何调用webdriver接口方法来打开和关闭Firefox浏览器,本文介绍如何打开IE和Chrome浏览器.web项目,需要做兼容性测试,最重要的是浏览器兼容性测试.如果只考虑win ...

  7. 前端应该掌握的CSS实现多列等高布局

    1.引言 我们在写页面的时候,有的时候会遇到多栏布局,每个栏目里面的内容有的时候可能不一样,这样就会导致每个栏目实际的高度也是不一样的,如果每个栏目有背景颜色的,就会导致每个栏目的底部是对不齐的,用户 ...

  8. c++ primer plus 第6版 部分三 9章 - 章

    c++ primer plus 第6版                                               部分三 9章 - 章 第9章   内存模型和名称空间 1.单独编译 ...

  9. CSU-2034 Column Addition

    CSU-2034 Column Addition Description A multi-digit column addition is a formula on adding two intege ...

  10. hnust CZJ-Superman

    问题 B: CZJ-Superman 时间限制: 1 Sec  内存限制: 128 MB提交: 636  解决: 87[提交][状态][讨论版] 题目描述 “那是只鸟?那是飞机?那是——超人!” 程序 ...