Calculate MD5 Value

Source: Internet
Author: User
Tags file url

////MD5Value.h//iosedu////Created by Littest on 16/2/26.//copyright©2016 year littest. All rights reserved.//#import<Foundation/Foundation.h>#import<CommonCrypto/CommonDigest.h>#defineFilehashdefaultchunksizeforreadingdata 1024*8//8K@interfaceMd5value:nsobject//calculates the MD5 value of a nsdata+ (nsstring*) Getmd5withdata: (nsdata*) data;//computes the MD5 value of the string,+ (nsstring*) getmd5withstring: (nsstring*)string;//calculate the MD5 value of a large file+ (nsstring*) Getfilemd5withpath: (nsstring*) path;@end
////MD5VALUE.M//iosedu////Created by Littest on 16/2/26.//copyright©2016 year littest. All rights reserved.//#import "MD5Value.h"@implementationMd5value+ (nsstring*) getmd5withstring: (NSString *)string{        Const Char* original_str=[stringutf8string]; unsignedCharDigist[cc_md5_digest_length];//cc_md5_digest_length =cc_md5 (Original_str, strlen (ORIGINAL_STR), digist); Nsmutablestring* Outputstr = [nsmutablestring stringwithcapacity:Ten];  for(inti =0; i<cc_md5_digest_length;i++) {[Outputstr AppendFormat:@"%02x", Digist[i]];//lowercase x indicates that the output is lowercase MD5, and uppercase x indicates that the output is uppercase MD5            }        return[Outputstr lowercasestring]; }+ (nsstring*) Getmd5withdata: (NSData *) data{Const Char* Original_str = (Const Char*) [data bytes]; unsignedCharDigist[cc_md5_digest_length];//cc_md5_digest_length =cc_md5 (Original_str, strlen (ORIGINAL_STR), digist); Nsmutablestring* Outputstr = [nsmutablestring stringwithcapacity:Ten];  for(inti =0; i<cc_md5_digest_length;i++) {[Outputstr AppendFormat:@"%02x", Digist[i]];//lowercase x indicates that the output is lowercase MD5, and uppercase x indicates that the output is uppercase MD5            }                //You can also define a byte array to receive the computed MD5 value//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 variablescfstringref Result=NULL; Cfreadstreamref Readstream=NULL; //Get the file URLcfurlref FileURL=Cfurlcreatewithfilesystempath (Kcfallocatordefault,                                                                    (cfstringref) FilePath, Kcfurlposixpathstyle, (Boolean)false);        Cc_md5_ctx Hashobject; BOOLHasmoredata =true; BOOLDidsucceed; if(!fileurl)GotoDone ; //Create and open the read streamReadstream=Cfreadstreamcreatewithfile (Kcfallocatordefault,        (cfurlref) FileURL); if(!readstream)GotoDone ; Didsucceed= (BOOL) Cfreadstreamopen (Readstream); if(!didsucceed)GotoDone ; //Initialize the hash objectCc_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) /c6>sizeof(buffer)); if(Readbytescount = =-1) Break; if(Readbytescount = =0) {Hasmoredata=false; Continue; } cc_md5_update (&hashobject, (Const void*) buffer, (Cc_long) readbytescount); }                //Check If the read operation succeededDidsucceed= !Hasmoredata; //Compute the Hash digestunsignedCharDigest[cc_md5_digest_length]; Cc_md5_final (Digest,&hashobject); //Abort If the read operation failed        if(!didsucceed)GotoDone ; //Compute The string result        Charhash[2*sizeof(Digest) +1];  for(size_t i =0; I <sizeof(digest); ++i) {snprintf (hash+ (2* i),3,"%02x", (int) (Digest[i])); } result=cfstringcreatewithcstring (Kcfallocatordefault, (Const Char*) hash, kCFStringEncodingUTF8); Done:if(Readstream) {cfreadstreamclose (readstream);            Cfrelease (Readstream); }        if(FileURL) {cfrelease (FileURL); }        returnresult; }@end

Calculate MD5 Value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.