2017-02-09 5 views

ответ

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) 
Смежные вопросы