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* 组装成可以显示的字符串: 2 个字符一 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
iOS App中 使用 OpenSSL 库的更多相关文章
- Android app中的so库和CPU架构
一.android目前有几种cpu架构? 早期的Android系统几乎只支持ARMv5的CPU架构,目前支持七种CPU架构:ARMv5,ARMv7 (从2010年起),x86 (从2011年起),MI ...
- iOS 在Xcode中使用OpenSSL库
最近要做一个密码键盘,想内置一些加密算法,所以就想到了添加OpenSSL库,现在mac也自带了OpenSSL库,但是每次都从终端是生成是很麻烦的.网上找了很多文档.博客去介绍如何编译可以在Xcode中 ...
- 在iOS APP中使用H5显示百度地图时如何支持HTTPS?
现象: 公司正在开发一个iOSAPP,使用h5显示百度地图,但是发现同样的H5页面,在安卓可以显示出来,在iOS中就显示不出来. 原因分析: 但是现在iOS开发中,苹果已经要求在APP中的所有对外连接 ...
- iOS开发中使用静态库 .a 文件
iOS开发中,在使用一些第三方库时,可能是一个静态库(比如GPUImage).这种情况下,需要编译出静态库文件(.a) ,然后配合响应的头文件(.h 文件)使用. 编译静态库,直接在Xcode中编 ...
- 在iOS App 中添加启动画面
你可以认为你需要为启动画面编写代码,然而Apple 让你可以非常简单地在Xcode中完成.不需要编写代码,你仅需要在Xcode中进行一些配置. 1.什么是启动画面(Splash Screen)? 启动 ...
- iOS——Xcode中添加第三方库
一.只有.h和.a文件的库 1.向项目中添加三方库文件 如果添加的第三方库只有.h和.a文件,直接把文件夹拖进项目下面,这时会弹出下面的提示框,一定要勾选下面选择的选项: 这里要注意,在Add to ...
- 关于iOS APP中网络层的设计
在iOS开发中,请求网络数据,处理获得的数据是很常见的功能,但是很少有资料会讨论关于网络的处理应该放在MVC中得哪个层中. 我在网上Google了一番,记下了几个觉得比较不错的链接.现记录如下: ht ...
- 五分钟,运用cocoaui库,搭建主流iOS app中我的界面
本项目基于天天团购项目,在上一篇中有说到! 首先介绍一些cocoaui,是国内的一名程序员做的开源的开源系统,目的是为了简化ios布局!官网地址:www.cocoaui.com,github地址:ht ...
- [置顶] ios App 中嵌入应用商店
昨晚同事拿了一个app 发现其app 内部页面打开了appstore 并没有唤起手机自带的appstore, 刚开始以为是用webview 加载的 ,可是自己些了一个demo 发现并不是那样一回事 用 ...
随机推荐
- Spring boot @Transactional
1.注解@Transactional 2.异常回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); @Ov ...
- Django模板自定义标签和过滤器,模板继承(extend),Django的模型层
上回精彩回顾 视图函数: request对象 request.path 请求路径 request.GET GET请求数据 QueryDict {} request.POST POST请求数据 Quer ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 【转】解决ajax跨域问题的5种解决方案
转自: https://blog.csdn.net/itcats_cn/article/details/82318092 什么是跨域问题?跨域问题来源于JavaScript的"同源策略& ...
- 利用python进行数据分析--numpy基础
随书练习,第四章 NumPy基础:数组和矢量运算 # coding: utf-8 # In[1]: # 加注释的三个方法1.用一对"""括起来要注释的代码块. # 2. ...
- buildroot
http://buildroot.uclibc.org/downloads/snapshots/buildroot-snapshot.tar.bz2 简介 buildroot是一个Makefiles和 ...
- 测开之路一百一十三:bootstrap媒体对象
实现效果,左边是图片或者其他媒体,右边是对应的描述 引入bootstrap和jquery标签 class="media" 数量多一些看着就会很规整 <!DOCTYPE htm ...
- idea的热部署
1:先找到你要热部署的tomcat之后 ,在设置tomcat时 先选择 server,里面有On 'Update' action () 和 On frame deactivation 这两项 都 ...
- 剑指offer--day05
1.1 题目:二进制中1的个数:输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 1.2 解题思路:通过位移来进行.举例:7(二进制:0111),7 >> 1(得到:001 ...
- ecshop注册用户增加手机验证功能
1.去掉“用户名”注册 a.去掉提交 user_passport.dwt页面去掉 <input name="username" type="text" s ...