在你的 iOS App中 使用 OpenSSL 库

转自:http://blog.csdn.net/kmyhy/article/details/6534067

在你的 iOS App中 使用 OpenSSL 库

——译自x2on的“Tutorial: iPhone App with 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/<username>/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/<username>/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/<username>/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,将 Library Search 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 设置,在 Header Search Paths 中加入 OpenSSL 的头文件路径,如:

/Users/<yourname>/Library/openssl-1.0.0a/include

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

接下来写点简单的代码。为求省事,我们把所有代码写在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){

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

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

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

unsigned long lngth = [string length ];

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

unsigned char result[ MD5_DIGEST_LENGTH ];

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

NSMutableString *outStrg = [ NSMutableString string ];

// 调用 OpenSSL 函数

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

}

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

input string:12345

md5:827ccb0eea8a706c4c34a16891f84e7b


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值