Skip to content

Commit

Permalink
Release 2.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujg-00 committed May 8, 2021
1 parent a12b862 commit 822f740
Show file tree
Hide file tree
Showing 28 changed files with 1,098 additions and 362 deletions.
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "SensorsAnalyticsSDK"
s.version = "2.6.0"
s.version = "2.6.1"
s.summary = "The official iOS SDK of Sensors Analytics."
s.homepage = "http://www.sensorsdata.cn"
s.source = { :git => 'https://github.com/sensorsdata/sa-sdk-ios.git', :tag => "v#{s.version}" }
Expand Down
138 changes: 90 additions & 48 deletions SensorsAnalyticsSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions SensorsAnalyticsSDK/Core/Encrypt/SAAESEncryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <Foundation/Foundation.h>
#import "SAAlgorithmProtocol.h"
NS_ASSUME_NONNULL_BEGIN

#import "SAAbstractEncryptor.h"

NS_ASSUME_NONNULL_BEGIN
@interface SAAESEncryptor : NSObject <SAAlgorithmProtocol>

@interface SAAESEncryptor : SAAbstractEncryptor
@property (nonatomic, copy, readonly) NSData *key;

@end

Expand Down
39 changes: 20 additions & 19 deletions SensorsAnalyticsSDK/Core/Encrypt/SAAESEncryptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,38 @@

@interface SAAESEncryptor ()

@property (nonatomic, copy) NSData *secretKey;
@property (nonatomic, copy, readwrite) NSData *key;

@end

@implementation SAAESEncryptor
#pragma mark - Public Methods

#pragma mark - Life Cycle

- (instancetype)initWithSecretKey:(id)secretKey {
self = [super initWithSecretKey:secretKey];
if (self) {
[self configWithSecretKey:secretKey];
- (NSData *)key {
if (!_key) {
// 默认使用 16 位长度随机字符串,RSA 和 ECC 保持一致
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 self;
return _key;
}

- (void)configWithSecretKey:(id)secretKey {
if (![SAValidator isValidData:secretKey]) {
return;
}
self.secretKey = secretKey;
- (NSString *)algorithm {
return kSAAlgorithmTypeAES;
}

#pragma mark - Public Methods

- (nullable NSString *)encryptObject:(NSData *)obj {
- (nullable NSString *)encryptData:(NSData *)obj {
if (![SAValidator isValidData:obj]) {
SALogError(@"Enable AES encryption but the input obj is invalid!");
return nil;
}
if (![SAValidator isValidData:self.secretKey]) {

if (![SAValidator isValidData:self.key]) {
SALogError(@"Enable AES encryption but the secret key data is invalid!");
return nil;
}
Expand All @@ -78,7 +78,7 @@ - (nullable NSString *)encryptObject:(NSData *)obj {
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
[self.secretKey bytes],
[self.key bytes],
kCCBlockSizeAES128,
[ivData bytes],
[data bytes],
Expand All @@ -87,6 +87,7 @@ - (nullable NSString *)encryptObject:(NSData *)obj {
bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
// 获得加密内容后,在内容前添加 16 位随机字节,增加数据复杂度
NSData *encryptData = [NSData dataWithBytes:buffer length:numBytesEncrypted];
NSMutableData *ivEncryptData = [NSMutableData dataWithData:ivData];
[ivEncryptData appendData:encryptData];
Expand Down
47 changes: 0 additions & 47 deletions SensorsAnalyticsSDK/Core/Encrypt/SAAbstractEncryptor.h

This file was deleted.

37 changes: 37 additions & 0 deletions SensorsAnalyticsSDK/Core/Encrypt/SAAlgorithmProtocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// SAEncryptor.h
// SensorsAnalyticsSDK
//
// Created by 彭远洋 on 2021/4/23.
// Copyright © 2021 Sensors Data Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

extern NSString * const kSAAlgorithmTypeAES;
extern NSString * const kSAAlgorithmTypeRSA;
extern NSString * const kSAAlgorithmTypeECC;
extern NSString * const kSAEncryptECCClassName;

@protocol SAAlgorithmProtocol <NSObject>

- (nullable NSString *)encryptData:(NSData *)data;
- (NSString *)algorithm;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// SAAbstractEncryptor.m
// SAEncryptor.m
// SensorsAnalyticsSDK
//
// Created by wenquan on 2020/12/14.
// Copyright © 2020 Sensors Data Co., Ltd. All rights reserved.
// Created by 彭远洋 on 2021/4/23.
// Copyright © 2021 Sensors Data Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -22,26 +22,9 @@
#error This file must be compiled with ARC. Either turn on ARC for the project or use -fobjc-arc flag on this file.
#endif

#import "SAAbstractEncryptor.h"
#import "SAAlgorithmProtocol.h"

@implementation SAAbstractEncryptor

- (instancetype)initWithSecretKey:(id)secretKey {
self = [super init];
if (self) {
// base implementation
}
return self;
}

- (nullable NSString *)encryptObject:(NSData *)obj {
// base implementation
return nil;
}

- (NSData *)random16ByteData {
// base implementation
return nil;
}

@end
NSString * const kSAAlgorithmTypeAES = @"AES";
NSString * const kSAAlgorithmTypeRSA = @"RSA";
NSString * const kSAAlgorithmTypeECC = @"EC";
NSString * const kSAEncryptECCClassName = @"SACryptoppECC";
10 changes: 5 additions & 5 deletions SensorsAnalyticsSDK/Core/Encrypt/SAECCEncryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
// limitations under the License.
//

#import "SAAbstractEncryptor.h"

extern NSString * const kSAEncryptECCPrefix;
extern NSString * const kSAEncryptECCClassName;
#import <Foundation/Foundation.h>
#import "SAAlgorithmProtocol.h"

NS_ASSUME_NONNULL_BEGIN

@interface SAECCEncryptor : SAAbstractEncryptor
@interface SAECCEncryptor : NSObject <SAAlgorithmProtocol>

@property (nonatomic, copy) NSString *key;

@end

Expand Down
48 changes: 12 additions & 36 deletions SensorsAnalyticsSDK/Core/Encrypt/SAECCEncryptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,34 @@
#import "SALog.h"

NSString * const kSAEncryptECCPrefix = @"EC:";
NSString * const kSAEncryptECCClassName = @"SACryptoppECC";

typedef NSString* (*SAEEncryptImplementation)(Class, SEL, NSString *, NSString *);

@interface SAECCEncryptor ()

/// 移除 EC: 前缀之后真正的公钥
@property (nonatomic, copy) NSString *publicKey;

@end

@implementation SAECCEncryptor

#pragma mark - Life Cycle

- (instancetype)initWithSecretKey:(NSString *)secretKey {
self = [super initWithSecretKey:secretKey];
if (self) {
[self configWithSecretKey:secretKey];
}
return self;
}

- (void)configWithSecretKey:(id)secretKey {
if (![SAValidator isValidString:secretKey]) {
- (void)setKey:(NSString *)key {
if (![SAValidator isValidString:key]) {
SALogError(@"Enable ECC encryption but the secret key is invalid!");
return;
}

if (![secretKey hasPrefix:kSAEncryptECCPrefix]) {
if (![key hasPrefix:kSAEncryptECCPrefix]) {
SALogError(@"Enable ECC encryption but the secret key is not ECC key!");
return;
}

self.publicKey = [secretKey substringFromIndex:[kSAEncryptECCPrefix length]];
_key = [key substringFromIndex:[kSAEncryptECCPrefix length]];
}

#pragma mark - Public Methods

- (nullable NSString *)encryptObject:(NSData *)obj {
- (NSString *)encryptData:(NSData *)obj {
if (![SAValidator isValidData:obj]) {
SALogError(@"Enable ECC encryption but the input obj is invalid!");
return nil;
}

if (![SAValidator isValidString:self.publicKey]) {

// 去除非对称秘钥公钥中的前缀内容,返回实际的非对称秘钥公钥内容
NSString *asymmetricKey = self.key;
if (![SAValidator isValidString:asymmetricKey]) {
SALogError(@"Enable ECC encryption but the public key is invalid!");
return nil;
}
Expand All @@ -82,20 +64,14 @@ - (nullable NSString *)encryptObject:(NSData *)obj {
IMP methodIMP = [class methodForSelector:selector];
if (methodIMP) {
NSString *string = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];
return ((SAEEncryptImplementation)methodIMP)(class, selector, string, self.publicKey);
return ((SAEEncryptImplementation)methodIMP)(class, selector, string, asymmetricKey);
}

return nil;
}

- (NSData *)random16ByteData {
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])]];
}
return [randomString dataUsingEncoding:NSUTF8StringEncoding];
- (NSString *)algorithm {
return kSAAlgorithmTypeECC;
}

@end
31 changes: 31 additions & 0 deletions SensorsAnalyticsSDK/Core/Encrypt/SAECCPluginEncryptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// SAECCPluginEncryptor.h
// SensorsAnalyticsSDK
//
// Created by 彭远洋 on 2021/4/14.
// Copyright © 2021 Sensors Data Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <Foundation/Foundation.h>
#import "SAEncryptProtocol.h"
#import "SAECCEncryptor.h"

NS_ASSUME_NONNULL_BEGIN

@interface SAECCPluginEncryptor : NSObject <SAEncryptProtocol>

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit 822f740

Please sign in to comment.