Skip to content

Commit a9be95a

Browse files
authored
Merge pull request #242 from devfeel/develop
Bug fix: fix GetRandString return same result
2 parents a93379d + 7a12abd commit a9be95a

5 files changed

Lines changed: 26 additions & 11 deletions

File tree

consts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotweb
33
// Global define
44
const (
55
// Version current version
6-
Version = "1.7.16"
6+
Version = "1.7.17"
77
)
88

99
// Log define

dotweb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ func (app *DotWeb) printDotLogo() {
670670
app.Logger().Print(` / / / / / __ \ / __/| | /| / / / _ \ / __ \`, LogTarget_HttpServer)
671671
app.Logger().Print(` / /_/ / / /_/ // /_ | |/ |/ / / __/ / /_/ /`, LogTarget_HttpServer)
672672
app.Logger().Print(`/_____/ \____/ \__/ |__/|__/ \___/ /_.___/`, LogTarget_HttpServer)
673-
app.Logger().Print(` Version 1.7.14`, LogTarget_HttpServer)
673+
app.Logger().Print(` Version `+Version, LogTarget_HttpServer)
674674
}
675675

676676
// Close immediately stops the server.

framework/crypto/cryptos.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package cryptos
22

33
import (
4+
"bytes"
45
"crypto/md5"
5-
"math/rand"
6+
"crypto/rand"
67
"encoding/hex"
7-
"time"
8+
"math/big"
89
)
910

1011
// GetMd5String compute the md5 sum as string
@@ -16,11 +17,14 @@ func GetMd5String(s string) string {
1617

1718
// GetRandString returns randominzed string with given length
1819
func GetRandString(length int) string {
19-
bytes := []byte("0123456789abcdefghijklmnopqrstuvwxyz")
20-
result := []byte{}
21-
r := rand.New(rand.NewSource(time.Now().UnixNano()))
20+
var container string
21+
var str = "0123456789abcdefghijklmnopqrstuvwxyz"
22+
b := bytes.NewBufferString(str)
23+
len := b.Len()
24+
bigInt := big.NewInt(int64(len))
2225
for i := 0; i < length; i++ {
23-
result = append(result, bytes[r.Intn(len(bytes))])
26+
randomInt, _ := rand.Int(rand.Reader, bigInt)
27+
container += string(str[randomInt.Int64()])
2428
}
25-
return string(result)
29+
return container
2630
}

framework/crypto/cryptos_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ func Test_GetMd5String_1(t *testing.T) {
1717

1818
func Test_GetRandString(t *testing.T) {
1919
randStr := GetRandString(12)
20-
t.Log("GetRandString:", randStr)
21-
test.Equal(t, 12, len(randStr))
20+
rand1 := GetRandString(12)
21+
rand2 := GetRandString(12)
22+
rand3 := GetRandString(12)
23+
if rand1 == rand2 || rand2 == rand3 || rand1 == rand3 {
24+
t.Error("rand result is same")
25+
} else {
26+
t.Log("GetRandString:", randStr)
27+
test.Equal(t, 12, len(randStr))
28+
}
2229
}

version.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## dotweb版本记录:
22

3+
####Version 1.7.17
4+
* Bug fix: fix GetRandString return same result
5+
* 2021-01-29 08:00 at ShangHai
6+
37
####Version 1.7.16
48
* Bug fix: fix middleware chain misbehaving in netsed groups
59
* Tips: for issue #234, thanks for @live's code

0 commit comments

Comments
 (0)