Skip to content

Incorrect use of blockhash

Description

The blockhash function in Solidity returns 0 when used with block.number or block.number + N, making it predictable and insecure for generating random numbers.

Examples

Insecure Code

solidity
uint256 random = uint256(blockhash(block.number));

Secure Code

solidity
uint256 random = rand(); // using OpenZeppelin's rand() function

Remediation

Use a secure random number generator, such as the rand() function from the OpenZeppelin library, to generate unpredictable numbers.

Rule Details

FieldValue
IDCODE-0474
CategoryCrypto
SeverityMEDIUM
CWECWE-341
ConfidenceHIGH
ImpactMEDIUM
LikelihoodLOW
ExploitabilityMODERATE
Tagsrandomness, predictability
OWASPN/A

References