I want to create a script that will take a string from a html form, add it to a txt file stored online, then compute sha256 from the sum and check if the hash starts from two zeros. I wrote a simple program, but don't know if I'm doing it right. I can't find any mistakes.
<form onsubmit= check()>
<label for="try">Write your answer.</label><br>
<input type="text" id="try" name="try"><br>
<input type="submit" value="Send">
</form>
<p id="return"></p>
<script>
function read(textFile){
var xhr=new XMLHttpRequest;
xhr.open('GET',textFile);
xhr.onload = function(){
var blockchain = xhr.response;
}
xhr.send()
}
function check(){
read('blockchain.txt');
document.getElementById("try") = new string(input);
var whole = blockchain + input;
var hash = sha256(whole);
if(hash.substring(2)=='00'){
document.getElementById("return") = "congratulations!";
}
else{
document.getElementById("return") = "Try again.";
}
}
async function sha256(message) {
const msgBuffer = new TextEncoder('utf-8').encode(message);
const hashBuffer = await window.crypto.subtle.digest('SHA-256', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join('');
console.log(hashHex);
return hashHex;
}
</script>