-
Notifications
You must be signed in to change notification settings - Fork 3
/
MPOAuthSignatureParameter.m
81 lines (66 loc) · 3.02 KB
/
MPOAuthSignatureParameter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// MPOAuthSignatureParameter.m
// MPOAuthConnection
//
// Created by Karl Adam on 08.12.07.
// Copyright 2008 matrixPointer. All rights reserved.
//
#import "MPOAuthSignatureParameter.h"
#import "MPOAuthURLRequest.h"
#import "NSString+URLEscapingAdditions.h"
#import "NSURL+MPURLParameterAdditions.h"
#import <CommonCrypto/CommonHMAC.h>
#include "Base64Transcoder.h"
@interface MPOAuthSignatureParameter ()
- (id)initUsingHMAC_SHA1WithText:(NSString *)inText andSecret:(NSString *)inSecret forRequest:(MPOAuthURLRequest *)inRequest;
@end
@implementation MPOAuthSignatureParameter
+ (NSString *)signatureBaseStringUsingParameterString:(NSString *)inParameterString forRequest:(MPOAuthURLRequest *)inRequest {
return [NSString stringWithFormat:@"%@&%@&%@", [inRequest HTTPMethod],
[[inRequest.url absoluteNormalizedString] stringByAddingURIPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[inParameterString stringByAddingURIPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
+ (NSString *)HMAC_SHA1SignatureForText:(NSString *)inText usingSecret:(NSString *)inSecret {
NSData *secretData = [inSecret dataUsingEncoding:NSUTF8StringEncoding];
NSData *textData = [inText dataUsingEncoding:NSUTF8StringEncoding];
unsigned char result[CC_SHA1_DIGEST_LENGTH];
CCHmacContext hmacContext;
bzero(&hmacContext, sizeof(CCHmacContext));
CCHmacInit(&hmacContext, kCCHmacAlgSHA1, secretData.bytes, secretData.length);
CCHmacUpdate(&hmacContext, textData.bytes, textData.length);
CCHmacFinal(&hmacContext, result);
//Base64 Encoding
char base64Result[32];
size_t theResultLength = 32;
Base64EncodeData(result, 20, base64Result, &theResultLength);
NSData *theData = [NSData dataWithBytes:base64Result length:theResultLength];
NSString *base64EncodedResult = [[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding] autorelease];
return base64EncodedResult;
}
- (id)initWithText:(NSString *)inText andSecret:(NSString *)inSecret forRequest:(MPOAuthURLRequest *)inRequest usingMethod:(NSString *)inMethod {
if ([inMethod isEqual:kMPOAuthSignatureMethodHMACSHA1]) {
self = [self initUsingHMAC_SHA1WithText:inText andSecret:inSecret forRequest:inRequest];
} else if ([inMethod isEqualToString:kMPOAuthSignatureMethodPlaintext]) {
if (self = [super init]) {
self.name = @"oauth_signature";
self.value = inSecret;
}
} else {
[self release];
self = nil;
[NSException raise:@"Unsupported Signature Method" format:@"The signature method \"%@\" is not currently support by MPOAuthConnection", inMethod];
}
return self;
}
- (id)initUsingHMAC_SHA1WithText:(NSString *)inText andSecret:(NSString *)inSecret forRequest:(MPOAuthURLRequest *)inRequest {
if (self = [super init]) {
NSString *signatureBaseString = [MPOAuthSignatureParameter signatureBaseStringUsingParameterString:inText forRequest:inRequest];
self.name = @"oauth_signature";
self.value = [MPOAuthSignatureParameter HMAC_SHA1SignatureForText:signatureBaseString usingSecret:inSecret];
}
return self;
}
- (oneway void)dealloc {
[super dealloc];
}
@end