renamed onionr dir and bugfixes/linting progress
This commit is contained in:
parent
2b996da17f
commit
720efe4fca
226 changed files with 179 additions and 142 deletions
8
src/onionrcrypto/cryptoutils/__init__.py
Normal file
8
src/onionrcrypto/cryptoutils/__init__.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
from . import safecompare, replayvalidation, randomshuffle, 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
|
6
src/onionrcrypto/cryptoutils/getpubfrompriv.py
Normal file
6
src/onionrcrypto/cryptoutils/getpubfrompriv.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from nacl import signing, encoding
|
||||
|
||||
from onionrtypes import UserID, UserIDSecretKey
|
||||
|
||||
def get_pub_key_from_priv(priv_key: UserIDSecretKey, raw_encoding:bool=False)->UserID:
|
||||
return signing.SigningKey(priv_key, encoder=encoding.Base32Encoder).verify_key.encode(encoding.Base32Encoder)
|
13
src/onionrcrypto/cryptoutils/randomshuffle.py
Normal file
13
src/onionrcrypto/cryptoutils/randomshuffle.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
import secrets
|
||||
def random_shuffle(theList):
|
||||
myList = list(theList)
|
||||
shuffledList = []
|
||||
myListLength = len(myList) + 1
|
||||
while myListLength > 0:
|
||||
removed = secrets.randbelow(myListLength)
|
||||
try:
|
||||
shuffledList.append(myList.pop(removed))
|
||||
except IndexError:
|
||||
pass
|
||||
myListLength = len(myList)
|
||||
return shuffledList
|
6
src/onionrcrypto/cryptoutils/replayvalidation.py
Normal file
6
src/onionrcrypto/cryptoutils/replayvalidation.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from onionrutils import epoch
|
||||
def replay_timestamp_validation(timestamp):
|
||||
if epoch.get_epoch() - int(timestamp) > 2419200:
|
||||
return False
|
||||
else:
|
||||
return True
|
12
src/onionrcrypto/cryptoutils/safecompare.py
Normal file
12
src/onionrcrypto/cryptoutils/safecompare.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
import hmac
|
||||
def safe_compare(one, two):
|
||||
# Do encode here to avoid spawning core
|
||||
try:
|
||||
one = one.encode()
|
||||
except AttributeError:
|
||||
pass
|
||||
try:
|
||||
two = two.encode()
|
||||
except AttributeError:
|
||||
pass
|
||||
return hmac.compare_digest(one, two)
|
38
src/onionrcrypto/cryptoutils/verifypow.py
Normal file
38
src/onionrcrypto/cryptoutils/verifypow.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
from .. import hashers
|
||||
import config, onionrproofs, logger
|
||||
import onionrexceptions
|
||||
def verify_POW(blockContent):
|
||||
'''
|
||||
Verifies the proof of work associated with a block
|
||||
'''
|
||||
retData = False
|
||||
|
||||
dataLen = len(blockContent)
|
||||
|
||||
try:
|
||||
blockContent = blockContent.encode()
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
blockHash = hashers.sha3_hash(blockContent)
|
||||
try:
|
||||
blockHash = blockHash.decode() # bytes on some versions for some reason
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
difficulty = onionrproofs.getDifficultyForNewBlock(blockContent)
|
||||
|
||||
if difficulty < int(config.get('general.minimum_block_pow')):
|
||||
difficulty = int(config.get('general.minimum_block_pow'))
|
||||
mainHash = '0000000000000000000000000000000000000000000000000000000000000000'#nacl.hash.blake2b(nacl.utils.random()).decode()
|
||||
puzzle = mainHash[:difficulty]
|
||||
|
||||
if blockHash[:difficulty] == puzzle:
|
||||
# logger.debug('Validated block pow')
|
||||
retData = True
|
||||
else:
|
||||
logger.debug("Invalid token, bad proof")
|
||||
raise onionrexceptions.InvalidProof('Proof for %s needs to be %s' % (blockHash, puzzle))
|
||||
|
||||
return retData
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue