When calculating the SHA256,is `00' not the first value to achieve if it is formulated correctly or I'm I wrong?
It's not necessarily the first value, but if you want it to be the first value (or the first two characters, call it whatever you want) it's 1 in 16^2.
Since random numbers are picked, (which automatically brings in probability) what the prob that a number is picked at random and it produces an `00'.?
So the odds of having a "00" appeared in your hash, in any position? Definitely less than 1 in 16^2.
You have 1:16^2 chances to get:
00xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
And 1:16^2 chances to get:
x00xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
And it goes on, and on.
I'm not a mathematician, but let's say we want to find the chances of getting either of the first two cases. I presume that would be a 2:16^2, or 1 in (16^2)/2, which means 1 in 128.
To make sure it's correct, I'll calculate thousands of hashes and see how frequent the appearance of 00x... and x00... is.
I just wrote this small node.js code that does what I said:
const crypto = require('crypto'),
hash = crypto.getHashes();
var times = 0;
for(var i=1; i<128001; i++){
hashPwd = crypto.createHash('sha256')
.update(i.toString())
.digest('hex');
var first_and_second = hashPwd.slice(0, 2);
var second_and_third = hashPwd.slice(1, 3);
if(first_and_second == "00"){
times += 1;
}
if(second_and_third == "00"){
times += 1;
}
}
console.log("times: " + times);
It returns me "times: 991" for 128,000 hashes, and I expected about 1,000. So, I assume I'm correct. If you increase the number of hashes to millions, it becomes even more accurate.
Back to your question. "00" can appear in 63 positions. This means you have 63:16^2 chances, or 63 in 256, which is ~24.6%.
This new code checks for "00" in any position. We should get it ~24.6% of our times.
const crypto = require('crypto'),
hash = crypto.getHashes();
var times = 0;
for(var i=1; i<1000001; i++){
hashPwd = crypto.createHash('sha256')
.update(i.toString())
.digest('hex');
if(hashPwd.includes("00")){
let count = (hashPwd.match(/00/g) || []).length;
times+=count;
}
}
console.log("times: " + times);
It returned me 231570.