In iOS development process, in order to ensure the security of data, we usually have to take some encryption methods, common encryption has Base64 encryption and MD5 encryption. BASE64 encryption is reversible, MD5 encryption is generally irreversible at the moment. We in the development of an app process, for the request, which has a "sign" field, the key corresponding to the value is MD5 encrypted field, next to the Android colleague asked Php backstage, said MD5 encryption is 32 or 16 bits, because previously did not notice, so the search under, Now a little summary below:
MD5 is message-digest algorithm 5 (Information-Digest algorithm 5), which is used to ensure complete and consistent information transmission. is one of the widely used hashing algorithms (also translation digest algorithm, hashing algorithm), mainstream programming language has been widely MD5 implemented. MD5 's role is to allow bulk information to be "compressed" into a confidential format before signing a private key with a digital signature software (that is, converting an arbitrary-length byte string into a long hexadecimal string). (quoted from Baidu Encyclopedia)
Note that "a certain length" is generated, how long is this " certain length "? Read a lot of information, including Wikipedia and some forums, said MD5 actually into the algorithm produced is fixed 128bit, that is, 128 0 and 1 of the bits, and in the actual application development, is usually 16 in the output, so exactly 32 bits 16, plainly speaking is 32 16 decimal numbers.
The iOS MD5 encryption method is as follows
#import<commoncrypto/commondigest.h>-(NSString *) MD5: (NSString *) str{Const Char*cstr =[str utf8string]; unsignedCharresult[ -]; CC_MD5 (CStr, strlen (CSTR), result); //The MD5 call 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[Ten], result[ One], result[ A], result[ -], result[ -], result[ the] ]; }
Where%02x is the format control: ' X ' is expressed as 16 binary output, ' 02 ' means less than two bits, the front is 0, if the ' F ' output is 0f, ' 1f3 ' is output 1f3; I'd like to introduce you to the code where result is a character array, why is [ 16], this is because the MD5 algorithm last generated 128 bits, while the smallest storage unit in the computer is byte, 1 bytes is 8 bits, corresponding to a char type, the calculation can require 16 char. So result is [16]. So why is the format of the output must be%02x, not the other one? This is also a reason: because the Convention MD5 is generally in 16 binary format output, then in fact, the problem is converted to 128 0 and 1 in 16 binary, each 4 binary corresponding to a 16 element, you need 32 16 elements, if the element is all 0, put into the array of char, Normal is not output, such as 00001111, to the output of%x, then F, then will be lost 0, but if the output is a%02x representation of the result is 0f, is exactly the correct result of the conversion.
So these are the origins of char[16] and%02x.
As for the 16-bit MD5 encryption that people say, this is actually true: if the resulting MD5 encryption string is: 01234567abcdefababcdefab76543210, The 16-bit MD encryption character is Abcdefababcdefab, that is, it just intercepts the middle 16 bits. In fact, this operation is not included in the MD5 encryption algorithm, but should be the result of the MD5 encryption algorithm two processing. The other 64-bit and uppercase-and-lowercase are all two processing of the results of the MD5 algorithm. Because the MD5 algorithm produces a 128bit,128 binary number.
The above is my MD5 about 16-bit and 32 bit of some simple understanding, hehe.
iOS about MD5 encryption for 32-bit and 16-bit