Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
Socket Programming-Threads
import threading
import socket
import pickle
import hashlib
# from ecdsa import SigningKey
import time
import hashlib
import json
import uuid
NANOSECONDS = 1
MICROSECONDS = 1000 * NANOSECONDS
MILLISECONDS = 1000 * MICROSECONDS
SECONDS = 1000 * MILLISECONDS
MINE_RATE = 4 * SECONDS
STARTING_BALANCE = 1000
MINING_REWARD = 50
MINING_REWARD_INPUT = { 'address': '*mining-reward*' }
'''CRYPTO'''
def crypto_hash(*args):
"""
Return a sha-256 hash of the given arguments.
"""
stringified_args = sorted(map(lambda data: json.dumps(data), args))
joined_data = ''.join(stringified_args)
return hashlib.sha256(joined_data.encode('utf-8')).hexdigest()
def main():
print(f"crypto_hash('one', 2, [3]): {crypto_hash('one', 2, [3])}")
print(f"crypto_hash(2, 'one', [3]): {crypto_hash(2, 'one', [3])}")
if __name__ == '__main__':
main()
crypto_hash('one', 2, [3]): 49d135ee795768472bd5f9e5d11c3982e6bdeae55bc86a0f4351654e3d4e8b2a
crypto_hash(2, 'one', [3]): 49d135ee795768472bd5f9e5d11c3982e6bdeae55bc86a0f4351654e3d4e8b2a
HEX_TO_BINARY_CONVERSION_TABLE = {
'0': '0000',
'1': '0001',
'2': '0010',
'3': '0011',
'4': '0100',
'5': '0101',
'6': '0110',
'7': '0111',
'8': '1000',
'9': '1001',
'a': '1010',
'b': '1011',
'c': '1100',
'd': '1101',
'e': '1110',
'f': '1111'
}
def hex_to_binary(hex_string):
binary_string = ''
for character in hex_string:
binary_string += HEX_TO_BINARY_CONVERSION_TABLE[character]
return binary_string
def main():
number = 451
hex_number = hex(number)[2:]
print(f'hex_number: {hex_number}')
binary_number = hex_to_binary(hex_number)
print(f'binary_number: {binary_number}')
original_number = int(binary_number, 2)
print(f'original_number: {original_number}')
hex_to_binary_crypto_hash = hex_to_binary(crypto_hash('test-data'))
print(f'hex_to_binary_crypto_hash: {hex_to_binary_crypto_hash}')
if __name__ == '__main__':
main()
hex_number: 1c3
binary_number: 000111000011
original_number: 451
hex_to_binary_crypto_hash: 1000011110000111100110000100010011000000001010110100001011001011111110101111110000101001100110010001101001110111011001010110010111111001101101101011011100001001101101100101101011110011111100101001000111011011000010101001000000000000010011001100111110101011