在你的 iOS App中 使用 OpenSSL 库 转发
在你的 iOS App中 使用 OpenSSL 庫 轉發
英文原文鏈接: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/<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,將 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/<yourname>/Library/openssl-1.0.0a/include
注意,勾上“Recursive”(搜索子目錄)。
接下來寫點簡單的代碼。為求省事,我們把所有代碼寫在main.m里:
#import?<UIKit/UIKit.h>
#include?<Openssl/md5.h>
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
?
?
?
下文僅供參考:并不實用
http://atastypixel.com/blog/easy-inclusion-of-openssl-into-iphone-app-projects/
Easy inclusion of OpenSSL into iOS projects
?
Oddly, iOS doesn’t provide any OpenSSL implementation at all — If you want to do anything with crypto (like checking signatures, checksumming, etc.), you have to build in the library yourself.
I came across a great?XCode project wrapper?for OpenSSL yesterday, by Stephen Lombardo. This is an XCode project file that contains a target to build OpenSSL from source, and works with both Mac and iOS projects. I made some?modifications?to it, in order to make it work by just dropping in the OpenSSL source tarball, without having to dirty up your source tree with the extracted OpenSSL distribution.
Here’s how to use it:
On the Build tab for your project’s target, find the “Header Search Paths” option, and add the path:
$(SRCROOT)/Library/openssl/build/openssl.build/openssl/include
(Assuming you’ve put openssl.xcodeproj at the path?Library/openssl?— adjust as necessary).
Then, you can just import and use as normal (#import <openssl/dsa.h>, etc).
Download it here
?
總結
以上是生活随笔為你收集整理的在你的 iOS App中 使用 OpenSSL 库 转发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求大神解决下这个题目!做好请发到3089
- 下一篇: vtun中setsockopt fcn