-
Notifications
You must be signed in to change notification settings - Fork 0
/
passutils.nim
213 lines (151 loc) · 5.07 KB
/
passutils.nim
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
#import system/io # for some reason this just stopped working. idk why, but let it be
import strutils
import dialogs
import safeutils
const nameMinLength* = 2
let filename = getEnv("HOME") / ".nimpass-passwords.txt"
type
Entry* = object
name*: string
password*: string
Storage = object
masterkeySum: string
len: int
entries*: seq[Entry]
InvalidMasterkeyError* = object of ValueError
EntryNotFoundError* = object of ValueError
EntryAlreadyExistsError* = object of ValueError
NameIsTooShortError* = object of ValueError
PasswordContainsBadCharacterError* = object of ValueError
proc toBytes(s: string): seq[byte] =
for i in 0..<s.len:
result.add(byte(s[i]))
return result
proc fromBytes(s: seq[byte]): string =
for i in 0..<s.len:
result.add(char(s[i]))
return result
proc writeLen(f: File, n: int) =
assert n <= 0xFFFF
var byte1 = n shr 8
var byte2 = n and 0xFF
assert f.writeBytes([byte(byte1), byte(byte2)], 0, 2) == 2
proc readLen(f: File): int =
var bytes: array[2, byte]
assert f.readBytes(bytes, 0, 2) == 2
return (int(bytes[0]) shl 8) + int(bytes[1])
proc writeString(f: File, s: string) =
writeLen(f, s.len)
assert f.writeBytes(toBytes(s), 0, s.len) == s.len
proc readString(f: File): string =
var len = readLen(f)
if len == 0:
return ""
var bytes = newSeq[byte](len)
assert f.readBytes(bytes, 0, len) == len
return fromBytes(bytes)
proc writeMasterkey(f: File, s: string) =
writeString(f, s)
proc readMasterkey(f: File): string =
return readString(f)
proc writeEntry(f: File, e: Entry) =
writeString(f, e.name)
writeString(f, e.password)
proc readEntry(f: File): Entry =
result.name = readString(f)
result.password = readString(f)
return result
proc decryptStorage(mkey: string, s: var Storage) =
for (_, e) in mpairs(s.entries):
e.name = decryptText(mkey, e.name)
e.password = decryptText(mkey, e.password)
proc encryptStorage(mkey: string, s: var Storage) =
for (_, e) in mpairs(s.entries):
e.name = encryptText(mkey, e.name)
e.password = encryptText(mkey, e.password)
proc writeStorage(mkey: string, s: Storage) =
var s = s
encryptStorage(mkey, s)
var f = open(filename, fmWrite)
writeMasterkey(f, s.masterkeySum)
writeLen(f, s.len)
for e in s.entries:
writeEntry(f, e)
f.close()
proc readStorage*(mkey: string): Storage =
var f = open(filename)
result.masterkeySum = readMasterkey(f)
result.len = readLen(f)
for i in 0..<result.len:
result.entries.add(readEntry(f))
f.close()
decryptStorage(mkey, result)
return result
proc verifyMasterkey*(mkey: string) =
var f = open(filename)
var key = readString(f)
f.close()
if hashsum(mkey) != key:
raise newException(InvalidMasterkeyError, "Invalid masterkey.")
proc initStorage*() =
if fileExists(filename):
return
var masterkey = initStoragePrompt().strip(chars = {'\n'})
if masterkey == "":
echo "Failed to initialize storage."
quit(1)
var s: Storage
s.masterkeySum = hashsum(masterkey)
s.len = 0
writeStorage(masterkey, s)
echo "Successfully initialized storage: $1" % filename
proc checkNameLength(name: string) =
if name.len < nameMinLength:
raise newException(NameIsTooShortError, "Name $1 is too short" % name)
proc checkPassword(pass: string) =
for c in pass:
if c notin alphabet:
raise newException(PasswordContainsBadCharacterError, "Password contains illegal character.")
proc getEntry*(mkey: string, name: string): Entry =
verifyMasterkey(mkey)
checkNameLength(name)
var s = readStorage(mkey)
for e in s.entries:
if e.name == name:
return e
raise newException(EntryNotFoundError, "Entry with name $1 was not found" % name)
proc checkEntry*(mkey: string, name: string): bool =
try:
discard getEntry(mkey, name)
except EntryNotFoundError:
return false
return true
proc insertEntry*(mkey: string, e: Entry) =
verifyMasterkey(mkey)
checkNameLength(e.name)
checkPassword(e.password)
checkPassword(e.name)
var exists = checkEntry(mkey, e.name)
if exists:
raise newException(EntryAlreadyExistsError, "Entry with name $1 already exists." % e.name)
var s = readStorage(mkey)
s.len += 1
s.entries.add(e)
writeStorage(mkey, s)
proc deleteEntry*(mkey: string, name: string) =
verifyMasterkey(mkey)
checkNameLength(name)
var exists = checkEntry(mkey, name)
if not exists:
raise newException(EntryNotFoundError, "Entry with name $1 was not found." % name)
var s = readStorage(mkey)
s.len -= 1
var pos = -1
for (i, e) in pairs(s.entries):
if e.name == name:
pos = i
break
assert pos != -1
s.entries.delete(pos)
writeStorage(mkey, s)