您的位置:首页 > 移动开发 > IOS开发

在你的 iOS App中 使用 OpenSSL 库 转发

2011-10-11 11:17 417 查看
英文原文链接:http://www.x2on.de/2010/07/13/tutorial-iphone-app-with-compiled-openssl-1-0-0a-library/

下文有错误 参照有风险:需要修改 输入命令行的部分 建议用英文原版里的!!!

在你的 iOS App中 使用 OpenSSL 库

——译自x2on的“Tutorial: iPhone Appwith compiled OpenSSL 1.0.0a Library”

原文地址:http://www.x2on.de/2010/07/13/tutorial-iphone-app-with-compiled-openssl-1-0-0a-library/,本文有少许地方做了调整。

1、下载OpenSSL源代码库:
http://www.openssl.org/source/
当前最新版本1.0.0d。

下载后,将其中的 openssl-1.0.0x 目录解压出来放在合适的地方。

2、编译OpenSSL

openssl是一个c语言函数库,为方便在Xcode中使用,我们需要把它编译为静态库。

打开crypto/ui/ui_openssl.c进行编辑。



static volatile sig_atomic_t intr_signal;

修改为:

static volatile int intr_signal;

否则会出现一个编译错误。

2.1 编译 i386 库(用于iPhone模拟器)

执行以下命令:

mkdir ssllibs

将在用户主目录下建立ssllibs目录。

切换到openssl-1.0.0a安装(解压)目录,在其下建立3个子目录:

cd openssl-1.0.0a

mkdir openssl_armv6 openssl_armv7 openssl_i386

执行目录下的congfigure:

./configure BSD-generic32--openssldir=/Users//openssl-1.0.0a/openssl_i386

编辑 makefile 文件,找到:

CC= gcc

修改为:

CC=/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc -arch i386

下一行,在CFLAG = 的后面增加

-isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk

进行编译:

make

make install

检查 openssl_i386/lib目录下 libcrypto.a和 libssl.a 是否生成。

2.2 编译 armv6 库(armv6架构的iOS使用)

先将编译好的 i386 库保存到 ssllibs 目录:

mv openssl_i386 ../ssllibs

清除上次编译的配置:

make clean

执行configure,重新生成新的编译配置:

./configure BSD-generic32--openssldir=/Users//openssl-1.0.0a/openssl_armv6

修改 makefile 文件,将 CC=gcc修改为:

CC= /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-arch armv6

注意,这里是iPhoneOS.platform而不是先前的 iPhoneSimulator.platform了。

同样,需要在CFLAG=后面加上:

-isysroot/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk

可以进行编译了:

make

make install

检查 openssl_armv6/lib 目录下 libcrypto.a和 libssl.a 是否生成。

2.3 编译 armv7 库(armv7 架构的 iOS 使用)

先将先前编译好的 armv6 库移到 ssllibs 目录。

mv openssl_armv6 ../ssllibs

清除前面编译配置:

make clean

执行configure配置编译环境:

./configure BSD-generic32 --openssldir=/Users//openssl-1.0.0a/openssl_armv7

修改 makefile 文件,将 CC=cc修改为:

CC= /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-arch armv7

注意,gcc 编译选项 arch 由 armv6 变为了 armv7。

同时,在CFLAG=后面添加:

-isysroot/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk

进行编译:

make make

install

检查 openssl_armv7/lib 目录下 libcrypto.a和 libssl.a 是否生成。

把编译结果移到ssllibs目录:

mv openssl_armv7 ../ssllibs

2.4 制作“通用”静态库

通用静态库是一个“多架构”文件,它是多个单一架构静态库的融合。

制作“通用”静态库需要使用 Mac OS X 的 lipo 命令(具体请参考Mac OS X 手册)。

合并 libcrypto.a 库:

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

合并 libssl.a 库:

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、在 Xcode 项目的进行设置

把 OpenSSL 的 include 目录拷贝到项目文件夹。

把 libcrypto.a 和 libssl.a 文件拷贝到项目文件夹。

把 libcrypto.a 和 libssl.a 文件拖到项目的Framework 组中。

在 target 上右键,选择 Get Info,将 LibrarySearch Path 设置为:

$(inherited) “$(SRCROOT)”

将 User Header Search Paths 设为include。

选中 Always Search User Paths 选项。

现在可以在你的iPhone项目中实用OpenSSL了。

4、写一个应用 OpenSSL 的小例子

新建 Window-based application,命名为OpenSSLTest.

“AddàExisting FrameworksàOthers…”,把libssl.a和libcrypto.a加进来(即我们前面制作的“通用”库)。

打开项目info 的 Build 设置,在 HeaderSearch Paths 中加入 OpenSSL 的头文件路径,如:

/Users//Library/openssl-1.0.0a/include

注意,勾上“Recursive”(搜索子目录)。

接下来写点简单的代码。为求省事,我们把所有代码写在main.m里:

#import

#include

voidMd5(NSString*);

intmain(intargc, char*argv[]) {

NSAutoreleasePool* pool = [[NSAutoreleasePoolalloc] init];

Md5(@"12345");

intretVal = UIApplicationMain(argc, argv, nil, nil);

[pool release];

returnretVal;

}

voidMd5(NSString* string){

// 输入参数1:要生成md5值的字符串,NSString-->uchar*

unsignedchar*inStrg = (unsignedchar*)[[string dataUsingEncoding:NSASCIIStringEncoding] bytes];

// 输入参数2:字符串长度

unsignedlonglngth =[string length];

// 输出参数3:要返回的md5值,MD5_DIGEST_LENGTH为16bytes,128 bits

unsignedcharresult[MD5_DIGEST_LENGTH];

// 临时NSString变量,用于把uchar* 组装成可以显示的字符串:2 个字符一byte 的16 进制数

NSMutableString*outStrg =[NSMutableStringstring];

// 调用OpenSSL 函数

MD5(inStrg,lngth, result);

unsignedinti;

for(i = 0; i < MD5_DIGEST_LENGTH; i++)

{

[outStrg appendFormat:@"%02x", result];

}

NSLog(@"inputstring:%@",string);

NSLog(@"md5:%@",outStrg);

}

你可以在控制台查看程序的输出:

inputstring:12345

md5:827ccb0eea8a706c4c34a16891f84e7b

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  职场 iphone 休闲