Created
May 18, 2021 21:06
-
-
Save SomajitDey/1f2056c0a1fbe4b45ee935fa45bd2a32 to your computer and use it in GitHub Desktop.
Basic PoW with deterministic output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Brief: Basic Proof-of-Work (PoW) with deterministic output for input string passed as parameter. | |
# Usage: ./pow <text> | |
# Ctrl+'\' (SIGQUIT) to check current nonce anytime | |
# Output: Nonce and 00* SHA256-Hash | |
#=================================================== | |
hash_of(){ | |
sha256sum <(echo "${1}") | awk '{print $1}' | |
}; export -f hash_of | |
trap 'echo Nonce: "${nonce}"' exit QUIT | |
input="${1}" | |
input_hash="$(hash_of "${input}")" | |
nonce=0 | |
until [[ "${final_hash}" == 00* ]];do | |
nonce_hash="$(hash_of "${nonce}")" | |
final_hash="$(hash_of "${input_hash}_${nonce_hash}")" | |
((nonce++)) | |
done | |
echo "Hash: ${final_hash}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment