Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Threema/Threema/Onboarding/   (Social Medium ©)  Datei vom 26.9.2026 mit Größe 7 kB image not shown  

Quelle  RandomSeedViewController.m  Sprache: unbekannt

 
Spracherkennung für: .m vermutete Sprache: Abap {ABAP[50] Lisp[38] Masm[38]} [Methode: maximale Elemente, drei Dimensionen]

#import "RandomSeedViewController.h"
#import "BundleUtil.h"
#import "MotionEntropyCollector.h"
#import "NaClCrypto.h"
#import <CommonCrypto/CommonDigest.h>
#import "NSString+Hex.h"
#import "UIDefines.h"
#import "LicenseStore.h"

#define NUM_POSITIONS_REQUIRED 200
#define NUM_LINES 16
#define NUM_BYTES 16
#define CHARACTER_SPACING 8.0
#define LINE_SPACING 6.0

@interface RandomSeedViewController () <MoveFingerDelegate>

@property MotionEntropyCollector *motionEntropyCollector;
@property BOOL doneCollecting;
@property BOOL startedCollectiong;

@property NSString *randomString;
@property NSMutableAttributedString *attributedString;

@property NSMutableArray *labelMatrix;

@property CGFloat accessabilityLastProgress;

@end

@implementation RandomSeedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
}

- (void)viewDidAppear:(BOOL)animated {
    UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, self.titleLabel);
}

- (void)adaptToSmallScreen {
    [super adaptToSmallScreen];
    
    CGFloat yOffset = -10.0;
    _titleLabel.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y + yOffset, _titleLabel.frame.size.width, _titleLabel.frame.size.height);
    
    yOffset -= 24.0;
    _actionLabel.frame = CGRectMake(_actionLabel.frame.origin.x, _actionLabel.frame.origin.y + yOffset, _actionLabel.frame.size.width, _actionLabel.frame.size.height);
    _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y + yOffset, _progressView.frame.size.width, _progressView.frame.size.height);
    _randomDataView.frame = CGRectMake(_randomDataView.frame.origin.x, _randomDataView.frame.origin.y + yOffset, _randomDataView.frame.size.width, _randomDataView.frame.size.height);
    _fingerView.frame = CGRectMake(_fingerView.frame.origin.x, _fingerView.frame.origin.y + yOffset, _fingerView.frame.size.width, _fingerView.frame.size.height);
    _fingerView.frame = CGRectMake(_fingerView.frame.origin.x, _fingerView.frame.origin.y, _fingerView.frame.size.width - 24.0, _fingerView.frame.size.height - 24.0);
    
    yOffset -= 20.0;
    _cancelButton.frame = CGRectMake(self.cancelButton.frame.origin.x, self.cancelButton.frame.origin.y + yOffset, self.cancelButton.frame.size.width, self.cancelButton.frame.size.height);
    
    yOffset = 45.0;
    self.moreView.frame = CGRectMake(self.moreView.frame.origin.x, self.moreView.frame.origin.y + yOffset, self.moreView.frame.size.width, self.moreView.frame.size.height);
}

- (void)setup {
    self.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;

    _randomDataBackground.layer.cornerRadius = 5.0;

    _progressView.tintColor = UIColor.tintColor;
    
    _titleLabel.text = [NSString stringWithFormat:[BundleUtil localizedStringForKey:@"welcome"], TargetManagerObjC.appName];
    _actionLabel.text = [NSString stringWithFormat:[BundleUtil localizedStringForKey:@"move_your_finger"], TargetManagerObjC.localizedAppName];
    
    self.moreView.mainView = self.mainContentView;
    self.moreView.moreButtonTitle = [BundleUtil localizedStringForKey:@"more_information"];
    self.moreView.moreMessageText = [NSString stringWithFormat:[BundleUtil localizedStringForKey:@"more_information_random_seed"], TargetManagerObjC.localizedAppName];
        
    _motionEntropyCollector = [[MotionEntropyCollector alloc] init];
    
    _randomDataView.delegate = self;
    _randomDataView.isAccessibilityElement = YES;
    [_randomDataView setAccessibilityHint:[NSString stringWithFormat:[BundleUtil localizedStringForKey:@"move_your_finger"], TargetManagerObjC.localizedAppName]];
    _randomDataView.accessibilityIdentifier = @"randomDataView";
    
    [self setupRandomMatrix];
    [self progressUpdate];
    
    [_motionEntropyCollector start];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    _accessabilityLastProgress = 0.0;
    
    _fingerView.image = [StyleKit finger];

    _cancelButton.cancelStyle = YES;
    _cancelButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    _cancelButton.titleLabel.minimumScaleFactor = 0.6;
    [_cancelButton setTitle:[BundleUtil localizedStringForKey:@"cancel"] forState:UIControlStateNormal];
}

- (void)setupRandomMatrix {
    CGFloat size = _randomDataView.frame.size.width / NUM_BYTES;
    
    _labelMatrix = [NSMutableArray arrayWithCapacity:NUM_LINES*NUM_BYTES];
    
    NSData *random = [[NaClCrypto sharedCrypto] randomBytes:(NUM_LINES*NUM_BYTES)];
    NSString *randomString = [NSString stringWithHexData:random];
    randomString = [randomString uppercaseString];
    
    for (int n = 0; n < NUM_LINES; n++) {
        for (int i = 0; i < NUM_BYTES; i++) {
            CGRect rect = CGRectMake(i*size, n*size, size, size);
            UILabel *label = [self charLabelAt:rect];
            label.text = [randomString substringWithRange:NSMakeRange((i+1)*(n+1), 1)];
            label.isAccessibilityElement = NO;
            
            [_randomDataView addSubview:label];
            [_labelMatrix addObject:label];
        }
    }
}

- (UILabel *)charLabelAt:(CGRect)rect {
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = THREEMA_COLOR_LIGHT_GREY;
    label.textAlignment = NSTextAlignmentCenter;
    label.lineBreakMode = NSLineBreakByClipping;
    label.backgroundColor = [UIColor clearColor];

    return label;
}

- (void)dealloc {
    [_motionEntropyCollector stop];
}

- (void)didMoveFingerInView:(MoveFingerView *)view {
    if (_startedCollectiong == NO) {
        _startedCollectiong = YES;
        [UIView animateWithDuration:0.3 animations:^{
            _fingerView.alpha = 0.0;
        } completion:^(BOOL finished) {
            _fingerView.hidden = YES;
        }];
    }
    
    [self progressUpdate];
    
    if (_randomDataView.numberOfPositionsRecorded >= NUM_POSITIONS_REQUIRED && !_doneCollecting) {
        /* done! */
        _doneCollecting = YES;
        
        NSData *seed = [self getSeed];
        if ([_delegate respondsToSelector: @selector(generatedRandomSeed:)]) {
            [_delegate generatedRandomSeed:seed];
        }
    }
}

- (void)progressUpdate {
    CGFloat progress = (float)_randomDataView.numberOfPositionsRecorded / NUM_POSITIONS_REQUIRED;
    
    _progressView.progress = progress;
    
    if (progress >= 1.0) {
        for (UILabel *label in _labelMatrix) {
            label.textColor = UIColor.tintColor;
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, [BundleUtil localizedStringForKey:@"done"]);
        });

    } else if (progress > 0) {
        int posIncrement = 1.0/progress + arc4random() % 5;
        
        for (int i=0; i < _labelMatrix.count; i += posIncrement) {
            // some randomness if the color should change
            int random = arc4random();
            if (random % 10 > 8) {
                UILabel *label = _labelMatrix[i];
                label.textColor = UIColor.tintColor;
            }
        }

        if (progress - _accessabilityLastProgress >= 0.1) {
            dispatch_async(dispatch_get_main_queue(), ^{
                UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, _progressView.accessibilityValue);
            });
            _accessabilityLastProgress = progress;
        }
    }
}

- (NSData *)getSeed {
    NSData *motionSeed = [_motionEntropyCollector stop];
    NSData *fingerSeed = _randomDataView.digest;
    
    /* mix the two seeds */
    CC_SHA256_CTX ctx;
    CC_SHA256_Init(&ctx);
    if (motionSeed.length > 0)
        CC_SHA256_Update(&ctx, motionSeed.bytes, (CC_LONG)motionSeed.length);
    if (fingerSeed.length > 0)
        CC_SHA256_Update(&ctx, fingerSeed.bytes, (CC_LONG)fingerSeed.length);
    unsigned char digest[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256_Final(digest, &ctx);
    NSData *seed = [NSData dataWithBytes:digest length:sizeof(digest)];
    
    return seed;
}

- (IBAction)cancelButtonPressed:(id)sender {
    [_delegate cancelPressed];
}

@end

[Dauer der Verarbeitung: 0.19 Sekunden]