Skip to content

Commit 092a0c3

Browse files
committed
Merge pull request #32 from shenli/master
util: Limit random salt to ascii chars
2 parents dc5ca30 + 358bcad commit 092a0c3

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

mysql/util.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package mysql
22

33
import (
4-
"crypto/rand"
54
"crypto/sha1"
65
"encoding/binary"
76
"fmt"
87
"io"
8+
"math/rand"
99
"runtime"
10+
"time"
1011
"unicode/utf8"
1112
)
1213

@@ -45,20 +46,16 @@ func CalcPassword(scramble, password []byte) []byte {
4546
return scramble
4647
}
4748

48-
func RandomBuf(size int) ([]byte, error) {
49+
func RandomBuf(size int) []byte {
4950
buf := make([]byte, size)
50-
51-
if _, err := io.ReadFull(rand.Reader, buf); err != nil {
52-
return nil, err
53-
}
54-
55-
// avoid to generate '\0'
56-
for i, b := range buf {
57-
if uint8(b) == 0 {
58-
buf[i] = '0'
51+
rand.Seed(time.Now().UTC().UnixNano())
52+
for i := 0; i < size; i++ {
53+
buf[i] = byte(rand.Intn(127))
54+
if buf[i] == 0 || buf[i] == byte('$') {
55+
buf[i]++
5956
}
6057
}
61-
return buf, nil
58+
return buf
6259
}
6360

6461
func LengthEncodedInt(b []byte) (num uint64, isNull bool, n int) {

proxy/conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (s *Server) newConn(co net.Conn) *Conn {
7373

7474
c.status = SERVER_STATUS_AUTOCOMMIT
7575

76-
c.salt, _ = RandomBuf(20)
76+
c.salt = RandomBuf(20)
7777

7878
c.txConns = make(map[*Node]*client.SqlConn)
7979

0 commit comments

Comments
 (0)