Added libs
This commit is contained in:
67
fspn/utils/sha256_util.py
Normal file
67
fspn/utils/sha256_util.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import hashlib
|
||||
|
||||
def hash_file(filepath):
|
||||
BUF_SIZE = 65 * 1024
|
||||
|
||||
sha = hashlib.sha256()
|
||||
|
||||
with open(filepath, 'rb') as f:
|
||||
while True:
|
||||
data = f.read(BUF_SIZE)
|
||||
if not data:
|
||||
break
|
||||
sha.update(data)
|
||||
return sha.hexdigest()
|
||||
|
||||
def hash_bytes(data):
|
||||
sha = hashlib.sha256()
|
||||
sha.update(data)
|
||||
return sha.hexdigest()
|
||||
|
||||
def hash_string(data:str):
|
||||
sha = hashlib.sha256()
|
||||
sha.update(str(data).encode('utf-8'))
|
||||
return sha.hexdigest()
|
||||
|
||||
# ---------------
|
||||
import datetime
|
||||
import string
|
||||
import itertools
|
||||
import random
|
||||
|
||||
def get_nonce(characters, lenght):
|
||||
yield from itertools.product(*([characters] * lenght))
|
||||
|
||||
def count_leading_zeros(text):
|
||||
n = 0
|
||||
for i in range(len(text)):
|
||||
if text[:i] == "0" * i:
|
||||
n = i
|
||||
else:
|
||||
break
|
||||
return n
|
||||
|
||||
def mine_user(data, force=4, nonce_lenght=4):
|
||||
characters = '[@_!#$%^&*()<>?/\|}{~:]'+string.ascii_letters+string.digits
|
||||
while True:
|
||||
for x in get_nonce(characters, nonce_lenght):
|
||||
nonce = ''.join(x)+random.choice(characters)
|
||||
hash = hash_string(data+ nonce)
|
||||
leading_zeros = count_leading_zeros(hash)
|
||||
if leading_zeros >= force:
|
||||
return nonce
|
||||
|
||||
|
||||
def test():
|
||||
print(datetime.datetime.now(), "EXECUTING HASH TEST - MINE USER")
|
||||
public_key_str = "A4DZSk+TlR+4w39MbiIAQbti+N0H1QlJEhRH2DI6Iubj"
|
||||
nonce, hash = mine_user(public_key_str)
|
||||
|
||||
print(nonce, hash)
|
||||
|
||||
# test()
|
||||
|
||||
# A4DZSk+TlR+4w39MbiIAQbti+N0H1QlJEhRH2DI6Iubj
|
||||
# 8
|
||||
# eYnU*@
|
||||
# [/Q#7r
|
||||
Reference in New Issue
Block a user