pEpObjCAdapter/PEPPassphraseCache.m
author Dirk Zimmermann <dz@pep.security>
Fri, 26 Jun 2020 16:54:34 +0200
branchIOSAD-172
changeset 1460 4cacc1f773c4
parent 1459 a2d09a38d424
child 1461 c7790f7d9185
permissions -rw-r--r--
IOSAD-172 Rename max number of passphrases
dz@1449
     1
//
dz@1449
     2
//  PEPPassphraseCache.m
dz@1449
     3
//  pEpObjCAdapter
dz@1449
     4
//
dz@1449
     5
//  Created by Dirk Zimmermann on 25.06.20.
dz@1449
     6
//  Copyright © 2020 p≡p. All rights reserved.
dz@1449
     7
//
dz@1449
     8
dz@1449
     9
#import "PEPPassphraseCache.h"
dz@1449
    10
dz@1460
    11
static NSUInteger s_maxNumberOfPassphrases = 20;
dz@1457
    12
dz@1454
    13
@interface PEPPassphraseCache ()
dz@1454
    14
dz@1456
    15
@property (nonatomic) dispatch_queue_t queue;
dz@1457
    16
@property (nonatomic) NSMutableArray *mutablePassphrases;
dz@1456
    17
dz@1454
    18
@end
dz@1454
    19
dz@1449
    20
@implementation PEPPassphraseCache
dz@1449
    21
dz@1449
    22
/// Internal constructor (for now).
dz@1449
    23
- (instancetype)initTimeout:(NSUInteger)timeout
dz@1449
    24
{
dz@1449
    25
    self = [super init];
dz@1449
    26
    if (self) {
dz@1449
    27
        _timeout = timeout;
dz@1456
    28
        _queue = dispatch_queue_create("PEPPassphraseCache Queue", DISPATCH_QUEUE_SERIAL);
dz@1460
    29
        _mutablePassphrases = [NSMutableArray arrayWithCapacity:s_maxNumberOfPassphrases];
dz@1449
    30
    }
dz@1449
    31
    return self;
dz@1449
    32
}
dz@1449
    33
dz@1449
    34
/// Public constructor with default values.
dz@1449
    35
- (instancetype)init
dz@1449
    36
{
dz@1449
    37
    return [self initTimeout:10 * 60];
dz@1449
    38
}
dz@1449
    39
dz@1449
    40
- (void)addPassphrase:(NSString *)passphrase
dz@1449
    41
{
dz@1459
    42
    dispatch_sync(self.queue, ^{
dz@1459
    43
        [self.mutablePassphrases addObject:passphrase];
dz@1460
    44
        if (self.mutablePassphrases.count > s_maxNumberOfPassphrases) {
dz@1459
    45
            [self.mutablePassphrases removeObjectAtIndex:0];
dz@1459
    46
        }
dz@1459
    47
    });
dz@1449
    48
}
dz@1449
    49
dz@1449
    50
- (NSArray *)passphrases
dz@1449
    51
{
dz@1457
    52
    NSMutableArray *resultingPassphrases = [NSMutableArray
dz@1460
    53
                                            arrayWithCapacity:s_maxNumberOfPassphrases + 1];
dz@1457
    54
    [resultingPassphrases addObject:@""];
dz@1457
    55
    dispatch_sync(self.queue, ^{
dz@1457
    56
        for (NSString *passphrase in self.mutablePassphrases) {
dz@1457
    57
            [resultingPassphrases addObject:passphrase];
dz@1457
    58
        }
dz@1457
    59
    });
dz@1457
    60
    return [NSArray arrayWithArray:resultingPassphrases];
dz@1449
    61
}
dz@1449
    62
dz@1449
    63
@end