Solidity – Understanding the Output of uint(keccak256(_str)) in Hashing and Randomness

hashkeccakrandomnesssolidityuint256

I'm doing the chapter 1 of the crypto zombies course.

        uint id = zombies.push(Zombie(_name, _dna)) - 1;
        NewZombie(id, _name, _dna);
    }

    function _generateRandomDna(string _str) private view returns (uint) {
        uint rand = uint(keccak256(_str));
        return rand % dnaModulus;
    }

the return of this is supposed to be a number of 16 numbers. per exemple "8356281049284737".
But for return rand % dnaModulus; . but i cant figure out what is the return of uint(keccak256(_str)).

Best Answer

Keccak256 is the EVM hash function that is used in Ethereum. It's quite similar to SHA3-256. You can also use this hash function outside of the EVM with tools like Web3js, ethers.js or just try it online here. In Solidity the call will return a hash in bytes32 form which can then easily be converted to a number by casting it to uint256.

Related Topic