Aim is to generate Asynchronous SHA-256/SHA-512 hash for a block in blockchain with NONCE.
It generates the appropriate hash according to the network difficulty.
HOW TO USE?
/
the above code generates SHA-256/SHA-512 hash for any string. To find the hash of "inputString", use "nonce"="" and "delimiter"="" or we could just use "getHash(inputString)"
/
// to find hash of a block with appropriate nonce
IMPORTANT : After importing make sure that you pass block hash, difficulty level and delimiter.
INPUT PARAMS of getBlockHash():
blockHash (string) => This is the hash of the block(element of block chain) before adding NONCE.
difficulty (integer) => This is the network difficulty set by the blockchain network(explained in detail below).
delimiter (string) => This is used when the NONCE appended to the blockHash for finding the appropriate hash based on the difficulty.
OUTPUT VARAIBLES of getBlockHash():
newString (string): blockHash concatenated with delimiter and nonce.
newHash (string): SHA-256/SHA-512 hash of newString.
nonce (number): Random integer when appended with blockHash along with delimiter gives appropriate hash.
Example:
* Let's say that the network difficulty is < 65536 (2^16).
* Since, SHA-256/SHA-512 hash is 256 bits long (32 bytes or 64 HexaDecimal characters);
* Based on the network difficulty, which is < 65536(2^16), the first 30 bytes of the hash should start with zeros.i,e; first 60 HexaDecimal characters should be zeros.
NOTE: The above example is used just for the sake of understanding. Hash required for the above mentioned difficulty is impractical.
Example code for above example:
* Let's say the blockHash is 6b88c087247aa2f07ee1c5956b8e1a9f4c7f892a70e324f1bb3d161e05ca107b (SHA-256/SHA-512 hash for the string "bitcoin").
NOTE: the above hash can be generated using crypto module of Node JS. Or it can also be generated using getHash(string, nonce, delimiter).
* given network difficulty is 60 (meaning the first 60 characters of the hash should start with zero).
* For simplicity, we can use the delimiter as "." .