- fix Encryption

This commit is contained in:
Giuseppe Nucifora 2016-03-16 02:05:24 +01:00
parent 6a5119dbe3
commit 9ff44f2497
4 changed files with 21 additions and 26 deletions

View File

@ -8,7 +8,7 @@
Pod::Spec.new do |s|
s.name = "PNObject"
s.version = "0.4.0"
s.version = "0.4.5"
s.summary = "PNObject is a simple replica of the more complex ParseObject"
# This description is used to generate tags and improve search results.

View File

@ -14,6 +14,9 @@
#import "AFHTTPRequestSerializer+OAuth2.h"
#import "AFOAuthCredential.h"
extern NSString* _Nonnull const PNObjectEncryptionKey;
extern NSString* _Nonnull const PNObjectEncryptionNonce;
#pragma mark LocalNotification Keys
extern NSString* _Nonnull const PNObjectLocalNotificationRefreshTokenClientCredentialSuccess;
@ -209,11 +212,5 @@ extern NSString* _Nonnull const Client_Secret;
* <#Description#>
*/
@property (nonatomic) NSInteger minPasswordLenght;
/**
* <#Description#>
*/
@property (nonatomic, strong, nonnull, readonly) NSString *encrypKey;
@property (nonatomic, strong, nonnull, readonly) NSString *nonce;
@end

View File

@ -13,8 +13,8 @@
#import "NSString+Helper.h"
#import "PNObject+Protected.h"
#import "NSUserDefaults+AESEncryptor.h"
NSString * const PNObjectNSUserDefaultsAESKey = @"feiGuP5iYZB8cSwHnmCtAWomLcarVoxDe3L8jVSxv6f6dOUtSF";
#import "NASecRandom.h"
#import "NAKeychain.h"
NSString * const PNObjectLocalNotificationRefreshTokenClientCredentialSuccess = @"PNObjectLocalNotificationRefreshTokenClientCredentialSuccess";
NSString * const PNObjectLocalNotificationRefreshTokenClientCredentialFail = @"PNObjectLocalNotificationRefreshTokenClientCredentialFail";
@ -165,20 +165,18 @@ static bool isFirstAccess = YES;
_headerFields = [[NSMutableDictionary alloc] init];
[[NSUserDefaults standardUserDefaults] setAESKey:PNObjectNSUserDefaultsAESKey];
NSLog(@"%@",[NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionKey]);
if (![NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionKey]) {
NSError *error;
NSData *key = [NASecRandom randomData:32 error:&error];
[NAKeychain addSymmetricKey:key applicationLabel:PNObjectEncryptionKey tag:nil label:nil];
NSData *nonce = [NASecRandom randomData:16 error:&error];
[NAKeychain addSymmetricKey:nonce applicationLabel:PNObjectEncryptionNonce tag:nil label:nil];
if ([[NSUserDefaults standardUserDefaults] objectForKey:PNObjectEncryptionKey]) {
_encrypKey = [[NSUserDefaults standardUserDefaults] decryptedValueForKey:PNObjectEncryptionKey];
_nonce = [[NSUserDefaults standardUserDefaults] decryptedValueForKey:PNObjectEncryptionNonce];
}
else {
_encrypKey = [[NSProcessInfo processInfo] globallyUniqueString];
_nonce = [[NSProcessInfo processInfo] globallyUniqueString];
[[NSUserDefaults standardUserDefaults] encryptValue:_encrypKey withKey:PNObjectEncryptionKey];
[[NSUserDefaults standardUserDefaults] encryptValue:_nonce withKey:PNObjectEncryptionNonce];
}
}
return self;
}

View File

@ -148,7 +148,7 @@ static bool isFirstAccess = YES;
NSError *error = nil;
NSData *data = [_AES decrypt:[_fileManager fetchFileDataWithPath:className] nonce:[[[PNObjectConfig sharedInstance] nonce] dataUsingEncoding:NSUTF8StringEncoding] key:[[[PNObjectConfig sharedInstance] encrypKey] dataUsingEncoding:NSUTF8StringEncoding] error:&error]; // password:[[PNObjectConfig sharedInstance] encrypKey] error:&error];
NSData *data = [_AES decrypt:[_fileManager fetchFileDataWithPath:className] nonce:[NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionNonce] key:[NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionKey] error:&error]; // password:[[PNObjectConfig sharedInstance] encrypKey] error:&error];
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
@ -173,7 +173,7 @@ static bool isFirstAccess = YES;
NSDictionary *objectDict = [(PNObject*) object reverseMapping];
NSData *objectData = [_AES encrypt:[NSKeyedArchiver archivedDataWithRootObject:objectDict] nonce:[[[PNObjectConfig sharedInstance] nonce] dataUsingEncoding:NSUTF8StringEncoding] key:[[[PNObjectConfig sharedInstance] encrypKey] dataUsingEncoding:NSUTF8StringEncoding] error:&error];//[RNCryptor encryptData:[NSKeyedArchiver archivedDataWithRootObject:objectDict] password:[[PNObjectConfig sharedInstance] encrypKey]];
NSData *objectData = [_AES encrypt:[NSKeyedArchiver archivedDataWithRootObject:objectDict] nonce:[NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionNonce] key:[NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionKey] error:&error];//[RNCryptor encryptData:[NSKeyedArchiver archivedDataWithRootObject:objectDict] password:[[PNObjectConfig sharedInstance] encrypKey]];
if ([self issetPNObjectModelForObject:object]) {
if ([_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
@ -199,7 +199,7 @@ static bool isFirstAccess = YES;
NSData *data = [_AES decrypt:[_fileManager fetchFileDataWithPath:[self objectName:object]] nonce:[[[PNObjectConfig sharedInstance] nonce] dataUsingEncoding:NSUTF8StringEncoding] key:[[[PNObjectConfig sharedInstance] encrypKey] dataUsingEncoding:NSUTF8StringEncoding] error:&error];
NSData *data = [_AES decrypt:[_fileManager fetchFileDataWithPath:[self objectName:object]] nonce:[NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionNonce] key:[NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionKey] error:&error];
//[RNCryptor decryptData:[_fileManager fetchFileDataWithPath:[self objectName:object]] password:[[PNObjectConfig sharedInstance] encrypKey] error:&error];
NSMutableArray *objects = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
@ -208,7 +208,7 @@ static bool isFirstAccess = YES;
[objects addObject:objectDict];
NSData *objectData = [_AES encrypt:[NSKeyedArchiver archivedDataWithRootObject:objects] nonce:[[[PNObjectConfig sharedInstance] nonce] dataUsingEncoding:NSUTF8StringEncoding] key:[[[PNObjectConfig sharedInstance] encrypKey] dataUsingEncoding:NSUTF8StringEncoding] error:&error];
NSData *objectData = [_AES encrypt:[NSKeyedArchiver archivedDataWithRootObject:objects] nonce:[NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionNonce] key:[NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionKey] error:&error];
//[RNCryptor encryptData:[NSKeyedArchiver archivedDataWithRootObject:objects] password:[[PNObjectConfig sharedInstance] encrypKey]];
if ([_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
@ -227,7 +227,7 @@ static bool isFirstAccess = YES;
[objects addObject:objectDict];
NSData *objectData = [_AES encrypt:[NSKeyedArchiver archivedDataWithRootObject:objects] nonce:[[[PNObjectConfig sharedInstance] nonce] dataUsingEncoding:NSUTF8StringEncoding] key:[[[PNObjectConfig sharedInstance] encrypKey] dataUsingEncoding:NSUTF8StringEncoding] error:&error];
NSData *objectData = [_AES encrypt:[NSKeyedArchiver archivedDataWithRootObject:objects] nonce:[NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionNonce] key:[NAKeychain symmetricKeyWithApplicationLabel:PNObjectEncryptionKey] error:&error];
//[RNCryptor encryptData:[NSKeyedArchiver archivedDataWithRootObject:objects] password:[[PNObjectConfig sharedInstance] encrypKey]];
if ([_fileManager createFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {