-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rando: refactor random generation of keys and hashes (#117)
* Refactor random generation of keys and hashes. * Add a unit test for rando.checkSeed.
- Loading branch information
Showing
11 changed files
with
208 additions
and
82 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
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,17 +1,87 @@ | ||
""" | ||
Copyright (c) 2019, Brian Stafford | ||
Copyright (c) 2019, The Decred developers | ||
Copyright (c) 2019-20, The Decred developers | ||
See LICENSE for details | ||
""" | ||
|
||
import os | ||
|
||
from decred import DecredError | ||
from decred.util.encode import ByteArray | ||
|
||
|
||
KEY_SIZE = 32 | ||
HASH_SIZE = 32 | ||
|
||
MinSeedBytes = 16 # 128 bits | ||
MaxSeedBytes = 64 # 512 bits | ||
|
||
|
||
def generateSeed(length=MaxSeedBytes): | ||
def checkSeedLength(length): | ||
""" | ||
Check that seed length is correct. | ||
Args: | ||
length int: the seed length to be checked. | ||
Raises: | ||
DecredError if length is not between MinSeedBytes and MaxSeedBytes | ||
included. | ||
""" | ||
if length < MinSeedBytes or length > MaxSeedBytes: | ||
raise AssertionError("invalid seed length %d" % length) | ||
raise DecredError(f"Invalid seed length {length}") | ||
|
||
|
||
def generateSeed(length=MaxSeedBytes): | ||
""" | ||
Generate a cryptographically-strong random seed. | ||
Returns: | ||
bytes: a random bytes object of the given length. | ||
Raises: | ||
DecredError if length is not between MinSeedBytes and MaxSeedBytes | ||
included. | ||
""" | ||
checkSeedLength(length) | ||
return os.urandom(length) | ||
|
||
|
||
def newHashRaw(): | ||
""" | ||
Generate a random hash of HASH_SIZE length. | ||
Returns: | ||
bytes: a random object of HASH_SIZE length. | ||
""" | ||
return generateSeed(HASH_SIZE) | ||
|
||
|
||
def newHash(): | ||
""" | ||
Generate a wrapped random hash of HASH_SIZE length. | ||
Returns: | ||
ByteArray: a random object of HASH_SIZE length. | ||
""" | ||
return ByteArray(newHashRaw()) | ||
|
||
|
||
def newKeyRaw(): | ||
""" | ||
Generate a random key of KEY_SIZE length. | ||
Returns: | ||
bytes: a random object of KEY_SIZE length. | ||
""" | ||
return generateSeed(KEY_SIZE) | ||
|
||
|
||
def newKey(): | ||
""" | ||
Generate a wrapped random key of KEY_SIZE length. | ||
Returns: | ||
ByteArray: a random object of KEY_SIZE length. | ||
""" | ||
return ByteArray(newKeyRaw()) |
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
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
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
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
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,20 @@ | ||
""" | ||
Copyright (c) 2020, The Decred developers | ||
See LICENSE for details | ||
""" | ||
|
||
import pytest | ||
|
||
from decred import DecredError | ||
from decred.crypto import rando | ||
|
||
|
||
def test_checkSeedLength(): | ||
with pytest.raises(DecredError): | ||
rando.checkSeedLength(rando.MinSeedBytes - 1) | ||
assert rando.checkSeedLength(rando.MinSeedBytes) is None | ||
assert rando.checkSeedLength(rando.HASH_SIZE) is None | ||
assert rando.checkSeedLength(rando.KEY_SIZE) is None | ||
assert rando.checkSeedLength(rando.MaxSeedBytes) is None | ||
with pytest.raises(DecredError): | ||
rando.checkSeedLength(rando.MaxSeedBytes + 1) |
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.