2012-07-17 20:57:11 +00:00
|
|
|
/**
|
2015-02-28 15:01:41 +00:00
|
|
|
* Copyright Maarten Billemont (http://www.lhunath.com, lhunath@lyndir.com)
|
|
|
|
*
|
|
|
|
* See the enclosed file LICENSE for license information (LGPLv3). If you did
|
|
|
|
* not receive this file, see http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
|
|
*
|
|
|
|
* @author Maarten Billemont <lhunath@lyndir.com>
|
|
|
|
* @license http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
|
|
*/
|
2012-07-17 20:57:11 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// MPKey
|
|
|
|
//
|
|
|
|
// Created by Maarten Billemont on 16/07/12.
|
|
|
|
// Copyright 2012 lhunath (Maarten Billemont). All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "MPAlgorithm.h"
|
|
|
|
|
2013-04-20 18:11:19 +00:00
|
|
|
@interface MPKey()
|
2012-07-17 20:57:11 +00:00
|
|
|
|
2016-01-14 14:58:04 +00:00
|
|
|
@property(nonatomic) MPKeyOrigin origin;
|
2017-04-15 06:28:11 +00:00
|
|
|
@property(nonatomic, copy) NSString *fullName;
|
|
|
|
@property(nonatomic, copy) NSData *( ^keyResolver )(id<MPAlgorithm>);
|
2012-07-17 20:57:11 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
@implementation MPKey {
|
|
|
|
NSCache *_keyCache;
|
|
|
|
};
|
2013-04-20 18:11:19 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
- (instancetype)initForFullName:(NSString *)fullName withMasterPassword:(NSString *)masterPassword {
|
2012-07-17 20:57:11 +00:00
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
return [self initForFullName:fullName withKeyResolver:^NSData *(id<MPAlgorithm> algorithm) {
|
|
|
|
return [algorithm keyDataForFullName:self.fullName withMasterPassword:masterPassword];
|
|
|
|
} keyOrigin:MPKeyOriginMasterPassword];
|
2015-02-28 15:01:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
- (instancetype)initForFullName:(NSString *)fullName withKeyResolver:(NSData *( ^ )(id<MPAlgorithm>))keyResolver
|
|
|
|
keyOrigin:(MPKeyOrigin)origin {
|
2015-02-28 15:01:41 +00:00
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
if (!(self = [super init]))
|
2015-02-28 15:01:41 +00:00
|
|
|
return nil;
|
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
_keyCache = [NSCache new];
|
|
|
|
|
2016-01-14 14:58:04 +00:00
|
|
|
self.origin = origin;
|
2017-04-15 06:28:11 +00:00
|
|
|
self.fullName = fullName;
|
|
|
|
self.keyResolver = keyResolver;
|
2012-07-17 20:57:11 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
- (NSData *)keyIDForAlgorithm:(id<MPAlgorithm>)algorithm {
|
|
|
|
|
|
|
|
return [algorithm keyIDForKeyData:[self keyDataForAlgorithm:algorithm]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData *)keyDataForAlgorithm:(id<MPAlgorithm>)algorithm {
|
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
@synchronized (self) {
|
|
|
|
NSData *keyData = [_keyCache objectForKey:algorithm];
|
|
|
|
if (keyData)
|
|
|
|
return keyData;
|
2016-07-23 15:44:17 +00:00
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
keyData = self.keyResolver( algorithm );
|
|
|
|
if (keyData)
|
|
|
|
[_keyCache setObject:keyData forKey:algorithm];
|
2015-02-28 15:01:41 +00:00
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
return keyData;
|
|
|
|
}
|
2015-02-28 15:01:41 +00:00
|
|
|
}
|
2012-07-17 20:57:11 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
- (NSData *)keyDataForAlgorithm:(id<MPAlgorithm>)algorithm trimmedLength:(NSUInteger)subKeyLength {
|
2012-07-17 20:57:11 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
NSData *keyData = [self keyDataForAlgorithm:algorithm];
|
|
|
|
return [keyData subdataWithRange:NSMakeRange( 0, MIN( subKeyLength, keyData.length ) )];
|
2012-07-17 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isEqualToKey:(MPKey *)key {
|
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
return [[self keyIDForAlgorithm:MPAlgorithmDefault] isEqualToData:[key keyIDForAlgorithm:MPAlgorithmDefault]];
|
2012-07-17 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isEqual:(id)object {
|
|
|
|
|
|
|
|
if (![object isKindOfClass:[MPKey class]])
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
return [self isEqualToKey:object];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|