[GH-ISSUE #6] Suggested method of installing framework #3

Closed
opened 2026-03-03 16:44:41 +03:00 by kerem · 2 comments
Owner

Originally created by @foffer on GitHub (Nov 24, 2014).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/6

What's the suggested way of installing this Framework? If you just drag and drop files in the SourceKitService crashes continuously

Edit: The demo project crashes sourcekit as well

Originally created by @foffer on GitHub (Nov 24, 2014). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/6 What's the suggested way of installing this Framework? If you just drag and drop files in the SourceKitService crashes continuously Edit: The demo project crashes sourcekit as well
kerem closed this issue 2026-03-03 16:44:42 +03:00
Author
Owner

@foffer commented on GitHub (Nov 24, 2014):

Ok, so I don't know how to make a pull request, but I think i made a work around... And whilst doing so, keeping everything swift...
I think the problem is the import of ObjC headers, it seems to crash everything when working with this Framework, so I converted it to swift

Found a SWIFT version of a HMACAlgorithm on SO and costumised it a bit:

enum HMACAlgorithm {
    case MD5, SHA1, SHA224, SHA256, SHA384, SHA512

    func toCCHmacAlgorithm() -> CCHmacAlgorithm {
        var result: Int = 0
        switch self {
        case .MD5:
            result = kCCHmacAlgMD5
        case .SHA1:
            result = kCCHmacAlgSHA1
        case .SHA224:
            result = kCCHmacAlgSHA224
        case .SHA256:
            result = kCCHmacAlgSHA256
        case .SHA384:
            result = kCCHmacAlgSHA384
        case .SHA512:
            result = kCCHmacAlgSHA512
        }
        return CCHmacAlgorithm(result)
    }

    func digestLength() -> Int {
        var result: CInt = 0
        switch self {
        case .MD5:
            result = CC_MD5_DIGEST_LENGTH
        case .SHA1:
            result = CC_SHA1_DIGEST_LENGTH
        case .SHA224:
            result = CC_SHA224_DIGEST_LENGTH
        case .SHA256:
            result = CC_SHA256_DIGEST_LENGTH
        case .SHA384:
            result = CC_SHA384_DIGEST_LENGTH
        case .SHA512:
            result = CC_SHA512_DIGEST_LENGTH
        }
        return Int(result)
    }
}
class DataHMACConverter: NSObject {

    class func hmacData(algorithm: HMACAlgorithm, keyData: NSData, data: NSData) -> String {

        var result = [CUnsignedChar](count: Int(algorithm.digestLength()), repeatedValue: 0)
        CCHmac(algorithm.toCCHmacAlgorithm(), keyData.bytes, UInt(keyData.length), data.bytes, UInt(data.length), &result)
        var hmacData:NSData = NSData(bytes: result, length: (Int(algorithm.digestLength())))
        var hmacBase64 = hmacData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding76CharacterLineLength)
        return hmacBase64
    }
}

And then in OAuthSwiftClient:

class func oauthSignatureForMethod(method: String, url: NSURL, parameters: Dictionary<String, AnyObject>, credential: OAuthSwiftCredential) -> String {
        var tokenSecret: NSString = ""
        tokenSecret = credential.oauth_token_secret.urlEncodedStringWithEncoding(dataEncoding)

        let encodedConsumerSecret = credential.consumer_secret.urlEncodedStringWithEncoding(dataEncoding)

        let signingKey = "\(encodedConsumerSecret)&\(tokenSecret)"
        let signingKeyData = signingKey.dataUsingEncoding(dataEncoding)

        var parameterComponents = parameters.urlEncodedQueryStringWithEncoding(dataEncoding).componentsSeparatedByString("&") as [String]
        parameterComponents.sort { $0 < $1 }

        let parameterString = "&".join(parameterComponents)
        let encodedParameterString = parameterString.urlEncodedStringWithEncoding(dataEncoding)

        let encodedURL = url.absoluteString!.urlEncodedStringWithEncoding(dataEncoding)

        let signatureBaseString = "\(method)&\(encodedURL)&\(encodedParameterString)"
        let signatureBaseStringData = signatureBaseString.dataUsingEncoding(dataEncoding)

        let signature = DataHMACConverter.hmacData(HMACAlgorithm.SHA1, keyData: signingKeyData!, data: signatureBaseStringData!)

        return signature

    }
<!-- gh-comment-id:64276589 --> @foffer commented on GitHub (Nov 24, 2014): Ok, so I don't know how to make a pull request, but I think i made a work around... And whilst doing so, keeping everything swift... I think the problem is the import of ObjC headers, it seems to crash everything when working with this Framework, so I converted it to swift Found a SWIFT version of a HMACAlgorithm on SO and costumised it a bit: ``` enum HMACAlgorithm { case MD5, SHA1, SHA224, SHA256, SHA384, SHA512 func toCCHmacAlgorithm() -> CCHmacAlgorithm { var result: Int = 0 switch self { case .MD5: result = kCCHmacAlgMD5 case .SHA1: result = kCCHmacAlgSHA1 case .SHA224: result = kCCHmacAlgSHA224 case .SHA256: result = kCCHmacAlgSHA256 case .SHA384: result = kCCHmacAlgSHA384 case .SHA512: result = kCCHmacAlgSHA512 } return CCHmacAlgorithm(result) } func digestLength() -> Int { var result: CInt = 0 switch self { case .MD5: result = CC_MD5_DIGEST_LENGTH case .SHA1: result = CC_SHA1_DIGEST_LENGTH case .SHA224: result = CC_SHA224_DIGEST_LENGTH case .SHA256: result = CC_SHA256_DIGEST_LENGTH case .SHA384: result = CC_SHA384_DIGEST_LENGTH case .SHA512: result = CC_SHA512_DIGEST_LENGTH } return Int(result) } } class DataHMACConverter: NSObject { class func hmacData(algorithm: HMACAlgorithm, keyData: NSData, data: NSData) -> String { var result = [CUnsignedChar](count: Int(algorithm.digestLength()), repeatedValue: 0) CCHmac(algorithm.toCCHmacAlgorithm(), keyData.bytes, UInt(keyData.length), data.bytes, UInt(data.length), &result) var hmacData:NSData = NSData(bytes: result, length: (Int(algorithm.digestLength()))) var hmacBase64 = hmacData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding76CharacterLineLength) return hmacBase64 } } ``` And then in `OAuthSwiftClient`: ``` class func oauthSignatureForMethod(method: String, url: NSURL, parameters: Dictionary<String, AnyObject>, credential: OAuthSwiftCredential) -> String { var tokenSecret: NSString = "" tokenSecret = credential.oauth_token_secret.urlEncodedStringWithEncoding(dataEncoding) let encodedConsumerSecret = credential.consumer_secret.urlEncodedStringWithEncoding(dataEncoding) let signingKey = "\(encodedConsumerSecret)&\(tokenSecret)" let signingKeyData = signingKey.dataUsingEncoding(dataEncoding) var parameterComponents = parameters.urlEncodedQueryStringWithEncoding(dataEncoding).componentsSeparatedByString("&") as [String] parameterComponents.sort { $0 < $1 } let parameterString = "&".join(parameterComponents) let encodedParameterString = parameterString.urlEncodedStringWithEncoding(dataEncoding) let encodedURL = url.absoluteString!.urlEncodedStringWithEncoding(dataEncoding) let signatureBaseString = "\(method)&\(encodedURL)&\(encodedParameterString)" let signatureBaseStringData = signatureBaseString.dataUsingEncoding(dataEncoding) let signature = DataHMACConverter.hmacData(HMACAlgorithm.SHA1, keyData: signingKeyData!, data: signatureBaseStringData!) return signature } ```
Author
Owner

@dongri commented on GitHub (Jan 13, 2015):

github.com/dongri/OAuthSwift@9654ccce15

<!-- gh-comment-id:69761103 --> @dongri commented on GitHub (Jan 13, 2015): https://github.com/dongri/OAuthSwift/commit/9654ccce1557834364b253bea87430303f0e36d5
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#3
No description provided.