Added Python script for generating Proof of Work code

main
TiredFromTelehack 2024-10-18 02:19:39 +02:00
parent 327d20cbfc
commit 807b252558
2 changed files with 23 additions and 38 deletions

View File

@ -1,38 +0,0 @@
import hashlib
import random
import time
def generate_salt(length=16):
"""Generates a random salt."""
return ''.join(random.choices('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=length))
def proof_of_work(difficulty, nonce_limit, salt):
"""Generates a proof of work."""
for nonce in range(nonce_limit):
# Combine salt and nonce to create a unique input
input_str = f"{salt}:{nonce}"
# Calculate the SHA-256 hash
hash_result = hashlib.sha256(input_str.encode()).hexdigest()
# Check if the hash meets the difficulty
if hash_result.startswith('0' * difficulty):
return nonce, hash_result
return None, None
def generate_proof_of_work(difficulty=6, nonce_limit=10000):
"""Generates a proof of work string."""
salt = generate_salt()
nonce, hash_result = proof_of_work(difficulty, nonce_limit, salt)
if nonce is not None:
# Format the proof of work
proof_of_work_string = f"{nonce}:{1}:{salt}:{nonce}"
return proof_of_work_string
else:
return "No valid proof of work found"
# Example usage
if __name__ == "__main__":
difficulty = 6 # Adjust the difficulty level (number of leading zeros)
nonce_limit = 10000 # Set the maximum nonce to test
pow_string = generate_proof_of_work(difficulty, nonce_limit)
print(pow_string)

23
pow_generator.py 100644
View File

@ -0,0 +1,23 @@
import os
import base64
import random
def generate_salt(length=16):
"""Generate a URL-safe base64-encoded random salt."""
salt = os.urandom(length) # Generate random bytes
return "SALTE" + base64.urlsafe_b64encode(salt).decode('utf-8')[:12] # Prefix "SALTE" and limit the length
def generate_pow_code():
"""Generate a proof of work challenge code."""
memory_cost = 262144 # Example memory cost (64 MB)
time_cost = 1 # Example time cost
salt = generate_salt() # Generate the salt
difficulty = random.randint(1000, 2000) # Random difficulty between 1000 and 2000
# Format the proof of work code
pow_code = f"{memory_cost}:{time_cost}:{salt}:{difficulty}"
return pow_code
if __name__ == "__main__":
pow_code = generate_pow_code()
print("Generated Proof of Work Code:", pow_code)