|
|
@@ -1,99 +0,0 @@
|
|
|
-#import "GenerateTestUserSig.h"
|
|
|
-#import <CommonCrypto/CommonCrypto.h>
|
|
|
-#import <zlib.h>
|
|
|
-
|
|
|
-@implementation GenerateTestUserSig
|
|
|
-
|
|
|
-+ (unsigned int)currentSDKAppid {
|
|
|
- return public_SDKAPPID;
|
|
|
-}
|
|
|
-
|
|
|
-+ (NSString *)currentSecretkey {
|
|
|
- return public_SECRETKEY;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-+ (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 = [SECRETKEY cStringUsingEncoding:NSASCIIStringEncoding];
|
|
|
- const char *cData = [plainText cStringUsingEncoding:NSASCIIStringEncoding];
|
|
|
-
|
|
|
- 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
|