First, download the OpenSSL source code, compiled into a callable library
In order to save everyone's trouble, we provide a ready-made OpenSSL library for everyone: Http://pan.baidu.com/s/1hqpbKpA
Unzip, you can see the Include folder and LIBCRYPTO.A, libssl.a two files.
Ii. New Project OpenSSL
Copy the Include folder to the project root directory,
Copy libcrypto.a and LIBSSL.A to the project root directory
Add the above files to your project.
The final project structure is as follows:
Third, set the value of "Header Search Paths" to/users/huangzhiming/desktop/work/openssl/openssl/include
Set the value of "Valid architectures" to armv7/armv7s
Iv. writing code.
#import <UIKit/UIKit.h>
#import <openssl/md5.h>
void Md5 (NSString *);
int main (int argc, char *argv[]) {
NSAutoreleasePool * Pool = [[NSAutoreleasePool alloc] init];
Md5 (@ "12345");
int retVal = Uiapplicationmain (argc, argv, nil, nil);
[Pool release];
return retVal;
}
void Md5 (NSString * string) {
Input parameter 1: String to generate the MD5 value, nsstring-->uchar*
unsigned char *INSTRG = (unsigned char *) [[string datausingencoding:nsasciistringencoding] bytes];
Input parameter 2: string length
unsigned long lngth = [string length];
Output Parameter 3: MD5 value to return, md5_digest_length to 16bytes, bits
unsigned char result[md5_digest_length];
A temporary nsstring variable used to assemble uchar* into a string that can be displayed: 2 characters selector byte of 16 binary number
nsmutablestring *OUTSTRG = [nsmutablestring string];
Call the OpenSSL function
MD5 (INSTRG, lngth, result);
unsigned int i;
for (i = 0; i < md5_digest_length; i++)
{
[OUTSTRG AppendFormat: @ "%02x", Result[i]];
}
NSLog (@ "Input string:%@", string);
NSLog (@ "md5:%@", OUTSTRG);
}
Run, you can see the output as follows:
Input string:12345
md5:827ccb0eea8a706c4c34a16891f84e7b
Using OpenSSL under iOS8.1