-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGenerateTestUserSig.m
More file actions
executable file
·91 lines (81 loc) · 3.15 KB
/
GenerateTestUserSig.m
File metadata and controls
executable file
·91 lines (81 loc) · 3.15 KB
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
82
83
84
85
86
87
88
89
90
91
// Copyright © 2021 Tencent. All rights reserved.
#import "GenerateTestUserSig.h"
#import <CommonCrypto/CommonCrypto.h>
#import <zlib.h>
@implementation GenerateTestUserSig
+ (NSString *)genTestUserSig:(NSString *)identifier
{
CFTimeInterval current = CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970;
long tlsTime = floor(current);
NSMutableDictionary *obj = [@{@"TLS.ver": @"2.0",
@"TLS.identifier": identifier,
@"TLS.sdkappid": @(SDKAppID),
@"TLS.expire": @(EXPIRETIME),
@"TLS.time": @(tlsTime)} mutableCopy];
NSMutableString *stringToSign = [[NSMutableString alloc] init];
NSArray *keyOrder = @[@"TLS.identifier",
@"TLS.sdkappid",
@"TLS.time",
@"TLS.expire"];
for (NSString *key in keyOrder) {
[stringToSign appendFormat:@"%@:%@\n", key, obj[key]];
}
NSLog(@"%@", stringToSign);
//NSString *sig = [self sigString:stringToSign];
NSString *sig = [self hmac:stringToSign];
obj[@"TLS.sig"] = sig;
NSLog(@"sig: %@", sig);
NSError *error = nil;
NSData *jsonToZipData = [NSJSONSerialization dataWithJSONObject:obj options:0 error:&error];
if (error) {
NSLog(@"[Error] json serialization failed: %@", error);
return @"";
}
const Bytef* zipsrc = (const Bytef*)[jsonToZipData bytes];
uLongf srcLen = jsonToZipData.length;
uLong upperBound = compressBound(srcLen);
Bytef *dest = (Bytef*)malloc(upperBound);
uLongf destLen = upperBound;
int ret = compress2(dest, &destLen, (const Bytef*)zipsrc, srcLen, Z_BEST_SPEED);
if (ret != Z_OK) {
NSLog(@"[Error] Compress Error %d, upper bound: %lu", ret, upperBound);
free(dest);
return @"";
}
NSString *result = [self base64URL: [NSData dataWithBytesNoCopy:dest length:destLen]];
return result;
}
+ (NSString *)hmac:(NSString *)plainText
{
const char *cKey = [SDKSECRETKEY cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [plainText cStringUsingEncoding:NSUTF8StringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *hmacData = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
return [hmacData base64EncodedStringWithOptions:0];
}
+ (NSString *)base64URL:(NSData *)data
{
NSString *result = [data base64EncodedStringWithOptions:0];
NSMutableString *final = [[NSMutableString alloc] init];
const char *cString = [result cStringUsingEncoding:NSUTF8StringEncoding];
for (int i = 0; i < result.length; ++ i) {
char x = cString[i];
switch(x){
case '+':
[final appendString:@"*"];
break;
case '/':
[final appendString:@"-"];
break;
case '=':
[final appendString:@"_"];
break;
default:
[final appendFormat:@"%c", x];
break;
}
}
return final;
}
@end