Swift中的Coinbase身份验证签名

时间:2018-10-02 16:17:12

标签: swift coinbase-api moya

我正在尝试与Coinbase API服务器通信。我想我已经尝试了HMAC,密钥,签名,时间等的所有排列,但是无法通过身份验证。

我正在使用AlamoFire和Moya进行REST调用:

这是我最新的HMac代码,可以验证:

    func getHMacSHA256(forMessage message: String, key: String) -> String? {
    let hMacVal = HMAC(algorithm: HMAC.Algorithm.sha256, key: key).update(string: message)?.final()
    if let encryptedData = hMacVal {
        let decData = NSData(bytes: encryptedData, length: Int(encryptedData.count))
        let base64String = decData.base64EncodedString(options: .lineLength64Characters)
        print("base64String: \(base64String)")
        return base64String
    } else {
        return nil
    }
}

接下来,我们有创建标头的代码:

   var headers: [String:String]? {

    let ts = Int64(Date().timeIntervalSince1970)

    print(UserDefaults.standard.string(forKey: "UserUUID")!)
    let appDelegate = UIApplication.shared.delegate as! AppDelegate


        let prehash = "\(ts)" + method.rawValue + "/v2/" + path// + finalBody
        print (prehash)

        let signature  =   getHMacSHA256(forMessage: prehash, key: appDelegate.tempCredential.apiSecret)!
        let obj =  [
                "content-type" : "application/json",
                "CB-ACCESS-KEY" : appDelegate.tempCredential.apiKey,
                "CB-ACCESS-SIGN" : signature,
                "CB-ACCESS-TIMESTAMP": "\(ts)"
                ]

        print(obj)
    return obj
}

有人看到我在做什么错吗?

谢谢

0 个答案:

没有答案