開源框架 RSA_Swift
RSA_Swift
RSA_Swift 是一款輕量級的 Swift
版本的框架,框架功能包括: RSA
加密/解密字串、 RSA
加密/解密 Data
、字串的 MD5
、檔案的 MD5
值的獲取。
寫 RSA_Swift 初衷?
github
上 Swift
版本的 RSA
加密/解密框架也有,但最近使用的幾個,總是會出現這樣或那樣的問題,所以就寫了這個框架,附帶的加上比較常見的功能:字串的 MD5
、檔案的 MD5
值的獲取。
對於檔案的 MD5
值的獲取,是將檔案分塊讀出並且計算 MD5
值的方法,有別於檔案一次性讀出並且計算 MD5
值的方法。
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Installation
RSA_Swift is available through CocoaPods . To install
it, simply add the following line to your Podfile:
pod 'RSA_Swift'
使用姿勢
rsa
加密字串和解密字串
/// 注:rsa 加密 Data 和解密 Data 方法使用與`rsa 加密字串和解密字串`類似,這裡就不寫示例程式碼了 let filePath = Bundle.main.path(forResource: "public_key", ofType: "der") let encryptString = "abcdefg" print("要加密的字串:\(encryptString)") /// Encrypt RSA.rsaEncrypt(filePath, encryptString) { (encryptedString) in print("加密後的字串:\(encryptedString ?? "")") let filePath1 = Bundle.main.path(forResource: "private_key.p12", ofType: nil) /// Decrypt RSA.rsaDecrypt(filePath1, "ios", encryptedString, { (decryptedString) in print("解密後的字串:\(decryptedString ?? "")") }) }
獲取檔案的 MD5
值
guard let filePath = Bundle.main.path(forResource: "test_file_md5", ofType: "png") else { return } print("檔案的 MD5 值:\(filePath.md5_File() ?? "")")
字串的 MD5
let str = "字串的MD5" print("字串的MD5:\(str.md5())")
License
RSA_Swift is available under the MIT license. See the LICENSE file for more info.
Author
如果你有什麼建議,可以關注我的公眾號: iOS開發者進階
,直接留言,留言必回。
