Calculating MD5 Digest with iPhone SDK

Source: Internet
Author: User
Tags md5 digest

The Code originates from http://blog.andrewpaulsimmons.com/2008/07/md5-hash-on-iphone.html
.
I have copied it here for processing ing purposes. Please express your appreciations to Andrew for his efforts.

#import <CommonCrypto/CommonDigest.h>
NSString* md5( NSString *str )
{
const char *cStr = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
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[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]
];
}

Bytes -------------------------------------------------------------------------------------------------------------

Today, the signature uses the MD5 algorithm, so I have some knowledge about it, mainly considering that it will not be cracked by others. Haha, it is indeed irreversible, now I will repost an article about the history of this algorithm. The specific algorithm is complicated. I have read it in my graduation project and I will not explain it here, in short, I think the MD5 algorithm is indeed a good algorithm.

Transfer
Post: //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////

I recently studied Java's encryption algorithms, such as blowfish/MD5/twofish/RSA. I guess MD5 should be the most widely used. I will go through an article to introduce MD5 to you.

Introduction to MD5, algorithms and implementations of wrote by doll (nydoll)/[CCG] China cracking organization China cracking Group

The full name of MD5 is message-Digest algorithm 5, which was invented by MIT's computer science lab and RSA Data Security Inc in Early 1990s and developed by md2, md3, and md4.

Message-digest refers to the hash transformation of a message, which is to convert a byte string of any length into a long big integer. Note thatByte string


"Rather than" string ", because this transformation is only related to the value of the byte, and has nothing to do with the character set or encoding method.

MD5 converts a "Byte string" of any length into a large integer of BITs, and it is an irreversible String Conversion Algorithm. (I was stupid at the beginning to think that MD5 is a reversible algorithm.
In other words, even if you see the source program and algorithm description, you cannot convert an MD5 value back to the original string. In terms of mathematical principle, it is because of the original character.
There are infinite strings, which is a bit like a mathematical function without an inverse function.

A typical application of MD5 is to generate fingerprint (fingerprint) for a message (byte string) to prevent "tampering ". For example, you can write a paragraph in
In the readme.txt file, and generate an MD5 value for this readme.txt file and record it. Then you can spread this file to others. If someone else modifies any content in the file
When you re-calculate the MD5 value for this file, you will find that. If there is another third-party certification authority, MD5 can also prevent the "credit" of the file author. This is the so-called digital signature application.

MD5 is also widely used in encryption and decryption technology. In many operating systems, users' passwords are stored in MD5 values (or similar algorithms, the system calculates the password entered by the user into an MD5 value, and then compares it with the MD5 value saved in the system. The system does not "know" what the user password is.

Some hackers crack this password in a way called "Running Dictionary. There are two ways to get the dictionary: one is the string table that is collected daily and the other is generated by means of arrangement and combination, use the MD5 program to calculate the MD5 value of these dictionary items, and then use the target MD5 value for retrieval in this dictionary.

Even if the maximum length of the password is 8 and the password can only contain letters and numbers, 26 + 26 + 10 = 62 characters in total, the number of dictionary items in the combination is
P (62,1) + P (62,2 ).... + P (), which is already a very astronomical number. to store this dictionary, you need a TB-level disk group, and this method has a premise:
Only when the MD5 value of the target account is obtained.

In software encryption protection, many software uses MD5 protection, but the MD5 algorithm is irreversible.
Therefore, all software uses the MD5 algorithm as an intermediate step of encryption. For example, if you perform an MD5 transformation on the user name, the result is then a reversible encryption transformation. When registering a machine, you only need to use MD5 to change it first.
And then use an inverse algorithm. Therefore, it is easy for the attacker to see that it is MD5.

The MD5 code has obvious features and is easy to find during tracking. If the software uses the MD5 algorithm, the following four constants must be used during data initialization.
0x67452301;
0xefcdab89;
0x98badcfe;
0x10325476;
If the constants are not equal, it may be the transformed MD5 algorithm or it is not the algorithm at all. In the memory, that is
01 23 45 67 89 AB CD ef fe DC... 32 10 16 bytes
MD5 Algorithm:

Step 1: Add Filling
Increase the padding value so that the Data Length (in BIT) mode is 512 and 448. If the data length is equal to the modulo 512 to 448, increase the number of filled BITs by 512, that is, the number of filled bits is 1-512. The first bit is 1, and all others are 0.

Step 2: Fill in the length
Convert the data length to a 64-bit value. If the length exceeds the range of the Data Length expressed by 64-bit, the value is reserved for the last 64-bit value and is added to the end of the previously filled data, make the final data an integer multiple of bits. That is, the 16-fold integer of 32bit. In rfc1321, 32bit is called a word.

Step 3: Initialize the variable:
Four variables, A, B, C, and D, are used. Initialization:
A: 01 23 45 67
B: 89 AB CD ef
C: Fe DC Ba 98
D: 76 54 32 10

Step 4: data processing:
Define four auxiliary functions:
F (x, y, z) = xy V not (x) Z
G (x, y, z) = xz v y not (z)
H (x, y, z) = x XOR y XOR Z
I (x, y, z) = y XOR (X v not (z ))
Where: XY indicates bitwise AND, x v y indicates bitwise OR, not (x) indicates bitwise inversion. XOR indicates bitwise XOR.
X, Y, and Z in the function are 32bit.

Defines an array to be used: T (I), I value 1-64, T (I) is equal to 4294967296 times of ABS (sin (I, I is a radian.
Assume that the data length after the first three steps is 32*16 * nbit.

Step 5: output:
The obtained ABCD is the output result of BITs in total. A is low, and D is high.

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.