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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
//
// ContentView.swift
// LibreSecrets
//
// Created by Christian Cleberg on 2024-01-10.
//
import SwiftUI
import UniformTypeIdentifiers
struct ContentView: View {
// Create initial variables
@State private var speed = 50.0
@State private var isEditing = false
@State private var enableNumbers = false
@State private var enableSpecial = false
@State private var enableCapitalization = false
@State private var isCopied: Bool = false
// Create Picker options to choose password type
enum PasswordType: String, CaseIterable, Identifiable {
case random, xkcd
var id: Self { self }
}
@State private var passwordType: PasswordType = .random
// Generates a random string of alphanumeric, numeric (optional), and special (optional) characters per user-determined length
func randomString(length: Int) -> String {
var characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
if enableNumbers {
characters.append("0123456789")
}
if enableSpecial {
characters.append("!%&()*+,-./:;<=>?@[^_`{|}~")
}
return String((0..<length).map{ _ in characters.randomElement()! })
}
// Generates a series of words separated by "-", including a digit (optional), and capitalization (optional) per user-determined length
func randomWord(length: Int) -> String {
var randomLine = ""
for i in 1...length {
if let wordsFilePath = Bundle.main.path(forResource: "words", ofType: nil) {
do {
let wordsString = try String(contentsOfFile: wordsFilePath)
let wordLines = wordsString.components(separatedBy: .newlines)
if enableCapitalization {
randomLine += wordLines[numericCast(arc4random_uniform(numericCast(wordLines.count)))].capitalized
} else {
randomLine += wordLines[numericCast(arc4random_uniform(numericCast(wordLines.count)))]
}
if i != length {
randomLine += "-"
}
if i == length && enableNumbers {
randomLine += String(Int.random(in: 0..<9))
}
} catch { // contentsOfFile throws an error
print("Error: \(error)")
}
}
}
return randomLine
}
// Generate the view
var body: some View {
VStack {
VStack {
Text("Password Generator")
.font(.largeTitle)
Text("Save your password somewhere safe!")
.font(.caption)
}
Form {
Section(header: Text("Password Type")) {
Picker("Type", selection: $passwordType) {
Text("Random").tag(PasswordType.random)
Text("XKCD").tag(PasswordType.xkcd)
}
}
if passwordType == .random {
Section(header: Text("Random Password")) {
Slider(
value: $speed,
in: 8...36,
step: 1
) {
Text("Characters")
} minimumValueLabel: {
Text("8")
} maximumValueLabel: {
Text("36")
} onEditingChanged: { editing in
isEditing = editing
}
.onAppear {
self.speed = 12
}
Toggle("Numbers", isOn: $enableNumbers)
Toggle("Special Characters", isOn: $enableSpecial)
}
let password = randomString(length: Int(speed))
Text("\(password)")
.onTapGesture {
let clipboard = UIPasteboard.general
clipboard.setValue(password, forPasteboardType: UTType.plainText.identifier)
withAnimation {
isCopied = true
}
DispatchQueue.main.asyncAfter(wallDeadline: .now() + 3) {
withAnimation {
isCopied = false
}
}
}
} else {
Section(header: Text("XKCD Password")) {
Slider(
value: $speed,
in: 1...10,
step: 1
) {
Text("Words")
} minimumValueLabel: {
Text("1")
} maximumValueLabel: {
Text("10")
} onEditingChanged: { editing in
isEditing = editing
}
.onAppear {
self.speed = 4
}
Toggle("Numbers", isOn: $enableNumbers)
Toggle("Capitalize Words", isOn: $enableCapitalization)
}
let password = randomWord(length: Int(speed))
Text("\(password)")
.onTapGesture {
let clipboard = UIPasteboard.general
clipboard.setValue(password, forPasteboardType: UTType.plainText.identifier)
withAnimation {
isCopied = true
}
DispatchQueue.main.asyncAfter(wallDeadline: .now() + 3) {
withAnimation {
isCopied = false
}
}
}
}
}
if isCopied {
Text("Copied successfully!")
.foregroundColor(.white)
.bold()
.font(.footnote)
.frame(width: 140, height: 30)
.background(Color.indigo.cornerRadius(7))
}
}
.padding()
}
}
#Preview {
ContentView()
}
|