from flask import Flask, request import hashlib, os, time app = Flask(__name__) DIFFICULTY = 20 challenge_store = {} def generate_challenge(): return os.urandom(12).hex() def check_difficulty(hash_bytes, bits): i = 0 while bits > 0: byte = hash_bytes[i] if bits >= 8: if byte != 0: return False bits -= 8 else: if byte & (0xFF << (8 - bits)) != 0: return False bits = 0 i += 1 return True @app.route('/', methods=['GET']) def index(): challenge = generate_challenge() challenge_store[challenge] = time.time() return f''' NOJSCAP demo

NOJSCAP demo

https://git.libroot.org/libroot/NOJSCAP/


If you don't already have the NOJSCAP client:

$ git clone https://git.libroot.org/libroot/NOJSCAP/ $ cd NOJSCAP/client/


$ python3 pow_client.py {challenge} {DIFFICULTY}

Go:

$ go run pow_client.go {challenge} {DIFFICULTY}

Node.js:

$ node pow_client.js {challenge} {DIFFICULTY}

Rust:

$ rustc pow_client.rs -o pow_client_rs $ pow_client_rs {challenge} {DIFFICULTY} Requires Rust and sha2 crate if using the Cargo version.

C:

$ gcc -O2 -o pow_client pow_client.c -lssl -lcrypto $ ./pow_client {challenge} {DIFFICULTY} Required: GCC or any C compiler. OpenSSL development libraries (libssl-dev on Debian-based systems)

''' @app.route('/', methods=['POST']) def submit(): challenge = request.form.get("challenge") nonce = request.form.get("nonce") if challenge not in challenge_store: return "Invalid challenge" challenge_store.pop(challenge) combined = (challenge + nonce).encode() h = hashlib.sha256(combined).digest() return "

Success! Valid nonce.

Try again" if check_difficulty(h, DIFFICULTY) else "

Invalid nonce.

Try again" app.run(port=3000)