Updated proof_of_work.sh with input validation and improvements

main
TiredFromTelehack 2024-10-18 01:50:35 +02:00
parent 774d2a584b
commit f36b63c30c
1 changed files with 28 additions and 8 deletions

30
proof_of_work.sh 100644 → 100755
View File

@ -1,26 +1,45 @@
#!/bin/bash #!/bin/bash
# Check if required utilities are installed
for utils in argon2 xxd bc; do for utils in argon2 xxd bc; do
if ! command -v $utils &> /dev/null; then if ! command -v $utils &> /dev/null; then
echo "$utils is not installed. Please install it and try again." echo "$utils is not installed. Please install it and try again."
exit 1 exit 1
fi fi
done done
# Read the challenge code from argument or prompt user
if [ $# -lt 1 ]; then if [ $# -lt 1 ]; then
read -p "Enter Challenge Code: " challenge read -p "Enter Challenge Code: " challenge
else else
challenge=$1 challenge=$1
fi fi
# Trim whitespace
challenge=$(echo "$challenge" | xargs) challenge=$(echo "$challenge" | xargs)
IFS=':' read -r _ memory_cost time_cost salt difficulty <<< "$challenge" # Validate the challenge format
if ! [[ $challenge =~ ^([0-9]+):([0-9]+):([A-Za-z0-9]+):([0-9]+)$ ]]; then
echo "Invalid challenge format. Expected format: memory_cost:time_cost:salt:difficulty"
exit 2
fi
# Parse challenge code
IFS=':' read -r memory_cost time_cost salt difficulty <<< "$challenge"
# Trim whitespace
memory_cost=$(echo "$memory_cost" | xargs) memory_cost=$(echo "$memory_cost" | xargs)
time_cost=$(echo "$time_cost" | xargs) time_cost=$(echo "$time_cost" | xargs)
salt=$(echo "$salt" | xargs)
difficulty=$(echo "$difficulty" | xargs) difficulty=$(echo "$difficulty" | xargs)
# Debugging output
echo "Memory Cost: $memory_cost"
echo "Time Cost: $time_cost"
echo "Salt: $salt"
echo "Difficulty: $difficulty"
# Generate prefix for the password
pw_prefix="UNBLOCK-$(head /dev/urandom | tr -dc A-Z0-9 | head -c 8)-" pw_prefix="UNBLOCK-$(head /dev/urandom | tr -dc A-Z0-9 | head -c 8)-"
difficulty_raw=$(echo "scale=10; e(l(256) * (4 - l($difficulty) / l(256))) / 1" | bc -l | xargs printf %.0f) difficulty_raw=$(echo "scale=10; e(l(256) * (4 - l($difficulty) / l(256))) / 1" | bc -l | xargs printf %.0f)
@ -29,18 +48,19 @@ echo "Time Cost: $time_cost"
echo echo
n=1 n=1
start_time=$(date +%s) start_time=$(date +%s)
# Function to display elapsed time
elapsed_time() { elapsed_time() {
current_time=$(date +%s) current_time=$(date +%s)
elapsed_time=$((current_time - start_time)) elapsed_time=$((current_time - start_time))
echo -ne "\rElapsed Time: $elapsed_time seconds." echo -ne "\rElapsed Time: $elapsed_time seconds."
} }
# Main loop to find the solution
while true; do while true; do
pw="$pw_prefix$n" pw="$pw_prefix$n"
hash=$(echo -n "$pw" | argon2 "$salt" -t "$time_cost" -k "$memory_cost" -p 1 -id -v 13 -r) hash=$(echo -n "$pw" | argon2 "$salt" -t "$time_cost" -k "$memory_cost" -p 1 -id -v 13 -r)
hash_bytes=${hash:0:8} hash_bytes=${hash:0:8}
if [ $((16#$hash_bytes)) -lt "$difficulty_raw" ]; then if [ $((16#$hash_bytes)) -lt "$difficulty_raw" ]; then
@ -49,7 +69,7 @@ while true; do
echo "Your unblock code is: $pw" echo "Your unblock code is: $pw"
echo "This is the code you enter into the site to pass the challenge." echo "This is the code you enter into the site to pass the challenge."
echo echo
break exit 0
else else
elapsed_time elapsed_time
n=$((n + 1)) n=$((n + 1))