Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-kbucket
Commits
71f3b420
Commit
71f3b420
authored
Jul 17, 2020
by
Steven Allen
Committed by
Adin Schmahmann
Jul 31, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use crypto/rand for generating random prefixes
This code isn't performance critical so we might as well be safe.
parent
ecd1ccdb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
2 deletions
+12
-2
table_refresh.go
table_refresh.go
+12
-2
No files found.
table_refresh.go
View file @
71f3b420
package
kbucket
import
(
"crypto/rand"
"encoding/binary"
"fmt"
"math/rand"
"time"
"github.com/libp2p/go-libp2p-core/peer"
...
...
@@ -34,6 +34,13 @@ func (rt *RoutingTable) GetTrackedCplsForRefresh() []time.Time {
return
cpls
}
func
randUint16
()
(
uint16
,
error
)
{
// Read a random prefix.
var
prefixBytes
[
2
]
byte
_
,
err
:=
rand
.
Read
(
prefixBytes
[
:
])
return
binary
.
BigEndian
.
Uint16
(
prefixBytes
[
:
]),
err
}
// GenRandPeerID generates a random peerID for a given Cpl
func
(
rt
*
RoutingTable
)
GenRandPeerID
(
targetCpl
uint
)
(
peer
.
ID
,
error
)
{
if
targetCpl
>
maxCplForRefresh
{
...
...
@@ -46,7 +53,10 @@ func (rt *RoutingTable) GenRandPeerID(targetCpl uint) (peer.ID, error) {
// Hence, to achieve a targetPrefix `T`, we must toggle the (T+1)th bit in L & then copy (T+1) bits from L
// to our randomly generated prefix.
toggledLocalPrefix
:=
localPrefix
^
(
uint16
(
0x8000
)
>>
targetCpl
)
randPrefix
:=
uint16
(
rand
.
Uint32
())
randPrefix
,
err
:=
randUint16
()
if
err
!=
nil
{
return
""
,
err
}
// Combine the toggled local prefix and the random bits at the correct offset
// such that ONLY the first `targetCpl` bits match the local ID.
...
...
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