Cryptographic global variables
Solidity provides cryptographic functions for hashing values within contract functions. There are two hashing functions – SHA2 and SHA3.
The sha3 or keccak256 function converts the input into a hash based on the sha3 algorithm, while sha256 converts the input into a hash based on the sha2 algorithm. It is recommended to use the keccak256 function for hashing needs.
The following code block illustrates this:
pragma solidity >=0.7.0 <0.9.0; contract CryptoFunctions { Â Â Â Â Â Â Â Â function CryptoDemo() pure public returns (bytes32, Â Â Â Â Â Â bytes32){ Â Â Â Â Â Â Â Â return (sha256("r"), keccak256("r")); Â Â Â Â } Â Â Â Â }
The result of executing this function is shown in the following screenshot. The result of both the keccak256
and sha3
functions is the same: