Use the OpenSSL library in the IOS app

Source: Internet
Author: User
Tags openssl library uikit

Transferred from: http://blog.csdn.net/kmyhy/article/details/6534067

Use the OpenSSL library in your IOS app

--"Tutorial:iphone App with compiled OpenSSL 1.0.0a Library" translated from X2on

Original address: http://www.x2on.de/2010/07/13/tutorial-iphone-app-with-compiled-openssl-1-0-0a-library/, this article has a little place to make adjustments.

1. Download the OpenSSL source code library:

http://www.openssl.org/source/

Current Latest version 1.0.0d.

After downloading, unzip the openssl-1.0.0x directory and put it in the appropriate place.

2. Compiling OpenSSL

OpenSSL is a C-language function library that we need to compile into a static library for ease of use in Xcode.

Open crypto/ui/ui_openssl.c for editing.

Will

static volatile sig_atomic_t intr_signal;

Modified to:

static volatile int intr_signal;

Otherwise a compilation error will occur.

2.1 Compiling the I386 library (for iphone simulator)

Execute the following command:

mkdir Ssllibs

The Ssllibs directory will be established under the user's home directory.

Switch to the openssl-1.0.0a install (unzip) directory, under which 3 subdirectories are created:

CD openssl-1.0.0a

mkdir openssl_armv6 openssl_armv7 openssl_i386

Execute the Congfigure in the directory:

./configure BSD-GENERIC32--openssldir=/users/<username>/openssl-1.0.0a/openssl_i386

Edit Makefile file to find:

cc= GCC

Modified to:

Cc=/developer/platforms/iphonesimulator.platform/developer/usr/bin/gcc-arch i386

Next line, added after Cflag =

-isysroot/developer/platforms/iphonesimulator.platform/developer/sdks/iphonesimulator4.0.sdk

To compile:

Make

Make install

Check whether LIBCRYPTO.A and LIBSSL.A are generated under the Openssl_i386/lib directory.

2.2 Compiling the ARMV6 library (for iOS using ARMV6 architecture)

Save the compiled I386 library to the Ssllibs directory first:

MV Openssl_i386. /ssllibs

Clear the last compiled configuration:

Make clean

Execute configure to regenerate the new compilation configuration:

./configure BSD-GENERIC32--openssldir=/users/<username>/openssl-1.0.0a/openssl_armv6

Modify the makefile file to modify the CC=GCC to:

cc=/developer/platforms/iphoneos.platform/developer/usr/bin/gcc-arch ARMv6

Note that this is iphoneos.platform and not the previous iphonesimulator.platform.

Again, you need to add the following cflag=:

-isysroot/developer/platforms/iphoneos.platform/developer/sdks/iphoneos4.0.sdk

It is possible to compile:

Make

Make install

Check whether LIBCRYPTO.A and LIBSSL.A are generated under the Openssl_armv6/lib directory.

2.3 Compiling the ARMV7 library (for IOS using ARMV7 architecture)

First, move the previously compiled ARMV6 library to the Ssllibs directory.

MV Openssl_armv6. /ssllibs

Clear the previous compilation configuration:

Make clean

To perform a configure configuration compilation environment:

./configure BSD-GENERIC32--openssldir=/users/<username>/openssl-1.0.0a/openssl_armv7

Modify the makefile file to modify the CC=CC to:

cc=/developer/platforms/iphoneos.platform/developer/usr/bin/gcc-arch ARMv7

Note that the GCC compilation option for arch changed from ARMv6 to ARMV7.

At the same time, add the following cflag=:

-isysroot/developer/platforms/iphoneos.platform/developer/sdks/iphoneos4.0.sdk

To compile:

Make make

Install

Check whether LIBCRYPTO.A and LIBSSL.A are generated under the Openssl_armv7/lib directory.

To move the compilation results to the Ssllibs directory:

MV Openssl_armv7. /ssllibs

2.4 Making a "common" static library

The Universal Static Library is a "multi-schema" file, which is a fusion of multiple single-schema static libraries.

Making a "common" static library requires the Lipo command of Mac OS X (refer to the Mac OS X manual for details).

Merging LIBCRYPTO.A libraries:

Lipo-create. /ssllibs/openssl_i386/lib/libcrypto.a. /ssllibs/openssl_armv6/lib/libcrypto.a. /ssllibs/openssl_armv7/lib/libcrypto.a-output. /ssllibs/libcrypto.a

Merging LIBSSL.A libraries:

Lipo-create. /ssllibs/openssl_i386/lib/libssl.a. /ssllibs/openssl_armv6/lib/libssl.a. /ssllibs/openssl_armv7/lib/libssl.a-output. /ssllibs/libssl.a

3, in the Xcode project to set up

Copy the OpenSSL include directory to the project folder.

Copy the LIBCRYPTO.A and LIBSSL.A files to the project folder.

Drag the libcrypto.a and LIBSSL.A files into the project's Framework group.

Right-click on target and select Get Info to set the Library Search Path to:

$ (inherited) "$ (srcroot)"

Set the User Header Search Paths to include.

Select the Always Search User Paths option.

You can now use OpenSSL in your iphone project.

4. Write a small example of using OpenSSL

Create a new window-based application, named Openssltest.

"Addàexisting frameworksàothers ...", put LIBSSL.A and libcrypto.a in (the "universal" library we made earlier).

Open the Build setting for project info, and add OpenSSL's header file path to header Search Paths, such as:

/users/<yourname>/library/openssl-1.0.0a/include

Note that "Recursive" (search subdirectories) is ticked.

Next, write some simple code. For the sake of convenience, we write all the code in MAIN.M:

#import <UIKit/UIKit.h>

#include <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);

}

You can view the output of the program from the console:

Input string:12345

md5:827ccb0eea8a706c4c34a16891f84e7b

Use the OpenSSL library in the IOS app

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.