//
// MD5Value.h
// iOSEdu
//
// Created by littest on 16/2/26.
// Copyright © 2016年 littest. All rights reserved.
// #import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#define FileHashDefaultChunkSizeForReadingData 1024*8 // 8K
@interface MD5Value : NSObject
// 计算 NSData 的 MD5 值 +(NSString*)getMD5WithData:(NSData*)data; // 计算字符串的 MD5 值, +(NSString*)getmd5WithString:(NSString*)string; // 计算大文件的 MD5 值 +(NSString*)getFileMD5WithPath:(NSString*)path;
@end
//
// MD5Value.m
// iOSEdu
//
// Created by littest on 16/2/26.
// Copyright © 2016年 littest. All rights reserved.
// #import "MD5Value.h" @implementation MD5Value
+ (NSString*)getmd5WithString:(NSString *)string { const char* original_str=[string UTF8String]; unsigned char digist[CC_MD5_DIGEST_LENGTH]; //CC_MD5_DIGEST_LENGTH = 16 CC_MD5(original_str, strlen(original_str), digist); NSMutableString* outPutStr = [NSMutableString stringWithCapacity:]; for(int i =; i<CC_MD5_DIGEST_LENGTH;i++){ [outPutStr appendFormat:@"%02x", digist[i]];// 小写 x 表示输出的是小写 MD5 ,大写 X 表示输出的是大写 MD5 } return [outPutStr lowercaseString]; } + (NSString*)getMD5WithData:(NSData *)data{ const char* original_str = (const char *)[data bytes]; unsigned char digist[CC_MD5_DIGEST_LENGTH]; //CC_MD5_DIGEST_LENGTH = 16 CC_MD5(original_str, strlen(original_str), digist); NSMutableString* outPutStr = [NSMutableString stringWithCapacity:]; for(int i =; i<CC_MD5_DIGEST_LENGTH;i++){ [outPutStr appendFormat:@"%02x",digist[i]];// 小写 x 表示输出的是小写 MD5 ,大写 X 表示输出的是大写 MD5 } // 也可以定义一个字节数组来接收计算得到的 MD5 值 // Byte byte[16]; // CC_MD5(original_str, strlen(original_str), byte); // NSMutableString* outPutStr = [NSMutableString stringWithCapacity:10]; // for(int i = 0; i<CC_MD5_DIGEST_LENGTH;i++){ // [outPutStr appendFormat:@"%02x",byte[i]]; // } // [temp release]; return [outPutStr lowercaseString]; } +(NSString*)getFileMD5WithPath:(NSString*)path { return (__bridge NSString *)FileMD5HashCreateWithPath((__bridge CFStringRef)path,FileHashDefaultChunkSizeForReadingData); } CFStringRef FileMD5HashCreateWithPath(CFStringRef filePath, size_t chunkSizeForReadingData) { // Declare needed variables CFStringRef result = NULL; CFReadStreamRef readStream = NULL; // Get the file URL CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)filePath, kCFURLPOSIXPathStyle, (Boolean)false); CC_MD5_CTX hashObject; bool hasMoreData = true; bool didSucceed; if (!fileURL) goto done; // Create and open the read stream readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault, (CFURLRef)fileURL); if (!readStream) goto done; didSucceed = (bool)CFReadStreamOpen(readStream); if (!didSucceed) goto done; // Initialize the hash object CC_MD5_Init(&hashObject); // Make sure chunkSizeForReadingData is valid if (!chunkSizeForReadingData) { chunkSizeForReadingData = FileHashDefaultChunkSizeForReadingData; } // Feed the data to the hash object while (hasMoreData) { uint8_t buffer[chunkSizeForReadingData]; CFIndex readBytesCount = CFReadStreamRead(readStream, (UInt8 *)buffer, (CFIndex)sizeof(buffer)); if (readBytesCount == -)break; if (readBytesCount == ) { hasMoreData =false; continue; } CC_MD5_Update(&hashObject,(const void *)buffer,(CC_LONG)readBytesCount); } // Check if the read operation succeeded didSucceed = !hasMoreData; // Compute the hash digest unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5_Final(digest, &hashObject); // Abort if the read operation failed if (!didSucceed) goto done; // Compute the string result char hash[ *sizeof(digest) + ]; for (size_t i =; i < sizeof(digest); ++i) { snprintf(hash + ( * i),, "%02x", (int)(digest[i])); } result = CFStringCreateWithCString(kCFAllocatorDefault, (const char *)hash, kCFStringEncodingUTF8); done: if (readStream) { CFReadStreamClose(readStream); CFRelease(readStream); } if (fileURL) { CFRelease(fileURL); } return result; }
@end

计算 MD5值的更多相关文章

  1. Windows下计算md5值

    目录 Windows下计算md5值 1.linux 下计算md5值 2.Windows下计算md5值 Windows下计算md5值 1.linux 下计算md5值 [root@master yl]# ...

  2. 不要对md5file.read()计算md5值

    最近遇到的一个问题,我使用以下代码对备份文件计算MD5值: # md5file=open("%s" % outputpath, 'rb') # md5=hashlib.md5(md ...

  3. shell 批量计算MD5值

    #!/bin/sh #需要计算MD5文件列表 # list=`ls` list="file list" for file in $list do file1=`` echo &qu ...

  4. python计算md5值

    from hashlib import md5 m = md5(') print m.hexdigest()

  5. Html5计算MD5值

    教程: http://www.tuicool.com/articles/InEBNz 组件: https://github.com/satazor/js-spark-md5

  6. python计算文件的md5值

    前言 最近要开发一个基于python的合并文件夹/目录的程序,本来的想法是基于修改时间的比较,即判断文件有没有改变,比较两个文件的修改时间即可.这个想法在windows的pc端下测试没有问题. 但是当 ...

  7. C#计算文件的MD5值实例

    C#计算文件的MD5值实例 MD5 是 Message Digest Algorithm 5(信息摘要算法)的缩写,MD5 一种散列(Hash)技术,广泛用于加密.解密.数据签名和数据完整性校验等方面 ...

  8. 计算字符串和文件的MD5值

    //计算字符串的MD5值 public string GetMD5(string sDataIn) { MD5CryptoServiceProvider md5 = new MD5CryptoServ ...

  9. c#计算文件的MD5值

    代码: /// <summary> /// 计算文件的 MD5 值 /// </summary> /// <param name="fileName" ...

随机推荐

  1. bash里,echo对换行符的处理

    echo -e "#include <stdio.h>\nint main()\n{\n printf(\"hello world\\\n\");\n ret ...

  2. CTSC2014 被虐总结

    第一次参加全世界最难的比赛- - 感觉简直神 两试考了65+81=146分 Ag线155 Au线190+ orz 又是一粒Cu QAQ orz神ak170大虐全场 Day1: 考试经过: day1睡得 ...

  3. Failed to allocate the network(s), not rescheduling

    Failed to allocate the network(s), not rescheduling 在计算节点的/etc/nova/nova.conf中添加下面两句 #Fail instance ...

  4. 黄金点游戏之客户端(homework-05)

    0. 摘要 之前我们玩了2次黄金数游戏,我也幸运的得到了一本<代码大全>,嘿嘿.这次的作业是一个Client/Server程序,自动化完成多轮重复游戏. 我完成了Client部分,使用C# ...

  5. 教程-Close、Halt、terminate、ExitProcess的区别

    Close:1.只关闭本窗体2.当Close是一个主窗体时,程序会退出.3.Close会发生FormClose事件,FormCloseQuery事件4.主窗体close以后程序就Application ...

  6. HDU 2425 DNA repair (AC自动机+DP)

    DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  8. Codeforces 600B Queries about less or equal elements(二分查找)

    Description You are given two arrays of integers a and b. For each element of the second array bj yo ...

  9. rop框架中@ServiceMethod注解属性

    @ServiceMethod 属性 method :代码服务方法名version :表 示 版 本 号 group:服务分组名.服务的分组没有特殊的意义,您可以为服务定义一个分组,以便在事件监听器.服 ...

  10. cookie 编码问题

    问题描述:  Control character in cookie value or attribute. 解决方案: 1.前台编码 encodeURIComponent(str) 2.后台解码 原 ...