In R I can generate a 256hash with the following function.
> sha256 ( 'R is great')
[1] "3df43bfb52a031873872284bec472cdd8e1fe8803a6ba936c2446d4d920fd592"
however I know that if I want to get a hash with 4 leading zeros the nounce is
62137
and the final has is 000099bed1e274d13fd4b9864d52ee6f2a8d1f93941f8283b879ba949250b01c
however I want to show this in R. But when I add the nounce to the previous function it does not produce the same hash.
> sha256 ( '000099bed1e274d13fd4b9864d52ee6f2a8d1f93941f8283b879ba949250b01cR is great')
[1] "e1c828213b2463986fe8a5557c4c7b39ea42b792de1bb2a22c50dc2472f7ac6f"
how do I add the nounce to the message to produce the same hash?
source: https://andersbrownworth.com/blockchain/blockchain thank you.
CodePudding user response:
I've dug around in the source code of the example. The function getText
generates the string that is hashed. getText
adds the following elements:
- block number
- nonce
- data
- previous hash
In R, you can reproduce it with:
sha256( '262137R is great000015783b764259d382017d91a36d206d0600e2cbb3567748f46a33fe9297cf')
[1] "000099bed1e274d13fd4b9864d52ee6f2a8d1f93941f8283b879ba949250b01c"