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 发现并不是那样一回事 用 ...
 
随机推荐
- scanf()函数原理
			
一.三点说明 1.用户输入的字符,会以ASCII码形式存储在键盘缓冲区:2.每调用一次scanf函数,就从键盘缓冲区读走一个字符,相当于清除缓冲区:3.若用户一次输入n个字符,则前n次调用scanf函 ...
 - 在 mac 系统上安装 python 的 MySQLdb 模块
			
在 mac 系统上安装 python 的 MySQLdb 模块 特别说明:本文主要参考了Mac系统怎么安装MySQLdb(MySQL-Python) 第 1 步:下载 MySQL-python-1.2 ...
 - How To Use the Widget Factory  使用widget factory创建插件
			
To start, we'll create a progress bar that just lets us set the progress once. 创建一个基于widget factory ...
 - 玩转visual studio系列之类设计图
			
类设计图 vs 类设计图 微软 安装好vs2017后,在打开的IDE中,随便选择一个文件夹创建[类图],却发现没有该选项 类图辅助器 解决方案 1.点击[工具]选项卡,在下拉的菜单中选择第一项 获取 ...
 - Python编程:从入门到实践—变量和简单数据类型
			
变量的命名和使用 #!/usr/bin/env python# -*- encoding:utf-8 -*- message ="Hello Python world!"print ...
 - TreeSet 源码分析
			
TreeSet 1)底层由 TreeMap 支持的 Set 接口实现,Set 中的元素按照自然顺序或指定的比较器排序. 创建实例 /** * 支持此 Set 的底层的 TreeMap 对象 */ pr ...
 - CopyOnWriteArrayList 源码分析
			
CopyOnWriteArrayList CopyOnWriteArrayList 能解决什么问题?什么时候使用 CopyOnWriteArrayList? 1)CopyOnWriteArrayLis ...
 - CSS镜像
			
https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scaleX transform: scaleX(-1);/*左 ...
 - centos修改时区,同步时间
			
查看当前系统时区 ls -la /etc/localtime 查看支持的时区 timedatectl list-timezones # 查看所有时区 timedatectl list-timezone ...
 - Python 是如何进行内存管理的?python 的程序会内存泄露吗?说说有没有什么方面防止或检测内存泄露?
			
Python GC主要使用 引用计数 来跟踪和回收垃圾.在引用计数的基础上,通过“标记-清除”解决容器对象可能产生的循环引用问题.通过分代 以空间换时间的方法提高垃圾回收效率 引用计数: 每个对象中都 ...