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 secrets import SystemRandom
if TYPE_CHECKING:
from communicator import OnionrCommunicatorDaemon
from deadsimplekv import DeadSimpleKV
@ -82,7 +83,7 @@ def download_blocks_from_communicator(comm_inst: "OnionrCommunicatorDaemon"):
except onionrexceptions.OnlinePeerNeeded:
continue
else:
blockPeers = onionrcrypto.cryptoutils.random_shuffle(blockPeers)
SystemRandom().shuffle(blockPeers)
peerUsed = blockPeers.pop(0)
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 time import sleep
from threading import Thread
from secrets import SystemRandom
from . import sessionmanager
@ -14,7 +15,6 @@ from communicatorutils import proxypicker
import onionrexceptions
from onionrblocks import onionrblockapi as block
from onionrutils import stringvalidators, basicrequests
import onionrcrypto
from communicator import onlinepeers
if TYPE_CHECKING:
from deadsimplekv import DeadSimpleKV
@ -47,8 +47,8 @@ def upload_blocks_from_communicator(comm_inst: 'OnionrCommunicatorDaemon'):
sessionmanager.BlockUploadSessionManager)
tried_peers: UserID = []
finishedUploads = []
kv.put('blocksToUpload', onionrcrypto.cryptoutils.random_shuffle(
kv.get('blocksToUpload')))
SystemRandom().shuffle(kv.get('blocksToUpload'))
def remove_from_hidden(bl):
sleep(60)

View File

@ -3,10 +3,10 @@
Upload pool
"""
from typing import List
from secrets import SystemRandom
import onionrutils
import onionrtypes
from onionrcrypto import cryptoutils
"""
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
@ -62,7 +62,8 @@ class UploadPool:
"""Get the hash pool in secure random order."""
if len(self._pool) != self._pool_size:
raise PoolNotReady
final_pool: List[onionrtypes.BlockHash] = cryptoutils.random_shuffle(
final_pool: List[onionrtypes.BlockHash] = SystemRandom().shuffle(
list(self._pool))
self._pool.clear()

View File

@ -1,8 +1,7 @@
from . import safecompare, replayvalidation, randomshuffle, verifypow
from . import safecompare, replayvalidation, verifypow
from . import getpubfrompriv
replay_validator = replayvalidation.replay_timestamp_validation
random_shuffle = randomshuffle.random_shuffle
safe_compare = safecompare.safe_compare
verify_POW = verifypow.verify_POW
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
"""
import os
from secrets import SystemRandom
import logger
from onionrutils import epoch
from onionrcrypto.cryptoutils.randomshuffle import random_shuffle
from . import uicheck, inserttest, stresstest
from . import ownnode
@ -55,7 +55,7 @@ class OnionrRunTestManager:
self.run_date: int = 0
def run_tests(self):
tests = random_shuffle(RUN_TESTS)
tests = SystemRandom.shuffle(list(RUN_TESTS))
cur_time = epoch.get_epoch()
logger.info(f"Doing runtime tests at {cur_time}")

View File

@ -2,9 +2,10 @@
NetUtils offers various useful functions to Onionr networking.
"""
from random import SystemRandom
from onionrutils import basicrequests
from .readstatic import read_static
from onionrcrypto.cryptoutils import random_shuffle
"""
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
@ -26,8 +27,7 @@ def check_network(torPort=0) -> bool:
success = False
connect_urls = []
try:
connect_urls = random_shuffle(
read_static('connect-check.txt').split(','))
connect_urls = SystemRandom().shuffle(read_static('connect-check.txt').split(','))
for url in connect_urls:
if basicrequests.do_get_request(

View File

@ -64,13 +64,6 @@ class OnionrCryptoTests(unittest.TestCase):
else:
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):
keyPair = crypto.generate()
keyPair2 = crypto.generate()