Undo reconstructhash changes, but with pep-8

This commit is contained in:
Kevin Froman 2019-12-01 02:07:41 -06:00
parent 9b2aa63846
commit 68ead2ae85
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B

View File

@ -1,8 +1,10 @@
''' '''
Onionr - Private P2P Communication Onionr - Private P2P Communication
z-fill (zero fill) a string to a specific length, intended for reconstructing block hashes z-fill (zero fill) a string to a specific length
intended for reconstructing block hashes
''' '''
from typing import Union
''' '''
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
@ -17,10 +19,17 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
def reconstruct_hash(hex_hash, length=64):
def reconstruct_hash(hex_hash: Union[str, bytes],
length: int = 64) -> Union[str, bytes]:
"""Pad hash hex string with zeros, return result"""
return hex_hash.zfill(length) return hex_hash.zfill(length)
def deconstruct_hash(hex_hash):
def deconstruct_hash(hex_hash: Union[str, bytes]) -> Union[str, bytes]:
"""Remove leading zeros from hex hash, return result"""
new_hash = ''
ret_bytes = False ret_bytes = False
try: try:
hex_hash = hex_hash.decode() hex_hash = hex_hash.decode()
@ -28,10 +37,15 @@ def deconstruct_hash(hex_hash):
except AttributeError: except AttributeError:
pass pass
num_zeroes = hex_hash.count('0') c = 0
new_hash = hex_hash[num_zeroes:] for x in hex_hash:
if x == '0':
c += 1
else:
break
new_hash = hex_hash[c:]
if ret_bytes: if ret_bytes:
new_hash = new_hash.encode()
new_hash = new_hash.encode()
return new_hash return new_hash