Skip to content

Commit 0bc514d

Browse files
committed
Small refactoring
1 parent 83ed16b commit 0bc514d

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

utils.go

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,24 @@ func parseDSN(dsn string) (cfg *config, err error) {
192192
return
193193
}
194194

195+
// Returns the bool value of the input.
196+
// The 2nd return value indicates if the input was a valid bool value
197+
func readBool(input string) (value bool, valid bool) {
198+
switch input {
199+
case "1", "true", "TRUE", "True":
200+
return true, true
201+
case "0", "false", "FALSE", "False":
202+
return false, true
203+
}
204+
205+
// Not a valid bool value
206+
return
207+
}
208+
209+
/******************************************************************************
210+
* Authentication *
211+
******************************************************************************/
212+
195213
// Encrypt password using 4.1+ method
196214
// http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#4.1_and_later
197215
func scramblePassword(scramble, password []byte) []byte {
@@ -244,58 +262,53 @@ func (r *myRnd) Float64() float64 {
244262
return float64(r.seed1) / myRndMaxVal
245263
}
246264

247-
// https://github.com/atcurtis/mariadb/blob/master/sql/password.c
248265
func pwHash(password []byte) (result [2]uint32) {
249-
var nr, add, nr2, tmp uint32
250-
nr, add, nr2 = 1345345333, 7, 0x12345671
266+
var add uint32 = 7
267+
var tmp uint32
268+
269+
result[0] = 1345345333
270+
result[1] = 0x12345671
251271

252272
for _, c := range password {
273+
// skip spaces and tabs in password
253274
if c == ' ' || c == '\t' {
254-
continue // skip space in password
275+
continue
255276
}
256277

257278
tmp = uint32(c)
258-
nr ^= (((nr & 63) + add) * tmp) + (nr << 8)
259-
nr2 += (nr2 << 8) ^ nr
279+
result[0] ^= (((result[0] & 63) + add) * tmp) + (result[0] << 8)
280+
result[1] += (result[1] << 8) ^ result[0]
260281
add += tmp
261282
}
262283

263-
result[0] = nr & ((1 << 31) - 1) // Don't use sign bit (str2int)
264-
result[1] = nr2 & ((1 << 31) - 1)
284+
result[0] &= (1 << 31) - 1 // Don't use sign bit (str2int)
285+
result[1] &= (1 << 31) - 1
265286
return
266287
}
267288

268289
func scrambleOldPassword(scramble, password []byte) []byte {
269290
if len(password) == 0 {
270291
return nil
271292
}
293+
272294
scramble = scramble[:8]
295+
273296
hashPw := pwHash(password)
274297
hashSc := pwHash(scramble)
298+
275299
r := newMyRnd(hashPw[0]^hashSc[0], hashPw[1]^hashSc[1])
300+
276301
var out [8]byte
277302
for i := range out {
278303
out[i] = byte(math.Floor(r.Float64()*31) + 64)
279304
}
280-
extra := byte(math.Floor(r.Float64() * 31))
281-
for i := range out {
282-
out[i] ^= extra
283-
}
284-
return out[:]
285-
}
286305

287-
// Returns the bool value of the input.
288-
// The 2nd return value indicates if the input was a valid bool value
289-
func readBool(input string) (value bool, valid bool) {
290-
switch input {
291-
case "1", "true", "TRUE", "True":
292-
return true, true
293-
case "0", "false", "FALSE", "False":
294-
return false, true
306+
mask := byte(math.Floor(r.Float64() * 31))
307+
for i := range out {
308+
out[i] ^= mask
295309
}
296310

297-
// Not a valid bool value
298-
return
311+
return out[:]
299312
}
300313

301314
/******************************************************************************

0 commit comments

Comments
 (0)