用于Swift 3.0中OAuth签名的HMAC-SHA1

时间:2017-05-18 06:09:17

标签: ios swift oauth

我想从基本字符串URL生成oauth 1.0签名但我从算法中获取无效签名。我的输出如下。

code = "woocommerce_rest_authentication_error";
data =     {
    status = 401;
};
message = "Invalid signature - provided signature does not match.";

任何人都可以帮助我吗?感谢。

 var strURLForSignature : String = String.init(format: "oauth_consumer_key=%@&oauth_nonce=%@&oauth_signature_method=%@&oauth_timestamp=%ld&oauth_version=1.0",API_consumer_key, nonce, "HMAC-SHA1", timestamp)
    print(strURLForSignature)

    strURLForSignature = strURLForSignature.replacingOccurrences(of: "/", with: "%2F")
    strURLForSignature = strURLForSignature.replacingOccurrences(of: ":", with: "%3A")
    strURLForSignature = strURLForSignature.replacingOccurrences(of: "&", with: "%26")
    strURLForSignature = strURLForSignature.replacingOccurrences(of: "=", with: "%3D")

    strURLForSignature = String.init(format: "%@%@", API_MAIN_URL_Oauth,strURLForSignature)

    strURLForSignature = strURLForSignature.replacingOccurrences(of: "/", with: "%2F")
    strURLForSignature = strURLForSignature.replacingOccurrences(of: ":", with: "%3A")
    strURLForSignature = strURLForSignature.replacingOccurrences(of: "=", with: "%3D")

    print(strURLForSignature)

    guard let keyData = key.data(using: String.Encoding.utf8),
        let stringData = strURLForSignature.data(using: String.Encoding.utf8),
        let outputData = NSMutableData(length: Int(CC_SHA1_DIGEST_LENGTH)) else {
            return
    }
    outputData.length = Int(CC_SHA1_DIGEST_LENGTH)
    let tempData: NSMutableData = NSMutableData(length: 26)!
    keyData.withUnsafeBytes {
        tempData.replaceBytes(in: NSMakeRange(0, keyData.count), withBytes: $0)
    }
    CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1),
           tempData.bytes, keyData.count,
           tempData.bytes, stringData.count,
           outputData.mutableBytes)
    outhSignature = outputData
        .base64EncodedString(options: [])
    print(outputData
        .base64EncodedString(options: []))
    print(outhSignature)

0 个答案:

没有答案