Function |
Properties |
---|---|
keccak256(bytes memory) returns (bytes32) | Computes the Keccak-256 hash of the input |
sha256(bytes memory) returns (bytes32) | Computes the SHA-256 hash of the input |
ripemd160(bytes memory) returns (bytes20) | Compute RIPEMD-160 hash of the input |
ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address) |
Recover the address associated with the public key from |
Here we want to take input arguments as string memory inside the function to get hash of the same;
function getRandomUserId(string memory _username) public view returns(bytes32){
return sha256(abi.encodePacked(_username)); //returns a result of 32 bytes
}
Suppose you are a curious and experimental nerd. You would like to return id of length as per user choice.Let's say you want to return the length of id generated to be 6. Then proceed like this.
//Here in the input section you can take input of _length as 6 to satisfy your hunger
function getRandomUserId(string memory _username, uint _length) public view returns (uint)
{
uint hashModulus = 10 ** _length;
uint random = uint(sha256(abi.encodePacked(_username)));
return random % hashModulus;
}
Caution:
Incase of ripemd160( ) it might not be feasible since it returns bytes20 & bytes20 can't be converted to uint256 explicitly
For many more exciting tutorials keep following my username @BlockTalks_Raj
!!!!!HAPPY LEARNING!!!!!