HMAC-SHA1 for OAuth Signature in Swift

I am trying to make an OAuth module for Swift 2 in my application. I get an HMAC-SHA1 signature signature because my function returns an invalid base64 hmac-sha1 for signature. Can you help me? What's wrong?

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([])
}

For check:

String for encode: in code

Correct: O8UjUTYX1UxKF93KaY/mta9HETs=
My (incorrect): f5elSONqP6nPdpgBIyroJTCN19s=

The correct encoder is here (javascript): https://oauth.googlecode.com/svn/code/javascript/example/signature.html

+4
source share
1 answer

Your method should return the correct result as it is.

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

HMAC-SHA1 HMAC-SHA1, [RFC2104], , key - ( ) strong > , "& ( ASCII 38), .

"efgh&mnop".

+3

Source: https://habr.com/ru/post/1605945/


All Articles