Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Łukasz Nowak
caddy
Commits
8d1da68b
Commit
8d1da68b
authored
7 years ago
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
D'oh, commit all changes to file
parent
7a7e3d16
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
18 deletions
+24
-18
caddyhttp/httpserver/context.go
caddyhttp/httpserver/context.go
+24
-18
No files found.
caddyhttp/httpserver/context.go
View file @
8d1da68b
...
...
@@ -5,6 +5,7 @@ import (
"crypto/rand"
"fmt"
"io/ioutil"
mathrand
"math/rand"
"net"
"net/http"
"net/url"
...
...
@@ -356,35 +357,40 @@ func (c Context) IsMITM() bool {
}
// RandomString generates a random string of random length given
// length bounds. Thanks to http://stackoverflow.com/a/3
1832326
/1048862
// for the clever technique that is fa
st and maintains proper
// distributions over the dictionary.
// length bounds. Thanks to http://stackoverflow.com/a/3
5615565
/1048862
// for the clever technique that is fa
irly fast, secure, and maintains
//
proper
distributions over the dictionary.
func
(
c
Context
)
RandomString
(
minLen
,
maxLen
int
)
string
{
const
(
letterBytes
=
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
letterIdxBits
=
6
// 6 bits to represent a letter index
letterIdxMask
=
1
<<
letterIdxBits
-
1
// All 1-bits, as many as letterIdxBits
letterIdxMax
=
63
/
letterIdxBits
// # of letter indices fitting in 63 bits
letterIdxBits
=
6
// 6 bits to represent 64 possibilities (indexes)
letterIdxMask
=
1
<<
letterIdxBits
-
1
// all 1-bits, as many as letterIdxBits
)
if
minLen
<
0
||
maxLen
<
0
||
maxLen
<
minLen
{
return
""
}
src
:=
rand
.
NewSource
(
time
.
Now
()
.
UnixNano
())
n
:=
rand
.
Intn
(
maxLen
-
minLen
+
1
)
+
minLen
// choose actual length
b
:=
make
([]
byte
,
n
)
for
i
,
cache
,
remain
:=
n
-
1
,
src
.
Int63
(),
letterIdxMax
;
i
>=
0
;
{
if
remain
==
0
{
cache
,
remain
=
src
.
Int63
(),
letterIdxMax
n
:=
mathrand
.
Intn
(
maxLen
-
minLen
+
1
)
+
minLen
// choose actual length
// secureRandomBytes returns a number of bytes using crypto/rand.
secureRandomBytes
:=
func
(
numBytes
int
)
[]
byte
{
randomBytes
:=
make
([]
byte
,
numBytes
)
rand
.
Read
(
randomBytes
)
return
randomBytes
}
result
:=
make
([]
byte
,
n
)
bufferSize
:=
int
(
float64
(
n
)
*
1.3
)
for
i
,
j
,
randomBytes
:=
0
,
0
,
[]
byte
{};
i
<
n
;
j
++
{
if
j
%
bufferSize
==
0
{
randomBytes
=
secureRandomBytes
(
bufferSize
)
}
if
idx
:=
int
(
cache
&
letterIdxMask
);
idx
<
len
(
letterBytes
)
{
b
[
i
]
=
letterBytes
[
idx
]
i
--
if
idx
:=
int
(
randomBytes
[
j
%
n
]
&
letterIdxMask
);
idx
<
len
(
letterBytes
)
{
result
[
i
]
=
letterBytes
[
idx
]
i
++
}
cache
>>=
letterIdxBits
remain
--
}
return
string
(
b
)
return
string
(
result
)
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment