-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
188 lines (165 loc) · 5.33 KB
/
main.swift
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
179
180
181
182
183
184
185
186
187
//
// main.swift
// diceware
//
// Created by ArgentWolf (SilverWolf32) on 2019-04-17
//
// This is free and unencumbered software released into the public domain.
// See COPYING.md for more information.
//
// vim:set list noet fo=tcqr:
//
import Foundation
import FoundationNetworking // required for Swift 5.1
var wordcount = 5
var wordlistURL = URL(string: "https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt")!
func printUsage() {
print("""
usage: \(CommandLine.arguments[0]) [\("word count".underlined())] [\("wordlist URL".underlined())]
""")
}
// handle the arguments
if (CommandLine.arguments.count > 1) {
if let tmpWordcount = Int(CommandLine.arguments[1]) {
wordcount = tmpWordcount
} else {
printUsage()
exit(1)
}
}
if (CommandLine.arguments.count > 2) {
if let newList = URL(string: CommandLine.arguments[2]) {
wordlistURL = newList
} else {
printUsage()
fputs("(not a valid URL)\n", stderr)
exit(1)
}
}
// make it a file:// URL if it's a local path
if wordlistURL.scheme == nil {
// fputs("Word list seems to be a local path, so making it a file URL\n", stderr)
wordlistURL = URL(fileURLWithPath: wordlistURL.path)
}
// fputs("Word count: \(wordcount)\n", stderr)
// fputs("List: \(wordlistURL)\n", stderr)
////////////////////////////////////////
let homedir = FileManager.default.homeDirectoryForCurrentUser.path
var cachedir = ""
if FileManager.default.fileExists(atPath: homedir + "/Library/Caches") {
// we're probably on a Mac, use ~/Library/Caches
cachedir = homedir + "/Library/Caches/diceware/"
} else {
// we're probably not on Mac, use ~/.cache instead
cachedir = homedir + "/.cache/diceware/"
}
var filename: String? = wordlistURL.lastPathComponent
if filename! == "" {
print("Couldn't get last path component, using domain instead")
filename = wordlistURL.host
guard filename != nil else {
print("Couldn't get domain either!")
print("Are you \("sure".underlined()) this is a valid URL?")
exit(2)
}
}
var cachepath = cachedir + filename!
var wordlistContents = ""
fputs("Checking for cached word list...", stderr)
if FileManager.default.fileExists(atPath: cachepath) {
fputs("found\n", stderr)
fputs("Using cached word list from \(cachepath)\n", stderr)
do {
wordlistContents = try String(contentsOfFile: cachepath)
} catch let e {
fputs("Error reading cached file! \(e.localizedDescription)\n", stderr)
exit(2)
}
} else {
fputs("not found\n", stderr)
fputs("Fetching word list from \(wordlistURL)...\n", stderr)
// get the word list
do {
wordlistContents = try String(contentsOf: wordlistURL)
} catch let e {
fputs("Error fetching word list: \(e.localizedDescription)\n", stderr)
exit(2)
}
fputs("Done!\n", stderr)
// cache the word list
fputs("Caching the word list for next time...\n", stderr)
do {
if !FileManager.default.fileExists(atPath: cachedir) {
try FileManager.default.createDirectory(atPath: cachedir, withIntermediateDirectories: true, attributes: nil)
}
try wordlistContents.write(toFile: cachepath, atomically: true, encoding: .utf8)
fputs("Saved word list to \(cachepath)\n", stderr)
} catch let e {
fputs("Error saving word list! \(e.localizedDescription)\n", stderr)
}
}
// fputs("Word list contents: \n\(wordlistContents)\n", stderr)
////////////////////////////////////////
// find the number of dice
var lines = wordlistContents.components(separatedBy: "\n")
// filter out blank lines and comments
lines = lines.filter { (line) in
if line.count == 0 {
return false
}
if line.hasPrefix("#") {
return false
}
return true
}
let firstID = lines[0].components(separatedBy: "\t")[0]
let nDice = firstID.count // number of digits = number of dice
// generate the word list
var generator = DevRandomGenerator()
var words: [String] = []
for _ in 0..<wordcount {
// this would not limit to 1...6
// let random6 = generator.next()
// this is not uniform, DO NOT USE! It's just for testing whether % affects uniformity -- turns out, yes it does.
// see https://crypto.stackexchange.com/questions/22767/does-using-modulo-affect-quality-of-randomness
// let random6 = generator.next() % 7 + 1
// print("\(random6)", terminator: " ")
// get X random numbers to use for passwords
var wordID = ""
for _ in 0..<nDice {
// 6-sided die
let random6 = Int.random(in: 1...6, using: &generator)
wordID += "\(random6)"
}
// get a random word
let matchingLines = lines.filter {
let components = $0.components(separatedBy: "\t")
if components.count == 0 {
return false // it's probably a blank line
}
return components[0] == wordID
}
// print(matchingWords)
// there should be one, and only one, match
if matchingLines.count == 0 {
fputs("*** Couldn't find word #\(wordID)! ***\n", stderr)
fputs("Your wordlist needs to have every possible sequence of \(nDice) numbers from 1 to 6.\n", stderr)
exit(3)
}
let components = matchingLines[0].components(separatedBy: "\t")
if components.count > 2 {
// can't get the actual word out of it
fputs("*** Couldn't find word on line #\(wordID)! ***\n", stderr)
fputs("Your wordlist needs to have the format '\(String(repeating: "#", count: nDice)) <tab> <word>'\n", stderr)
exit(3)
}
words.append(components[1])
}
// print out the words!
for i in 0..<words.count {
print(words[i], terminator: "")
if i != words.count - 1 { // not the last word
print(" ", terminator: "")
}
}
print("") // complete with a newline