@@ -17,6 +17,7 @@ import (
17
17
"fmt"
18
18
"io"
19
19
"log"
20
+ "math"
20
21
"os"
21
22
"regexp"
22
23
"strings"
@@ -213,6 +214,67 @@ func scramblePassword(scramble, password []byte) []byte {
213
214
return scramble
214
215
}
215
216
217
+ // Encrypt password using pre 4.1 (old password) method
218
+ // https://github.com/atcurtis/mariadb/blob/master/mysys/my_rnd.c
219
+ type myRnd struct {
220
+ seed1 , seed2 uint32
221
+ }
222
+
223
+ const myRndMaxVal = 0x3FFFFFFF
224
+
225
+ func newMyRnd (seed1 , seed2 uint32 ) * myRnd {
226
+ r := new (myRnd )
227
+ r .seed1 = seed1 % myRndMaxVal
228
+ r .seed2 = seed2 % myRndMaxVal
229
+ return r
230
+ }
231
+
232
+ func (r * myRnd ) Float64 () float64 {
233
+ r .seed1 = (r .seed1 * 3 + r .seed2 ) % myRndMaxVal
234
+ r .seed2 = (r .seed1 + r .seed2 + 33 ) % myRndMaxVal
235
+ return float64 (r .seed1 ) / myRndMaxVal
236
+ }
237
+
238
+ // https://github.com/atcurtis/mariadb/blob/master/sql/password.c
239
+ func pwHash (password []byte ) (result [2 ]uint32 ) {
240
+ var nr , add , nr2 , tmp uint32
241
+ nr , add , nr2 = 1345345333 , 7 , 0x12345671
242
+
243
+ for _ , c := range password {
244
+ if c == ' ' || c == '\t' {
245
+ continue // skip space in password
246
+ }
247
+
248
+ tmp = uint32 (c )
249
+ nr ^= (((nr & 63 ) + add ) * tmp ) + (nr << 8 )
250
+ nr2 += (nr2 << 8 ) ^ nr
251
+ add += tmp
252
+ }
253
+
254
+ result [0 ] = nr & ((1 << 31 ) - 1 ) // Don't use sign bit (str2int)
255
+ result [1 ] = nr2 & ((1 << 31 ) - 1 )
256
+ return
257
+ }
258
+
259
+ func scrambleOldPassword (scramble , password []byte ) []byte {
260
+ if len (password ) == 0 {
261
+ return nil
262
+ }
263
+ scramble = scramble [:8 ]
264
+ hashPw := pwHash (password )
265
+ hashSc := pwHash (scramble )
266
+ r := newMyRnd (hashPw [0 ]^ hashSc [0 ], hashPw [1 ]^ hashSc [1 ])
267
+ var out [8 ]byte
268
+ for i := range out {
269
+ out [i ] = byte (math .Floor (r .Float64 ()* 31 ) + 64 )
270
+ }
271
+ extra := byte (math .Floor (r .Float64 () * 31 ))
272
+ for i := range out {
273
+ out [i ] ^= extra
274
+ }
275
+ return out [:]
276
+ }
277
+
216
278
// Returns the bool value of the input.
217
279
// The 2nd return value indicates if the input was a valid bool value
218
280
func readBool (input string ) (value bool , valid bool ) {
0 commit comments