Ios obtains the MD5 value of a file.

Source: Internet
Author: User

Generally, when we use http or socket to upload or download files, we often perform MD5 verification after completion (especially when resumable data transfer is performed ).

Multiple). The MD5 value is verified to prevent packet loss or data packet tampering during transmission. Before using MD5, we should first understand some common knowledge about MD5. MD5 Baidu encyclopedia

 

To put it simply:

1) MD5 is the digest of a file or string calculated using a hash algorithm. The brief description of the MD5 algorithm can be: MD5 is grouped by 512 bits to process the input information, each group is divided into 16 32-bit groups. After a series of processing, the output of the algorithm is composed of four 32-bit groups, after the four 32-bit groups are cascaded, a 128-bit hash value is generated. 128/8 = 16, that is to say, MD5 is a group of octal bytes in length.

2) In general, it needs to be converted into hexadecimal output, and the output is in lower case at the same time.

With this basic knowledge, it is not that difficult to calculate MD5. Recently, I found a lot of ios MD5 code on the Internet during the MD5 Calculation of large files, most of them are unavailable, especially

NSFileHandle * handle = [NSFileHandle fileHandleForReadingAtPath: _ filePath, instead of calculating the entire file's MD5 Digest, you will always be in an awkward situation. Example :(

NSData * fileData = [handle readDataOfLength: 1024*8]; // always reads files of 1024*8 length starting from the starting position. If this method is used, the file read location must be set to the specified location before each read. NSFileHandle's-(void) seekToFileOffset :( unsigned long) offset should be used;
)

The following is a piece of code I can use: the MD5 value calculated by the same platform is the same. (You may see

If filehashdefachchunksizeforreadingdata is not defined, you should add a mixed definition to the header file:

# Define FileHashDefaultChunkSizeForReadingData 1024*8
)

The Code is as follows:

 

+ (NSString *) getFileMD5WithPath :( NSString *) path

{

Return (_ bridge_transfer 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 );

If (! FileURL) goto done;

// Create and open the read stream

ReadStream = CFReadStreamCreateWithFile (kCFAllocatorDefault,

(CFURLRef) fileURL );

If (! ReadStream) goto done;

Bool didSucceed = (bool) CFReadStreamOpen (readStream );

If (! DidSucceed) goto done;

// Initialize the hash object

CC_MD5_CTX hashObject;

CC_MD5_Init (& hashObject );

// Make sure chunkSizeForReadingData is valid

If (! ChunkSizeForReadingData ){

ChunkSizeForReadingData = FileHashDefaultChunkSizeForReadingData;

}

// Feed the data to the hash object

Bool hasMoreData = true;

While (hasMoreData ){

Uint8_t buffer [chunkSizeForReadingData];

CFIndex readBytesCount = CFReadStreamRead (readStream, (UInt8 *) buffer, (CFIndex) 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 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 [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 );

}

Return result;

}

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.