备份CatanBuilding瘦身独立工程
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// TDEncrypt.h
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/27.
|
||||
//
|
||||
|
||||
#import "TDEncryptAlgorithm.h"
|
||||
#import "TDEncryptProtocol.h"
|
||||
#import "TDEncryptManager.h"
|
||||
#import "TDSecretKey.h"
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 99ffee2b45dd4439c8b49b22c8899d70
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// TDEncryptAlgorithm.h
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/27.
|
||||
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol TDEncryptAlgorithm <NSObject>
|
||||
|
||||
|
||||
- (nullable NSString *)encryptData:(NSData *)data;
|
||||
|
||||
|
||||
- (NSString *)algorithm;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: be6654071274442269e91d5fd5a7fb7c
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// TDEncryptManager.h
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "TDConfig.h"
|
||||
|
||||
@class TDEventRecord;
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface TDEncryptManager : NSObject
|
||||
|
||||
|
||||
@property(nonatomic, assign, getter=isValid) BOOL valid;
|
||||
|
||||
|
||||
- (instancetype)initWithConfig:(TDConfig *)config;
|
||||
|
||||
|
||||
- (void)handleEncryptWithConfig:(NSDictionary *)encryptConfig;
|
||||
|
||||
|
||||
- (NSDictionary *)encryptJSONObject:(NSDictionary *)obj;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ecffce0fb694b4e81a9a0e64f8584b98
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,217 @@
|
||||
//
|
||||
// TDEncryptManager.m
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import "TDEncryptManager.h"
|
||||
#import "TDEncryptProtocol.h"
|
||||
#import "TDSecretKey.h"
|
||||
#import "TDRSAEncryptorPlugin.h"
|
||||
#import "NSData+TDGzip.h"
|
||||
#import "TDJSONUtil.h"
|
||||
#import "TDEventRecord.h"
|
||||
#import "TDLogging.h"
|
||||
|
||||
|
||||
@interface TDEncryptManager ()
|
||||
|
||||
@property (nonatomic, strong) TDConfig *config;
|
||||
@property (nonatomic, strong) id<TDEncryptProtocol> encryptor;
|
||||
@property (nonatomic, copy) NSArray<id<TDEncryptProtocol>> *encryptors;
|
||||
@property (nonatomic, copy) NSString *encryptedSymmetricKey;
|
||||
@property (nonatomic, strong) TDSecretKey *secretKey;
|
||||
|
||||
@end
|
||||
|
||||
@implementation TDEncryptManager
|
||||
|
||||
- (instancetype)initWithConfig:(TDConfig *)config
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
[self updateConfig:config];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)updateConfig:(TDConfig *)config {
|
||||
self.config = config;
|
||||
|
||||
|
||||
NSMutableArray *encryptors = [NSMutableArray array];
|
||||
[encryptors addObject:[TDRSAEncryptorPlugin new]];
|
||||
self.encryptors = encryptors;
|
||||
|
||||
|
||||
[self updateEncryptor:[self loadCurrentSecretKey]];
|
||||
}
|
||||
|
||||
- (void)handleEncryptWithConfig:(NSDictionary *)encryptConfig {
|
||||
|
||||
if (!encryptConfig || ![encryptConfig isKindOfClass:[NSDictionary class]]) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (![encryptConfig objectForKey:@"version"]) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSInteger version = [[encryptConfig objectForKey:@"version"] integerValue];
|
||||
TDSecretKey *secretKey = [[TDSecretKey alloc] initWithVersion:version
|
||||
publicKey:encryptConfig[@"key"]
|
||||
asymmetricEncryption:encryptConfig[@"asymmetric"]
|
||||
symmetricEncryption:encryptConfig[@"symmetric"]];
|
||||
|
||||
|
||||
if (![secretKey isValid]) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (![self encryptorWithSecretKey:secretKey]) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
[self updateEncryptor:secretKey];
|
||||
}
|
||||
|
||||
- (void)updateEncryptor:(TDSecretKey *)obj {
|
||||
@try {
|
||||
|
||||
TDSecretKey *secretKey = obj;
|
||||
if (!secretKey.publicKey.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ([self needUpdateSecretKey:self.secretKey newSecretKey:secretKey]) {
|
||||
return;
|
||||
}
|
||||
|
||||
id<TDEncryptProtocol> encryptor = [self filterEncrptor:secretKey];
|
||||
if (!encryptor) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *encryptedSymmetricKey = [encryptor encryptSymmetricKeyWithPublicKey:secretKey.publicKey];
|
||||
|
||||
if (encryptedSymmetricKey.length) {
|
||||
|
||||
self.secretKey = secretKey;
|
||||
|
||||
self.encryptor = encryptor;
|
||||
|
||||
self.encryptedSymmetricKey = encryptedSymmetricKey;
|
||||
|
||||
TDLogDebug(@"\n****************secretKey****************\n public key: %@ \n encrypted symmetric key: %@\n****************secretKey****************", secretKey.publicKey, encryptedSymmetricKey);
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
TDLogError(@"%@ error: %@", self, exception);
|
||||
}
|
||||
}
|
||||
|
||||
- (TDSecretKey *)loadCurrentSecretKey {
|
||||
TDSecretKey *secretKey = self.config.secretKey;
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
- (BOOL)needUpdateSecretKey:(TDSecretKey *)oldSecretKey newSecretKey:(TDSecretKey *)newSecretKey {
|
||||
if (oldSecretKey.version != newSecretKey.version) {
|
||||
return NO;
|
||||
}
|
||||
if (![oldSecretKey.publicKey isEqualToString:newSecretKey.publicKey]) {
|
||||
return NO;
|
||||
}
|
||||
if (![oldSecretKey.symmetricEncryption isEqualToString:newSecretKey.symmetricEncryption]) {
|
||||
return NO;
|
||||
}
|
||||
if (![oldSecretKey.asymmetricEncryption isEqualToString:newSecretKey.asymmetricEncryption]) {
|
||||
return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (id<TDEncryptProtocol>)filterEncrptor:(TDSecretKey *)secretKey {
|
||||
id<TDEncryptProtocol> encryptor = [self encryptorWithSecretKey:secretKey];
|
||||
if (!encryptor) {
|
||||
NSString *format = @"\n You have used the [%@] key, but the corresponding encryption plugin has not been registered. \n";
|
||||
NSString *type = [NSString stringWithFormat:@"%@+%@", secretKey.asymmetricEncryption, secretKey.symmetricEncryption];
|
||||
NSString *message = [NSString stringWithFormat:format, type];
|
||||
NSAssert(NO, message);
|
||||
return nil;
|
||||
}
|
||||
return encryptor;
|
||||
}
|
||||
|
||||
- (id<TDEncryptProtocol>)encryptorWithSecretKey:(TDSecretKey *)secretKey {
|
||||
if (!secretKey) {
|
||||
return nil;
|
||||
}
|
||||
__block id<TDEncryptProtocol> encryptor;
|
||||
[self.encryptors enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id<TDEncryptProtocol> obj, NSUInteger idx, BOOL *stop) {
|
||||
BOOL isSameAsymmetricType = [[obj asymmetricEncryptType] isEqualToString:secretKey.asymmetricEncryption];
|
||||
BOOL isSameSymmetricType = [[obj symmetricEncryptType] isEqualToString:secretKey.symmetricEncryption];
|
||||
if (isSameAsymmetricType && isSameSymmetricType) {
|
||||
encryptor = obj;
|
||||
*stop = YES;
|
||||
}
|
||||
}];
|
||||
return encryptor;
|
||||
}
|
||||
|
||||
- (NSDictionary *)encryptJSONObject:(NSDictionary *)obj {
|
||||
@try {
|
||||
if (!obj) {
|
||||
TDLogDebug(@"Enable encryption but the input obj is invalid!");
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (!self.encryptor) {
|
||||
TDLogDebug(@"Enable encryption but the secret key is invalid!");
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (![self encryptSymmetricKey]) {
|
||||
TDLogDebug(@"Enable encryption but encrypt symmetric key is failed!");
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
NSData *jsonData = [TDJSONUtil JSONSerializeForObject:obj];
|
||||
|
||||
|
||||
NSString *encryptedString = [self.encryptor encryptEvent:jsonData];
|
||||
if (!encryptedString) {
|
||||
TDLogDebug(@"Enable encryption but encrypted input obj is invalid!");
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
NSMutableDictionary *secretObj = [NSMutableDictionary dictionary];
|
||||
secretObj[@"pkv"] = @(self.secretKey.version);
|
||||
secretObj[@"ekey"] = self.encryptedSymmetricKey;
|
||||
secretObj[@"payload"] = encryptedString;
|
||||
return [NSDictionary dictionaryWithDictionary:secretObj];
|
||||
} @catch (NSException *exception) {
|
||||
TDLogDebug(@"%@ error: %@", self, exception);
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (BOOL)encryptSymmetricKey {
|
||||
if (self.encryptedSymmetricKey) {
|
||||
return YES;
|
||||
}
|
||||
NSString *publicKey = self.secretKey.publicKey;
|
||||
self.encryptedSymmetricKey = [self.encryptor encryptSymmetricKeyWithPublicKey:publicKey];
|
||||
return self.encryptedSymmetricKey != nil;
|
||||
}
|
||||
|
||||
- (BOOL)isValid {
|
||||
return _encryptor ? YES:NO;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1ad3a2338631944cd87df838012e7751
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// TDEncryptProtocol.h
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/27.
|
||||
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol TDEncryptProtocol <NSObject>
|
||||
|
||||
|
||||
- (NSString *)symmetricEncryptType;
|
||||
|
||||
|
||||
- (NSString *)asymmetricEncryptType;
|
||||
|
||||
|
||||
- (NSString *)encryptEvent:(NSData *)event;
|
||||
|
||||
|
||||
- (NSString *)encryptSymmetricKeyWithPublicKey:(NSString *)publicKey;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0bb0d612c5fc140eeb484ac00d791b63
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// TDSecretKey.h
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface TDSecretKey : NSObject<NSCopying>
|
||||
|
||||
/// Initialize key information
|
||||
- (instancetype)initWithVersion:(NSUInteger)version publicKey:(NSString *)publicKey;
|
||||
|
||||
/// Initialize key information
|
||||
/// @param version key version number
|
||||
/// @param publicKey public key
|
||||
/// @param asymmetricEncryption asymmetric encryption type
|
||||
/// @param symmetricEncryption Symmetric encryption type
|
||||
- (instancetype)initWithVersion:(NSUInteger)version
|
||||
publicKey:(NSString *)publicKey
|
||||
asymmetricEncryption:(NSString *)asymmetricEncryption
|
||||
symmetricEncryption:(NSString *)symmetricEncryption;
|
||||
|
||||
@property (nonatomic, assign, readonly) NSUInteger version;
|
||||
@property (nonatomic, copy, readonly) NSString *publicKey;
|
||||
@property (nonatomic, copy, readonly) NSString *symmetricEncryption;
|
||||
@property (nonatomic, copy, readonly) NSString *asymmetricEncryption;
|
||||
|
||||
/// Whether the key information is available
|
||||
@property (nonatomic, assign, readonly) BOOL isValid;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ec629b9835a264c5d91241e2774be8d8
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// TDSecretKey.m
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import "TDSecretKey.h"
|
||||
|
||||
@interface TDSecretKey ()
|
||||
|
||||
@property (nonatomic, assign) NSUInteger version;
|
||||
@property (nonatomic, copy) NSString *publicKey;
|
||||
@property (nonatomic, copy) NSString *symmetricEncryption;
|
||||
@property (nonatomic, copy) NSString *asymmetricEncryption;
|
||||
|
||||
@end
|
||||
|
||||
@implementation TDSecretKey
|
||||
|
||||
|
||||
- (instancetype)initWithVersion:(NSUInteger)version
|
||||
publicKey:(NSString *)publicKey {
|
||||
|
||||
return [[TDSecretKey alloc] initWithVersion:version
|
||||
publicKey:publicKey
|
||||
asymmetricEncryption:@"RSA"
|
||||
symmetricEncryption:@"AES"];
|
||||
}
|
||||
|
||||
- (instancetype)initWithVersion:(NSUInteger)version
|
||||
publicKey:(NSString *)publicKey
|
||||
asymmetricEncryption:(NSString *)asymmetricEncryption
|
||||
symmetricEncryption:(NSString *)symmetricEncryption {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.version = version;
|
||||
self.publicKey = publicKey;
|
||||
self.asymmetricEncryption = asymmetricEncryption;
|
||||
self.symmetricEncryption = symmetricEncryption;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)encodeWithCoder:(NSCoder *)coder {
|
||||
[coder encodeInteger:self.version forKey:@"version"];
|
||||
[coder encodeObject:self.publicKey forKey:@"publicKey"];
|
||||
[coder encodeObject:self.symmetricEncryption forKey:@"symmetricEncrypt"];
|
||||
[coder encodeObject:self.asymmetricEncryption forKey:@"asymmetricEncrypt"];
|
||||
}
|
||||
|
||||
- (instancetype)initWithCoder:(NSCoder *)coder {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.version = [coder decodeIntegerForKey:@"version"];
|
||||
self.publicKey = [coder decodeObjectForKey:@"publicKey"];
|
||||
self.symmetricEncryption = [coder decodeObjectForKey:@"symmetricEncrypt"];
|
||||
self.asymmetricEncryption = [coder decodeObjectForKey:@"asymmetricEncrypt"];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)isValid {
|
||||
if (self.publicKey.length && self.symmetricEncryption.length && self.asymmetricEncryption.length) {
|
||||
return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (id)copyWithZone:(NSZone *)zone {
|
||||
TDSecretKey *secretKey = [[[self class] allocWithZone:zone] init];
|
||||
secretKey.version = self.version;
|
||||
secretKey.publicKey = [self.publicKey copy];
|
||||
secretKey.symmetricEncryption = [self.symmetricEncryption copy];
|
||||
secretKey.asymmetricEncryption = [self.asymmetricEncryption copy];
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 03c8b384def5542ae8bd608d4b3968ed
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a51f5c933df844bfaa6e322b24b3ed9e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// TDAESEncryptor.h
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TDEncryptAlgorithm.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface TDAESEncryptor : NSObject <TDEncryptAlgorithm>
|
||||
|
||||
@property (nonatomic, copy, readonly) NSData *key;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9d1974cff31e245d4a80d7890c19ca79
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// TDAESEncryptor.m
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import "TDAESEncryptor.h"
|
||||
#import <CommonCrypto/CommonCryptor.h>
|
||||
#import "TDLogging.h"
|
||||
|
||||
@interface TDAESEncryptor ()
|
||||
|
||||
@property (nonatomic, copy, readwrite) NSData *key;
|
||||
|
||||
@end
|
||||
|
||||
@implementation TDAESEncryptor
|
||||
|
||||
|
||||
- (NSData *)key {
|
||||
if (!_key) {
|
||||
NSUInteger length = 16;
|
||||
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
NSMutableString *randomString = [NSMutableString stringWithCapacity:length];
|
||||
for (NSUInteger i = 0; i < length; i++) {
|
||||
[randomString appendFormat: @"%C", [letters characterAtIndex:arc4random_uniform((uint32_t)[letters length])]];
|
||||
}
|
||||
_key = [randomString dataUsingEncoding:NSUTF8StringEncoding];
|
||||
}
|
||||
return _key;
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)algorithm {
|
||||
return @"AES";
|
||||
}
|
||||
|
||||
|
||||
- (nullable NSString *)encryptData:(NSData *)obj {
|
||||
if (!obj) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (!self.key) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSData *data = obj;
|
||||
NSUInteger dataLength = [data length];
|
||||
size_t bufferSize = dataLength + kCCBlockSizeAES128;
|
||||
void *buffer = malloc(bufferSize);
|
||||
|
||||
size_t numBytesEncrypted = 0;
|
||||
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
|
||||
kCCAlgorithmAES128,
|
||||
kCCOptionPKCS7Padding | kCCOptionECBMode,
|
||||
[self.key bytes],
|
||||
kCCBlockSizeAES128,
|
||||
nil,
|
||||
[data bytes],
|
||||
dataLength,
|
||||
buffer,
|
||||
bufferSize,
|
||||
&numBytesEncrypted);
|
||||
if (cryptStatus == kCCSuccess) {
|
||||
|
||||
NSData *encryptData = [NSData dataWithBytes:buffer length:numBytesEncrypted];
|
||||
NSMutableData *ivEncryptData = [NSMutableData data];
|
||||
[ivEncryptData appendData:encryptData];
|
||||
|
||||
free(buffer);
|
||||
|
||||
NSData *base64EncodeData = [ivEncryptData base64EncodedDataWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
|
||||
NSString *encryptString = [[NSString alloc] initWithData:base64EncodeData encoding:NSUTF8StringEncoding];
|
||||
return encryptString;
|
||||
} else {
|
||||
free(buffer);
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3356f39043a754358aac9d671c6355d0
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// TDRSAEncryptor.h
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TDEncryptAlgorithm.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface TDRSAEncryptor : NSObject <TDEncryptAlgorithm>
|
||||
|
||||
@property (nonatomic, copy) NSString *key;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 31a27833643c74455bef05a2933ee2eb
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,180 @@
|
||||
//
|
||||
// TDRSAEncryptor.m
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import "TDRSAEncryptor.h"
|
||||
|
||||
@implementation TDRSAEncryptor
|
||||
|
||||
- (void)setKey:(NSString *)key {
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
NSString *publicKeyCopy = [key copy];
|
||||
publicKeyCopy = [publicKeyCopy stringByReplacingOccurrencesOfString:@"\r" withString:@""];
|
||||
publicKeyCopy = [publicKeyCopy stringByReplacingOccurrencesOfString:@"\n" withString:@""];
|
||||
publicKeyCopy = [publicKeyCopy stringByReplacingOccurrencesOfString:@"\t" withString:@""];
|
||||
publicKeyCopy = [publicKeyCopy stringByReplacingOccurrencesOfString:@" " withString:@""];
|
||||
_key = publicKeyCopy;
|
||||
}
|
||||
|
||||
- (NSString *)algorithm {
|
||||
return @"RSA";
|
||||
}
|
||||
|
||||
- (NSString *)encryptData:(NSData *)data {
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSString *asymmetricPublicKey = self.key;
|
||||
if (!asymmetricPublicKey) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
SecKeyRef keyRef = [self addPublicKey:asymmetricPublicKey];
|
||||
if (!keyRef) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
const uint8_t *srcbuf = (const uint8_t *)[data bytes];
|
||||
size_t srclen = (size_t)data.length;
|
||||
|
||||
size_t block_size = SecKeyGetBlockSize(keyRef) * sizeof(uint8_t);
|
||||
void *outbuf = malloc(block_size);
|
||||
size_t src_block_size = block_size - 11;
|
||||
|
||||
NSMutableData *ret = [[NSMutableData alloc] init];
|
||||
for(int idx=0; idx<srclen; idx+=src_block_size) {
|
||||
size_t data_len = srclen - idx;
|
||||
if (data_len > src_block_size) {
|
||||
data_len = src_block_size;
|
||||
}
|
||||
|
||||
size_t outlen = block_size;
|
||||
OSStatus status = noErr;
|
||||
|
||||
status = SecKeyEncrypt(keyRef,
|
||||
kSecPaddingPKCS1,
|
||||
srcbuf + idx,
|
||||
data_len,
|
||||
outbuf,
|
||||
&outlen
|
||||
);
|
||||
if (status != 0) {
|
||||
ret = nil;
|
||||
break;
|
||||
}else{
|
||||
[ret appendBytes:outbuf length:outlen];
|
||||
}
|
||||
}
|
||||
free(outbuf);
|
||||
CFRelease(keyRef);
|
||||
|
||||
return [ret base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
|
||||
}
|
||||
|
||||
#pragma mark – Private Methods
|
||||
- (SecKeyRef)addPublicKey:(NSString *)aymmetricPublicKey {
|
||||
NSString *key = [aymmetricPublicKey copy];
|
||||
|
||||
NSData *data = [[NSData alloc] initWithBase64EncodedString:key options:NSDataBase64DecodingIgnoreUnknownCharacters];
|
||||
data = [self stripPublicKeyHeader:data];
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
//a tag to read/write keychain storage
|
||||
NSString *tag = @"RSAUtil_PubKey";
|
||||
NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];
|
||||
|
||||
// Delete any old lingering key with the same tag
|
||||
NSMutableDictionary *publicKey = [[NSMutableDictionary alloc] init];
|
||||
[publicKey setObject:(__bridge id) kSecClassKey forKey:(__bridge id)kSecClass];
|
||||
[publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
|
||||
[publicKey setObject:d_tag forKey:(__bridge id)kSecAttrApplicationTag];
|
||||
SecItemDelete((__bridge CFDictionaryRef)publicKey);
|
||||
|
||||
// Add persistent version of the key to system keychain
|
||||
[publicKey setObject:data forKey:(__bridge id)kSecValueData];
|
||||
[publicKey setObject:(__bridge id) kSecAttrKeyClassPublic forKey:(__bridge id)
|
||||
kSecAttrKeyClass];
|
||||
[publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)
|
||||
kSecReturnPersistentRef];
|
||||
|
||||
CFTypeRef persistKey = nil;
|
||||
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)publicKey, &persistKey);
|
||||
if (persistKey != nil) {
|
||||
CFRelease(persistKey);
|
||||
}
|
||||
if ((status != noErr) && (status != errSecDuplicateItem)) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
[publicKey removeObjectForKey:(__bridge id)kSecValueData];
|
||||
[publicKey removeObjectForKey:(__bridge id)kSecReturnPersistentRef];
|
||||
[publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];
|
||||
[publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
|
||||
|
||||
// Now fetch the SecKeyRef version of the key
|
||||
SecKeyRef keyRef = nil;
|
||||
status = SecItemCopyMatching((__bridge CFDictionaryRef)publicKey, (CFTypeRef *)&keyRef);
|
||||
if (status != noErr) {
|
||||
return nil;
|
||||
}
|
||||
return keyRef;
|
||||
}
|
||||
|
||||
- (NSData *)stripPublicKeyHeader:(NSData *)d_key {
|
||||
// Skip ASN.1 public key header
|
||||
if (d_key == nil) {
|
||||
return(nil);
|
||||
}
|
||||
|
||||
unsigned long len = [d_key length];
|
||||
if (!len) {
|
||||
return(nil);
|
||||
}
|
||||
|
||||
unsigned char *c_key = (unsigned char *)[d_key bytes];
|
||||
unsigned int idx = 0;
|
||||
|
||||
if (c_key[idx++] != 0x30) {
|
||||
return(nil);
|
||||
}
|
||||
|
||||
if (c_key[idx] > 0x80) {
|
||||
idx += c_key[idx] - 0x80 + 1;
|
||||
} else {
|
||||
idx++;
|
||||
}
|
||||
|
||||
// PKCS #1 rsaEncryption szOID_RSA_RSA
|
||||
static unsigned char seqiod[] =
|
||||
{ 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
|
||||
0x01, 0x05, 0x00 };
|
||||
if (memcmp(&c_key[idx], seqiod, 15)) {
|
||||
return(nil);
|
||||
}
|
||||
idx += 15;
|
||||
|
||||
if (c_key[idx++] != 0x03) {
|
||||
return(nil);
|
||||
}
|
||||
|
||||
if (c_key[idx] > 0x80) {
|
||||
idx += c_key[idx] - 0x80 + 1;
|
||||
} else {
|
||||
idx++;
|
||||
}
|
||||
if (c_key[idx++] != '\0') {
|
||||
return(nil);
|
||||
}
|
||||
// Now make a new NSData from this buffer
|
||||
return([NSData dataWithBytes:&c_key[idx] length:len - idx]);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9bee54e226cda4595abb93c0ebbe11df
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// TDRSAEncryptorPlugin.h
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TDEncryptProtocol.h"
|
||||
#import "TDRSAEncryptor.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface TDRSAEncryptorPlugin : NSObject <TDEncryptProtocol>
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a6d6b82caa9fc42fa81c28fee8c4ad79
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// TDRSAEncryptorPlugin.m
|
||||
// ThinkingSDK
|
||||
//
|
||||
// Created by wwango on 2022/1/21.
|
||||
//
|
||||
|
||||
#import "TDRSAEncryptorPlugin.h"
|
||||
#import "TDAESEncryptor.h"
|
||||
#import "TDRSAEncryptor.h"
|
||||
|
||||
@interface TDRSAEncryptorPlugin ()
|
||||
|
||||
@property (nonatomic, strong) TDAESEncryptor *aesEncryptor;
|
||||
@property (nonatomic, strong) TDRSAEncryptor *rsaEncryptor;
|
||||
|
||||
@end
|
||||
|
||||
@implementation TDRSAEncryptorPlugin
|
||||
|
||||
- (instancetype)init {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_aesEncryptor = [[TDAESEncryptor alloc] init];
|
||||
_rsaEncryptor = [[TDRSAEncryptor alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)symmetricEncryptType {
|
||||
return [_aesEncryptor algorithm];
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)asymmetricEncryptType {
|
||||
return [_rsaEncryptor algorithm];
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)encryptEvent:(NSData *)event {
|
||||
return [_aesEncryptor encryptData:event];
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)encryptSymmetricKeyWithPublicKey:(NSString *)publicKey {
|
||||
if (![_rsaEncryptor.key isEqualToString:publicKey]) {
|
||||
_rsaEncryptor.key = publicKey;
|
||||
}
|
||||
return [_rsaEncryptor encryptData:_aesEncryptor.key];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,33 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d4d26e5bdde0c4282bf6539cf6df7001
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings:
|
||||
AddToEmbeddedBinaries: false
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user