用于Swift中OAuth签名的HMAC-SHA1

用于Swift中OAuth签名的HMAC-SHA1,第1张

概述我正在尝试在我的应用程序中为 Swift 2制作OAuth模块.我坚持使用HMAC-SHA1签名生成,因为我的函数返回错误的base64 hmac-sha1进行签名.你能帮帮我吗?怎么了? func URLEncodedKey() -> String? { let key = "efgh" let string = "GET&http%3A%2F%2Fhost.net%2Fr 我正在尝试在我的应用程序中为 Swift 2制作OAuth模块.我坚持使用HMAC-SHA1签名生成,因为我的函数返回错误的base64 hmac-sha1进行签名.你能帮帮我吗?怎么了?

func URLEncodedKey() -> String? {       let key = "efgh"    let string = "GET&http%3A%2F%2Fhost.net%2Fresource&name%3Dvalue%26name%3Dvalue%26oauth_consumer_key%3Dabcd%26oauth_nonce%3DxUwWEy3LVsI%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1441375089%26oauth_token%3Dijkl%26oauth_version%3D1.0"    guard let keyData = key.dataUsingEnCoding(NSUTF8StringEnCoding),stringData = string.dataUsingEnCoding(NSUTF8StringEnCoding),outputData = NSMutableData(length: Int(CC_SHA1_DIGEST_LENGTH)) else {            return nil    }    outputData.length = Int(CC_SHA1_DIGEST_LENGTH)    CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1),keyData.bytes,keyData.length,stringData.bytes,stringData.length,outputData.mutableBytes)    return outputData        .base64EncodedStringWithOptions([])}

用于检查:

String for encode: in codeCorrect: O8UjUTYX1UxKF93KaY/mta9HETs=My (incorrect): f5elSONqP6nPdpgBIyroJTCN19s=

正确的编码器在这里(JavaScript):https://oauth.googlecode.com/svn/code/javascript/example/signature.html

解决方法 您的方法必须按原样返回正确的结果.

见http://oauth.net/core/1.0/#signing_process:

The HMAC-SHA1 signature method uses the HMAC-SHA1 signature algorithm as defined in [RFC2104] where the Signature Base String is the text and the key is the concatenated values (each first encoded per Parameter EnCoding) of the Consumer Secret and Token Secret,separated by an ‘&’ character (ASCII code 38) even if empty.

在这种情况下,关键应该是“efgh& mnop”.

总结

以上是内存溢出为你收集整理的用于Swift中OAuth签名的HMAC-SHA1全部内容,希望文章能够帮你解决用于Swift中OAuth签名的HMAC-SHA1所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1003884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存