generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
346 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package keyutil | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/binary" | ||
"strconv" | ||
|
||
"github.com/plprobelab/go-kademlia/key" | ||
) | ||
|
||
// Random returns a KadKey of length l populated with random data. | ||
func Random(l int) key.KadKey { | ||
buf := make([]byte, l) | ||
if _, err := rand.Read(buf); err != nil { | ||
panic("RandomWithPrefix: failed to read enough entropy for key") | ||
} | ||
return buf | ||
} | ||
|
||
// RandomWithPrefix returns a KadKey of length l having a prefix equal to the bit pattern held in s. | ||
func RandomWithPrefix(s string, l int) key.KadKey { | ||
kk := Random(l) | ||
if s == "" { | ||
return kk | ||
} | ||
|
||
bits := len(s) | ||
if bits > 64 { | ||
panic("RandomWithPrefix: prefix too long") | ||
} | ||
n, err := strconv.ParseInt(s, 2, 64) | ||
if err != nil { | ||
panic("RandomWithPrefix: " + err.Error()) | ||
} | ||
prefix := uint64(n) << (64 - bits) | ||
|
||
size := l | ||
if size < 8 { | ||
size = 8 | ||
} | ||
|
||
buf := make([]byte, size) | ||
if _, err := rand.Read(buf); err != nil { | ||
panic("RandomWithPrefix: failed to read enough entropy for key") | ||
} | ||
|
||
lead := binary.BigEndian.Uint64(buf) | ||
lead <<= bits | ||
lead >>= bits | ||
lead |= prefix | ||
binary.BigEndian.PutUint64(buf, lead) | ||
return key.KadKey(buf[:l]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# Trie | ||
|
||
This package contains an implementation of a XOR Trie. | ||
|
||
The trie is to be treated as immutable. Mutator functions such as Add and Remove return copies of the trie. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.