@@ -192,6 +192,24 @@ func parseDSN(dsn string) (cfg *config, err error) {
192
192
return
193
193
}
194
194
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
+
195
213
// Encrypt password using 4.1+ method
196
214
// http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#4.1_and_later
197
215
func scramblePassword (scramble , password []byte ) []byte {
@@ -244,58 +262,53 @@ func (r *myRnd) Float64() float64 {
244
262
return float64 (r .seed1 ) / myRndMaxVal
245
263
}
246
264
247
- // https://github.com/atcurtis/mariadb/blob/master/sql/password.c
248
265
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
251
271
252
272
for _ , c := range password {
273
+ // skip spaces and tabs in password
253
274
if c == ' ' || c == '\t' {
254
- continue // skip space in password
275
+ continue
255
276
}
256
277
257
278
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 ]
260
281
add += tmp
261
282
}
262
283
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
265
286
return
266
287
}
267
288
268
289
func scrambleOldPassword (scramble , password []byte ) []byte {
269
290
if len (password ) == 0 {
270
291
return nil
271
292
}
293
+
272
294
scramble = scramble [:8 ]
295
+
273
296
hashPw := pwHash (password )
274
297
hashSc := pwHash (scramble )
298
+
275
299
r := newMyRnd (hashPw [0 ]^ hashSc [0 ], hashPw [1 ]^ hashSc [1 ])
300
+
276
301
var out [8 ]byte
277
302
for i := range out {
278
303
out [i ] = byte (math .Floor (r .Float64 ()* 31 ) + 64 )
279
304
}
280
- extra := byte (math .Floor (r .Float64 () * 31 ))
281
- for i := range out {
282
- out [i ] ^= extra
283
- }
284
- return out [:]
285
- }
286
305
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
295
309
}
296
310
297
- // Not a valid bool value
298
- return
311
+ return out [:]
299
312
}
300
313
301
314
/******************************************************************************
0 commit comments