remove random shuffle function for systemrandom equivalent

master
Kevin Froman 2020-10-21 09:46:05 +00:00
parent fb85856f76
commit 5dddeb3f10
8 changed files with 28 additions and 40 deletions

View File

@ -1,9 +1,10 @@
""" """Onionr - Private P2P Communication.
Onionr - Private P2P Communication
Download blocks using the communicator instance Download blocks using the communicator instance.
""" """
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from secrets import SystemRandom
if TYPE_CHECKING: if TYPE_CHECKING:
from communicator import OnionrCommunicatorDaemon from communicator import OnionrCommunicatorDaemon
from deadsimplekv import DeadSimpleKV from deadsimplekv import DeadSimpleKV
@ -82,7 +83,7 @@ def download_blocks_from_communicator(comm_inst: "OnionrCommunicatorDaemon"):
except onionrexceptions.OnlinePeerNeeded: except onionrexceptions.OnlinePeerNeeded:
continue continue
else: else:
blockPeers = onionrcrypto.cryptoutils.random_shuffle(blockPeers) SystemRandom().shuffle(blockPeers)
peerUsed = blockPeers.pop(0) peerUsed = blockPeers.pop(0)
if not kv.get('shutdown') and peerUsed.strip() != '': if not kv.get('shutdown') and peerUsed.strip() != '':

View File

@ -5,6 +5,7 @@ Upload blocks in the upload queue to peers from the communicator
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from time import sleep from time import sleep
from threading import Thread from threading import Thread
from secrets import SystemRandom
from . import sessionmanager from . import sessionmanager
@ -14,7 +15,6 @@ from communicatorutils import proxypicker
import onionrexceptions import onionrexceptions
from onionrblocks import onionrblockapi as block from onionrblocks import onionrblockapi as block
from onionrutils import stringvalidators, basicrequests from onionrutils import stringvalidators, basicrequests
import onionrcrypto
from communicator import onlinepeers from communicator import onlinepeers
if TYPE_CHECKING: if TYPE_CHECKING:
from deadsimplekv import DeadSimpleKV from deadsimplekv import DeadSimpleKV
@ -47,8 +47,8 @@ def upload_blocks_from_communicator(comm_inst: 'OnionrCommunicatorDaemon'):
sessionmanager.BlockUploadSessionManager) sessionmanager.BlockUploadSessionManager)
tried_peers: UserID = [] tried_peers: UserID = []
finishedUploads = [] finishedUploads = []
kv.put('blocksToUpload', onionrcrypto.cryptoutils.random_shuffle(
kv.get('blocksToUpload'))) SystemRandom().shuffle(kv.get('blocksToUpload'))
def remove_from_hidden(bl): def remove_from_hidden(bl):
sleep(60) sleep(60)

View File

@ -3,10 +3,10 @@
Upload pool Upload pool
""" """
from typing import List from typing import List
from secrets import SystemRandom
import onionrutils import onionrutils
import onionrtypes import onionrtypes
from onionrcrypto import cryptoutils
""" """
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -62,7 +62,8 @@ class UploadPool:
"""Get the hash pool in secure random order.""" """Get the hash pool in secure random order."""
if len(self._pool) != self._pool_size: if len(self._pool) != self._pool_size:
raise PoolNotReady raise PoolNotReady
final_pool: List[onionrtypes.BlockHash] = cryptoutils.random_shuffle(
final_pool: List[onionrtypes.BlockHash] = SystemRandom().shuffle(
list(self._pool)) list(self._pool))
self._pool.clear() self._pool.clear()

View File

@ -1,8 +1,7 @@
from . import safecompare, replayvalidation, randomshuffle, verifypow from . import safecompare, replayvalidation, verifypow
from . import getpubfrompriv from . import getpubfrompriv
replay_validator = replayvalidation.replay_timestamp_validation replay_validator = replayvalidation.replay_timestamp_validation
random_shuffle = randomshuffle.random_shuffle
safe_compare = safecompare.safe_compare safe_compare = safecompare.safe_compare
verify_POW = verifypow.verify_POW verify_POW = verifypow.verify_POW
get_pub_key_from_priv = getpubfrompriv.get_pub_key_from_priv get_pub_key_from_priv = getpubfrompriv.get_pub_key_from_priv

View File

@ -1,6 +0,0 @@
from random import SystemRandom
def random_shuffle(theList):
myList = list(theList)
SystemRandom().shuffle(myList)
return myList

View File

@ -3,10 +3,10 @@
Test Onionr as it is running Test Onionr as it is running
""" """
import os import os
from secrets import SystemRandom
import logger import logger
from onionrutils import epoch from onionrutils import epoch
from onionrcrypto.cryptoutils.randomshuffle import random_shuffle
from . import uicheck, inserttest, stresstest from . import uicheck, inserttest, stresstest
from . import ownnode from . import ownnode
@ -55,7 +55,7 @@ class OnionrRunTestManager:
self.run_date: int = 0 self.run_date: int = 0
def run_tests(self): def run_tests(self):
tests = random_shuffle(RUN_TESTS) tests = SystemRandom.shuffle(list(RUN_TESTS))
cur_time = epoch.get_epoch() cur_time = epoch.get_epoch()
logger.info(f"Doing runtime tests at {cur_time}") logger.info(f"Doing runtime tests at {cur_time}")

View File

@ -2,9 +2,10 @@
NetUtils offers various useful functions to Onionr networking. NetUtils offers various useful functions to Onionr networking.
""" """
from random import SystemRandom
from onionrutils import basicrequests from onionrutils import basicrequests
from .readstatic import read_static from .readstatic import read_static
from onionrcrypto.cryptoutils import random_shuffle
""" """
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -26,8 +27,7 @@ def check_network(torPort=0) -> bool:
success = False success = False
connect_urls = [] connect_urls = []
try: try:
connect_urls = random_shuffle( connect_urls = SystemRandom().shuffle(read_static('connect-check.txt').split(','))
read_static('connect-check.txt').split(','))
for url in connect_urls: for url in connect_urls:
if basicrequests.do_get_request( if basicrequests.do_get_request(

View File

@ -14,7 +14,7 @@ from onionrutils import stringvalidators, mnemonickeys
import onionrcrypto as crypto, onionrexceptions import onionrcrypto as crypto, onionrexceptions
class OnionrCryptoTests(unittest.TestCase): class OnionrCryptoTests(unittest.TestCase):
def test_blake2b(self): def test_blake2b(self):
self.assertEqual(crypto.hashers.blake2b_hash('test'), crypto.hashers.blake2b_hash(b'test')) self.assertEqual(crypto.hashers.blake2b_hash('test'), crypto.hashers.blake2b_hash(b'test'))
self.assertEqual(crypto.hashers.blake2b_hash(b'test'), crypto.hashers.blake2b_hash(b'test')) self.assertEqual(crypto.hashers.blake2b_hash(b'test'), crypto.hashers.blake2b_hash(b'test'))
@ -26,9 +26,9 @@ class OnionrCryptoTests(unittest.TestCase):
pass pass
else: else:
self.assertTrue(False) self.assertTrue(False)
self.assertEqual(nacl.hash.blake2b(b'test'), crypto.hashers.blake2b_hash(b'test')) self.assertEqual(nacl.hash.blake2b(b'test'), crypto.hashers.blake2b_hash(b'test'))
def test_sha3256(self): def test_sha3256(self):
hasher = hashlib.sha3_256() hasher = hashlib.sha3_256()
self.assertEqual(crypto.hashers.sha3_hash('test'), crypto.hashers.sha3_hash(b'test')) self.assertEqual(crypto.hashers.sha3_hash('test'), crypto.hashers.sha3_hash(b'test'))
@ -41,18 +41,18 @@ class OnionrCryptoTests(unittest.TestCase):
pass pass
else: else:
self.assertTrue(False) self.assertTrue(False)
hasher.update(b'test') hasher.update(b'test')
normal = hasher.hexdigest() normal = hasher.hexdigest()
self.assertEqual(crypto.hashers.sha3_hash(b'test'), normal) self.assertEqual(crypto.hashers.sha3_hash(b'test'), normal)
def valid_default_id(self): def valid_default_id(self):
self.assertTrue(stringvalidators.validate_pub_key(crypto.pub_key)) self.assertTrue(stringvalidators.validate_pub_key(crypto.pub_key))
def test_human_readable_length(self): def test_human_readable_length(self):
human = mnemonickeys.get_human_readable_ID() human = mnemonickeys.get_human_readable_ID()
self.assertTrue(len(human.split('-')) == 16) self.assertTrue(len(human.split('-')) == 16)
def test_safe_compare(self): def test_safe_compare(self):
self.assertTrue(crypto.cryptoutils.safe_compare('test', 'test')) self.assertTrue(crypto.cryptoutils.safe_compare('test', 'test'))
self.assertTrue(crypto.cryptoutils.safe_compare('test', b'test')) self.assertTrue(crypto.cryptoutils.safe_compare('test', b'test'))
@ -63,13 +63,6 @@ class OnionrCryptoTests(unittest.TestCase):
pass pass
else: else:
self.assertTrue(False) self.assertTrue(False)
def test_random_shuffle(self):
# Small chance that the randomized list will be same. Rerun test a couple times if it fails
startList = ['cat', 'dog', 'moose', 'rabbit', 'monkey', 'crab', 'human', 'dolphin', 'whale', 'etc'] * 10
self.assertNotEqual(startList, list(crypto.cryptoutils.random_shuffle(startList)))
self.assertTrue(len(list(crypto.cryptoutils.random_shuffle(startList))) == len(startList))
def test_asymmetric(self): def test_asymmetric(self):
keyPair = crypto.generate() keyPair = crypto.generate()
@ -87,12 +80,12 @@ class OnionrCryptoTests(unittest.TestCase):
pass pass
else: else:
self.assertTrue(False) self.assertTrue(False)
blankMessage = crypto.encryption.pub_key_encrypt('', keyPair2[0]) blankMessage = crypto.encryption.pub_key_encrypt('', keyPair2[0])
self.assertTrue('' == crypto.encryption.pub_key_decrypt(blankMessage, privkey=keyPair2[1], encodedData=False).decode()) self.assertTrue('' == crypto.encryption.pub_key_decrypt(blankMessage, privkey=keyPair2[1], encodedData=False).decode())
# Try to encrypt arbitrary bytes # Try to encrypt arbitrary bytes
crypto.encryption.pub_key_encrypt(os.urandom(32), keyPair2[0]) crypto.encryption.pub_key_encrypt(os.urandom(32), keyPair2[0])
def test_pub_from_priv(self): def test_pub_from_priv(self):
priv = nacl.signing.SigningKey.generate().encode(encoder=nacl.encoding.Base32Encoder) priv = nacl.signing.SigningKey.generate().encode(encoder=nacl.encoding.Base32Encoder)
pub = crypto.cryptoutils.getpubfrompriv.get_pub_key_from_priv(priv) pub = crypto.cryptoutils.getpubfrompriv.get_pub_key_from_priv(priv)
@ -114,9 +107,9 @@ class OnionrCryptoTests(unittest.TestCase):
pass pass
else: else:
self.assertFalse(True) self.assertFalse(True)
gen = crypto.generate_deterministic('weakpassword', bypassCheck=True) gen = crypto.generate_deterministic('weakpassword', bypassCheck=True)
password = base64.b64encode(os.urandom(32)) password = base64.b64encode(os.urandom(32))
gen1 = crypto.generate_deterministic(password) gen1 = crypto.generate_deterministic(password)
gen2 = crypto.generate_deterministic(password) gen2 = crypto.generate_deterministic(password)