-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature.m
69 lines (55 loc) · 2.23 KB
/
signature.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
//
// signature
// oauth-signature-demo
//
// Created by YourtionGuo on 14/12/2016.
// Copyright © 2016 Yourtion. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCrypto.h>
@implementation SIDSign : NSObject
+ (void)demo {
NSDictionary *dictionary = @{
@"a": @2,
@"b": @"a",
@"timestamp": @1442198292,
@"noncestr" : @"TWSm66JpFIlzdRyk",
@"app_id" : @"5vY4mg0Eog8SWo0mHYSWbqpl",
};
NSString *sign = [SIDSign getSignatureWithParmas:dictionary andSecret:@"Ppuj8xfvb8jltBkcDvALFcEtWvgXGdxj"];
NSLog(@"Sign: %@", sign);
NSLog(@"SHA1:%@",[SIDSign sha1:@"com.ISNC.FaceChat"]);
}
+ (NSString *)md5:(NSString *)string{
const char *cStr = [string UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, (CC_LONG)strlen(cStr), digest);
NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[result appendFormat:@"%02X", digest[i]];
}
return result;
}
+ (NSString *)getSignatureWithParmas:(NSDictionary *)params andSecret:(NSString *)secret {
NSArray *keys = [params allKeys];
NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
NSString *basestring = [[NSString alloc] init];
for (NSString *key in sortedArray) {
NSString *value = [[NSString stringWithFormat:@"%@", [params objectForKey:key]] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
basestring = [NSString stringWithFormat:@"%@&%@=%@", basestring, key, value];
}
basestring = [basestring substringFromIndex:1];
basestring = [basestring stringByAppendingFormat:@":%@", secret];
NSLog(@"BaseString: %@",basestring);
NSString *sign = [SIDSign md5:basestring];
NSLog(@"MD5: %@", sign);
return sign;
}
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
[SIDSign demo];
}
}