-
Notifications
You must be signed in to change notification settings - Fork 1
/
Password_GeneratorAppDelegate.m
109 lines (85 loc) · 2.98 KB
/
Password_GeneratorAppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// Password_GeneratorAppDelegate.m
// Password Generator
//
// Created by Neil Ang on 26/01/11.
// Copyright 2011 neilang.com. All rights reserved.
//
#import "Password_GeneratorAppDelegate.h"
@implementation Password_GeneratorAppDelegate
@synthesize window;
@synthesize lettersSlider = _lettersSlider;
@synthesize capitalsSlider = _capitalsSlider;
@synthesize digitsSlider = _digitsSlider;
@synthesize symbolsSlider = _symbolsSlider;
@synthesize passwordField = _passwordField;
@synthesize lettersField = _lettersField;
@synthesize capitalsField = _capitalsField;
@synthesize digitsField = _digitsField;
@synthesize symbolsField = _symbolsField;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {}
- (void)awakeFromNib {
[self generatePassword:nil];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES;
}
// Helper function for appending random chars from a supplied alphabet
// And then advances the pointer
char *appendRandom(char *str, char *alphabet, int amount) {
for (int i = 0; i < amount; i++) {
int r = arc4random() % strlen(alphabet);
*str = alphabet[r];
str++;
}
return str;
}
- (IBAction)generatePassword:(id)sender {
// Get slider values
int letters = [self.lettersSlider intValue];
int capitals = [self.capitalsSlider intValue];
int digits = [self.digitsSlider intValue];
int symbols = [self.symbolsSlider intValue];
int length = letters + capitals + digits + symbols;
// Update labels
[self.lettersField setStringValue:[NSString stringWithFormat:@"%d", letters]];
[self.capitalsField setStringValue:[NSString stringWithFormat:@"%d", capitals]];
[self.digitsField setStringValue:[NSString stringWithFormat:@"%d", digits]];
[self.symbolsField setStringValue:[NSString stringWithFormat:@"%d", symbols]];
// Build the password using C strings - for speed
char *cPassword = calloc(length + 1, sizeof(char));
char *ptr = cPassword;
cPassword[length - 1] = '\0';
char *lettersAlphabet = "abcdefghijklmnopqrstuvwxyz";
ptr = appendRandom(ptr, lettersAlphabet, letters);
char *capitalsAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ptr = appendRandom(ptr, capitalsAlphabet, capitals);
char *digitsAlphabet = "0123456789";
ptr = appendRandom(ptr, digitsAlphabet, digits);
char *symbolsAlphabet = "!@#$%*[];?()";
ptr = appendRandom(ptr, symbolsAlphabet, symbols);
// Shuffle the string!
for (int i = 0; i < length; i++) {
int r = arc4random() % length;
char temp = cPassword[i];
cPassword[i] = cPassword[r];
cPassword[r] = temp;
}
// Show the password
[self.passwordField setStringValue:[NSString stringWithCString:cPassword encoding:NSUTF8StringEncoding]];
// Clean up
free(cPassword);
}
- (void)dealloc {
self.lettersSlider = nil;
self.capitalsSlider = nil;
self.digitsSlider = nil;
self.symbolsSlider = nil;
self.passwordField = nil;
self.lettersField = nil;
self.capitalsField = nil;
self.digitsField = nil;
self.symbolsField = nil;
[super dealloc];
}
@end