-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.go
250 lines (209 loc) · 4.97 KB
/
hash.go
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"sync"
"time"
"github.com/remeh/sizedwaitgroup"
"golang.org/x/crypto/bcrypt"
)
const (
HASH_SLEEP = time.Millisecond * 10
HASH_TIMEOUT = time.Second * 60
PASSPHRASE_HASH_COST = 14
HASH_DEPTH_MAX = 100
)
var hashDepth int
type hashData struct {
isTest bool
id uint64
desc *descData
pass []byte
hash []byte
doEncrypt bool
changePass bool
complete bool
failed bool
correctPass bool
started time.Time
workStarted time.Time
}
var hashList []*hashData
var hashLock sync.Mutex
func hashReceiver() {
hashLock.Lock()
defer hashLock.Unlock()
var newList []*hashData
var newCount int
for _, item := range hashList {
if item.complete && !item.failed {
if item.changePass {
changePass(item)
continue
}
if item.doEncrypt {
hashGenComplete(item)
} else {
passCheckComplete(item)
}
continue
} else if item.failed {
hashGenFail(item)
continue
} else if !item.workStarted.IsZero() && time.Since(item.workStarted) > HASH_TIMEOUT {
item.desc.send("The passphrase processing timed out. Sorry!")
critLog("#%v: passphrase hashing timed out...", item.id)
continue
}
newList = append(newList, item)
newCount++
}
hashList = newList
hashDepth = newCount
}
// Threaded async hash
func hasherDaemon() {
wg := sizedwaitgroup.New(numThreads)
for serverState == SERVER_RUNNING {
time.Sleep(HASH_SLEEP)
hashLock.Lock()
hashDepth := len(hashList)
//Empty, just exit
if hashDepth == 0 {
hashLock.Unlock()
continue
}
//Limit worksize to workload and threads
workSize := numThreads
if workSize > hashDepth {
workSize = hashDepth
}
//Copy pointer list, so we can work async
workList := hashList
hashLock.Unlock()
//Process worklist
for x := 0; x < workSize; x++ {
item := workList[(hashDepth-1)-x]
if item.desc != nil {
wg.Add()
go processHash(item, &wg)
}
}
wg.Wait()
//Wait until all threads return
}
}
func changePass(item *hashData) {
if item.doEncrypt {
item.desc.account.PassHash = item.hash
//Save account
if !item.desc.account.saveAccount() {
//Save failure
item.desc.sendln(warnBuf)
item.desc.sendln("Unable to save account!")
critLog("#%v unable to save account!", item.id)
item.desc.kill()
return
}
item.desc.sendln("Passphrase changed and saved.")
item.desc.state = CON_CHAR_LIST
showStatePrompt(item.desc)
} else {
if item.correctPass {
item.desc.state = CON_CHANGE_PASS_NEW
} else {
item.desc.sendln("Incorrect passphrase, try again.")
item.desc.state = CON_CHANGE_PASS_OLD
}
showStatePrompt(item.desc)
}
}
func processHash(item *hashData, wg *sizedwaitgroup.SizedWaitGroup) {
var err error
defer wg.Done()
passGood := false
if item.doEncrypt {
item.hash, err = bcrypt.GenerateFromPassword([]byte(item.pass), PASSPHRASE_HASH_COST)
} else {
if bcrypt.CompareHashAndPassword(item.hash, []byte(item.pass)) == nil {
passGood = true
}
}
if passGood {
item.correctPass = true
}
if err != nil {
item.failed = true
critLog("ERROR: #%v passphrase hashing failed!!!: %v", item.id, err.Error())
return
}
item.complete = true
}
func passCheckComplete(item *hashData) {
if item.isTest {
return
}
if item.desc == nil {
critLog("Player left before passphrase check finished.")
return
}
if item.correctPass {
//Send to char menu
item.desc.state = CON_CHAR_LIST
pCharList(item.desc)
} else {
item.desc.send("Incorrect passphrase.")
critLog("#%v tried an incorrect passphrase.", item.id)
item.desc.kill()
}
}
func hashGenComplete(item *hashData) {
if item.isTest {
return
}
if item.desc == nil {
critLog("Player left before passphrase hash finished.")
return
}
item.desc.sendln("Passphrase processing complete!")
item.desc.account.PassHash = item.hash
//Check again! We don't want a collision
//Otherwise could be exploitable.
if !isAccNameAvail(item.desc.account.Login) {
item.desc.send("Sorry, that login name is already in use.")
item.desc.kill()
}
//Create account
err := item.desc.account.createAccountDir()
if err != nil {
item.desc.sendln(warnBuf)
item.desc.sendln("Unable to create account!")
critLog("#%v unable to create account!", item.id)
item.desc.kill()
return
}
//Save account
if !item.desc.account.saveAccount() {
//Save failure
item.desc.send(warnBuf)
item.desc.sendln("Unable to save account!")
critLog("#%v unable to save account!", item.id)
item.desc.kill()
return
}
newAcc := &accountIndexData{
Login: item.desc.account.Login,
UUID: item.desc.account.UUID,
Added: time.Now().UTC(),
}
//Update acc index
accountIndex[item.desc.account.Login] = newAcc
saveAccountIndex()
//save success
item.desc.sendln("Account created and saved.")
//Send to char menu
item.desc.state = CON_CHAR_LIST
showStatePrompt(item.desc)
}
func hashGenFail(item *hashData) {
item.desc.send("Somthing went wrong processing your passphrase. Sorry!")
critLog("#%v passphrase hash failed!", item.id)
}