1
A
回答
3
1)只要確保你添加#import <CommonCrypto/CommonHMAC.h>
對於橋接Objective-C的頭。
import Foundation
extension Data {
func hexEncodedString() -> String {
return map { String(format: "%02hhx", $0) }.joined()
}
}
func digestHMac(signature:String, secret: String) -> String! {
let secretData : Data = secret.data(using: .utf8)!
let signatureData : Data = signature.data(using: .utf8)!
let digest = UnsafeMutablePointer<UInt8>.allocate(capacity:Int(CC_SHA1_DIGEST_LENGTH))
var hmacContext = CCHmacContext()
CCHmacInit(&hmacContext, CCHmacAlgorithm(kCCHmacAlgSHA1), [UInt8](secretData), secretData.count)
CCHmacUpdate(&hmacContext, [UInt8](signatureData), [UInt8](signatureData).count)
CCHmacFinal(&hmacContext, digest)
let cryptData = Data(bytes: digest, count: Int(CC_SHA1_DIGEST_LENGTH))
return cryptData.hexEncodedString()
}
//Your 'API Application Identifier'
let applicaitonID = ""
//Your 'Authentication Key'
let authKey = ""
//Your 'Authentication Secret'
let authSecret = ""
//Your Quickblox API Endpoint
let apiEndpoint = "https://api.quickblox.com/session.json"
//Unix Timestamp It shouldn't be differ from time provided by NTP more than 60 minutes. We suggest you synchronize time on your devices with NTP service.
let timestamp = Int(Date().timeIntervalSince1970)
//Unique Random Value. Requests with the same timestamp and same value for nonce parameter can not be send twice.
let nonce = arc4random_uniform(1000000) + 1;
var payload = [
"application_id" : applicaitonID,
"auth_key": authKey,
"nonce" : String(nonce),
"timestamp" : String(timestamp)
]
//Request body is formed as the sorted (sorting alphabetically, as symbols, not as bytes) by increase the string array 'parameter=value', separated with the symbol "&".
let sortedKeys = Array(payload.keys).sorted(by: <)
var parametersString = ""
for key in sortedKeys {
parametersString = parametersString + key + "=" + payload[key]! + "&"
}
parametersString = parametersString.substring(to: parametersString.index(before: parametersString.endIndex))
let parameters = parametersString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
let hmac = digestHMac(signature: parameters, secret: authSecret)
payload["signature"] = hmac
//Session Request
var request:NSMutableURLRequest = NSMutableURLRequest(url: URL(string: apiEndpoint)!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField:"Content-Type")
request.httpBody = try! JSONSerialization.data(withJSONObject: payload, options: [])
print(payload)
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
let json = try! JSONSerialization.jsonObject(with: data!)
/* Response
{
session = {
"_id" = 589c94c0a28f9a15d7000037;
"application_id" = 39854;
"created_at" = "2017-02-09T16:11:44Z";
"device_id" = 0;
id = 61804;
nonce = 243190;
token = 419e64c861594b63b558b259f8b6e4fd4c009bae;
ts = 1486656703;
"updated_at" = "2017-02-09T16:11:44Z";
"user_id" = 0;
};
}
*/
}
task.resume()
sleep(30)
相關問題
- 1. 如何在Quickblox中的兩個用戶之間創建會話?
- 2. 無法在Android中的Quickblox中創建會話
- 3. 如何使用電子郵件和密碼在quickblox中創建聊天會話
- 4. QuickBlox會話創建:Rails應用程序中的意外簽名
- 5. 如何在ZF2中創建會話
- 6. 如何在Spring Junit中創建會話
- 7. 如何在Catalyst中創建會話?
- 8. 如何在web2py中創建Auth會話?
- 9. 如何在odata中創建會話?
- 10. 會話如何在JSP中創建?
- 11. 如何在angualr2中創建新會話
- 12. 如何管理quickblox會話Javascript SDK
- 13. 如何創建會話並在會話中設置UserBean
- 14. 如何在jsp中創建會話對象和會話變量?
- 15. 如何創建會話(iPhone)?
- 16. 如何創建會話express.js
- 17. 聊天時會創建多個對話框Quickblox
- 18. Quickblox通過意向服務創建會話
- 19. 使用quickblox創建用戶 - 會話令牌生成
- 20. 如何將用戶加入到已在Quickblox中創建的視頻通話會話?
- 21. 創建會話時在Grails中創建會話變量
- 22. 在C#中創建會話#
- 23. 在Django中創建會話
- 24. 在quickblox中創建對話框中的空指針異常
- 25. Quickblox Chat會話問題
- 26. 如何在Windows手機創建會話
- 27. 如何在春季創建新會話?
- 28. 如何停止symfony2創建會話cookie或如何在config.yml中加密會話?
- 29. 如何在創建會話後編輯會話
- 30. 創建會話